mpdebug.hh 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 "tuples.hh"
  10. #include <typeinfo>
  11. #include <sys/types.h>
  12. namespace ra::mp {
  13. template <dim_t value_, bool condition=false>
  14. struct show_number
  15. {
  16. static dim_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 value recursively, e.g. for int_c 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. if constexpr (is_tuple_v<A>) {
  32. std::apply([&o](auto ... a) { ((o << "[") << ... << print_int_list<decltype(a)> {}) << "]"; }, A {});
  33. return o;
  34. } else {
  35. return (o << A::value << " ");
  36. }
  37. }
  38. template <class T>
  39. std::string
  40. type_name()
  41. {
  42. using TR = std::remove_cvref_t<T>;
  43. std::string r = typeid(TR).name();
  44. if (std::is_const_v<TR>)
  45. r += " const";
  46. if (std::is_volatile_v<TR>)
  47. r += " volatile";
  48. if (std::is_lvalue_reference_v<T>)
  49. r += " &";
  50. else if (std::is_rvalue_reference_v<T>)
  51. r += " &&";
  52. return r;
  53. }
  54. } // namespace ra::mp