mpdebug.H 1.6 KB

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