print.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #ifndef BOOST_LEAF_DETAIL_PRINT_HPP_INCLUDED
  2. #define BOOST_LEAF_DETAIL_PRINT_HPP_INCLUDED
  3. // Copyright 2018-2022 Emil Dotchevski and Reverge Studios, Inc.
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/leaf/config.hpp>
  7. #include <boost/leaf/detail/demangle.hpp>
  8. #if BOOST_LEAF_CFG_DIAGNOSTICS
  9. #include <type_traits>
  10. #include <exception>
  11. #include <iosfwd>
  12. #include <cstring>
  13. namespace boost { namespace leaf {
  14. namespace leaf_detail
  15. {
  16. template <class T, class E = void>
  17. struct is_printable: std::false_type
  18. {
  19. };
  20. template <class T>
  21. struct is_printable<T, decltype(std::declval<std::ostream&>()<<std::declval<T const &>(), void())>: std::true_type
  22. {
  23. };
  24. ////////////////////////////////////////
  25. template <class T, class E = void>
  26. struct has_printable_member_value: std::false_type
  27. {
  28. };
  29. template <class T>
  30. struct has_printable_member_value<T, decltype(std::declval<std::ostream&>()<<std::declval<T const &>().value, void())>: std::true_type
  31. {
  32. };
  33. ////////////////////////////////////////
  34. template <
  35. class Wrapper,
  36. bool WrapperPrintable = is_printable<Wrapper>::value,
  37. bool ValuePrintable = has_printable_member_value<Wrapper>::value,
  38. bool IsException = std::is_base_of<std::exception,Wrapper>::value,
  39. bool IsEnum = std::is_enum<Wrapper>::value>
  40. struct diagnostic;
  41. template <class Wrapper, bool ValuePrintable, bool IsException, bool IsEnum>
  42. struct diagnostic<Wrapper, true, ValuePrintable, IsException, IsEnum>
  43. {
  44. static constexpr bool is_invisible = false;
  45. template <class CharT, class Traits>
  46. static void print( std::basic_ostream<CharT, Traits> & os, Wrapper const & x )
  47. {
  48. os << x;
  49. }
  50. };
  51. template <class Wrapper, bool IsException, bool IsEnum>
  52. struct diagnostic<Wrapper, false, true, IsException, IsEnum>
  53. {
  54. static constexpr bool is_invisible = false;
  55. template <class CharT, class Traits>
  56. static void print( std::basic_ostream<CharT, Traits> & os, Wrapper const & x )
  57. {
  58. os << type<Wrapper>() << ": " << x.value;
  59. }
  60. };
  61. template <class Wrapper, bool IsEnum>
  62. struct diagnostic<Wrapper, false, false, true, IsEnum>
  63. {
  64. static constexpr bool is_invisible = false;
  65. template <class CharT, class Traits>
  66. static void print( std::basic_ostream<CharT, Traits> & os, Wrapper const & ex )
  67. {
  68. os << type<Wrapper>() << ": std::exception::what(): " << ex.what();
  69. }
  70. };
  71. template <class Wrapper>
  72. struct diagnostic<Wrapper, false, false, false, false>
  73. {
  74. static constexpr bool is_invisible = false;
  75. template <class CharT, class Traits>
  76. static void print( std::basic_ostream<CharT, Traits> & os, Wrapper const & )
  77. {
  78. os << type<Wrapper>() << ": {Non-Printable}";
  79. }
  80. };
  81. template <class Wrapper>
  82. struct diagnostic<Wrapper, false, false, false, true>
  83. {
  84. static constexpr bool is_invisible = false;
  85. template <class CharT, class Traits>
  86. static void print( std::basic_ostream<CharT, Traits> & os, Wrapper const & w )
  87. {
  88. os << type<Wrapper>() << ": " << static_cast<typename std::underlying_type<Wrapper>::type>(w);
  89. }
  90. };
  91. template <>
  92. struct diagnostic<std::exception_ptr, false, false, false>
  93. {
  94. static constexpr bool is_invisible = true;
  95. template <class CharT, class Traits>
  96. BOOST_LEAF_CONSTEXPR static void print( std::basic_ostream<CharT, Traits> &, std::exception_ptr const & )
  97. {
  98. }
  99. };
  100. }
  101. } }
  102. #endif
  103. #endif