mpdebug.hh 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 <typeinfo>
  11. #include <sys/types.h>
  12. namespace ra::mp {
  13. template <class type_, bool condition=false>
  14. struct show
  15. {
  16. using type = type_;
  17. static bool const value = condition;
  18. static_assert(condition, "bad type");
  19. };
  20. // Prints value recursively, e.g. for int_c trees.
  21. template <class A> struct print_int_list {};
  22. template <class A> std::ostream &
  23. operator<<(std::ostream & o, print_int_list<A> const & a)
  24. {
  25. if constexpr (is_tuple<A>) {
  26. std::apply([&o](auto ... a) { ((o << "[") << ... << print_int_list<decltype(a)> {}) << "]"; }, A {});
  27. return o;
  28. } else {
  29. return (o << A::value << " ");
  30. }
  31. }
  32. template <class T>
  33. std::string
  34. type_name()
  35. {
  36. using TR = std::remove_cvref_t<T>;
  37. std::string r = typeid(TR).name();
  38. if (std::is_const_v<TR>)
  39. r += " const";
  40. if (std::is_volatile_v<TR>)
  41. r += " volatile";
  42. if (std::is_lvalue_reference_v<T>)
  43. r += " &";
  44. else if (std::is_rvalue_reference_v<T>)
  45. r += " &&";
  46. return r;
  47. }
  48. } // namespace ra::mp