mpdebug.hh 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra - Metaprogramming debug utilities.
  3. // (c) Daniel Llorens - 2011, 2019
  4. // This library is free software; you can redistribute it and/or modify it under
  5. // the terms of the GNU Lesser General Public License as published by the Free
  6. // Software Foundation; either version 3 of the License, or (at your option) any
  7. // later version.
  8. #pragma once
  9. #include "ra/tuples.hh"
  10. #include <iosfwd>
  11. #include <string>
  12. #include <typeinfo>
  13. #include <cxxabi.h>
  14. namespace ra::mp {
  15. template <class type_, bool condition=false>
  16. struct show
  17. {
  18. using type = type_;
  19. static bool const value = condition;
  20. static_assert(condition, "bad type");
  21. };
  22. // Prints value recursively, e.g. for int_c trees.
  23. template <class A> struct print_ilist_t {};
  24. template <class A> std::ostream &
  25. operator<<(std::ostream & o, print_ilist_t<A> const & a)
  26. {
  27. if constexpr (is_tuple<A>) {
  28. std::apply([&o](auto ... a) { ((o << "[") << ... << print_ilist_t<decltype(a)> {}) << "]"; }, A {});
  29. return o;
  30. } else {
  31. return (o << A::value << " ");
  32. }
  33. }
  34. template <class T>
  35. std::string
  36. type_name()
  37. {
  38. int status;
  39. auto s = abi::__cxa_demangle(typeid(T).name(), NULL, NULL, &status);
  40. std::string out(s);
  41. free(s);
  42. return out;
  43. }
  44. template <class A, int ... I> struct check_idx { constexpr static bool value = false; };
  45. template <> struct check_idx<nil> { constexpr static bool value = true; };
  46. template <class A0, int I0, class ... A, int ... I>
  47. struct check_idx<tuple<A0, A ...>, I0, I ...>
  48. {
  49. constexpr static bool value = (A0::value==I0) && check_idx<tuple<A ...>, I ...>::value;
  50. };
  51. } // namespace ra::mp