mpdebug.H 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // (c) Daniel Llorens - 2011
  2. // This library is free software; you can redistribute it and/or modify it under
  3. // the terms of the GNU Lesser General Public License as published by the Free
  4. // Software Foundation; either version 3 of the License, or (at your option) any
  5. // later version.
  6. #pragma once
  7. #include <sys/types.h>
  8. #include "ra/tuple-list.H"
  9. #include <typeinfo>
  10. /// @file mpdebug.H
  11. /// @brief Metaprogramming debugging utilities.
  12. namespace mp {
  13. template <ssize_t value_, bool condition=false>
  14. struct show_number
  15. {
  16. static ssize_t const value = value_;
  17. static_assert(condition, "bad number");
  18. };
  19. template <class type_, bool condition=false>
  20. struct show_type
  21. {
  22. using type = type_;
  23. static bool const value = condition;
  24. static_assert(condition, "bad type");
  25. };
  26. // Prints recursively, i.e. int_t trees.
  27. template <class A> struct print_int_list {};
  28. template <class A> std::ostream &
  29. operator<<(std::ostream & o, print_int_list<A> const & a)
  30. {
  31. return print_int_list<A>::f(o);
  32. }
  33. template <class ... A>
  34. struct print_int_list<std::tuple<A ...> >
  35. {
  36. static std::ostream & f(std::ostream & o)
  37. {
  38. return ((o << "[") << ... << print_int_list<A> {}) << "]";
  39. }
  40. };
  41. template <int i0>
  42. struct print_int_list<mp::int_t<i0> >
  43. {
  44. static std::ostream & f(std::ostream & o)
  45. {
  46. return (o << i0 << " ");
  47. }
  48. };
  49. template <class T>
  50. std::string
  51. type_name()
  52. {
  53. using TR = typename std::remove_reference<T>::type;
  54. std::string r = typeid(TR).name();
  55. if (std::is_const<TR>::value)
  56. r += " const";
  57. if (std::is_volatile<TR>::value)
  58. r += " volatile";
  59. if (std::is_lvalue_reference<T>::value)
  60. r += " &";
  61. else if (std::is_rvalue_reference<T>::value)
  62. r += " &&";
  63. return r;
  64. }
  65. } // namespace MP