to_string_stub.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc.
  2. //Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. //file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #ifndef BOOST_EXCEPTION_E788439ED9F011DCB181F25B55D89593
  5. #define BOOST_EXCEPTION_E788439ED9F011DCB181F25B55D89593
  6. #include <boost/exception/to_string.hpp>
  7. #include <boost/exception/detail/object_hex_dump.hpp>
  8. #include <boost/assert.hpp>
  9. #ifndef BOOST_EXCEPTION_ENABLE_WARNINGS
  10. #if __GNUC__*100+__GNUC_MINOR__>301
  11. #pragma GCC system_header
  12. #endif
  13. #ifdef __clang__
  14. #pragma clang system_header
  15. #endif
  16. #ifdef _MSC_VER
  17. #pragma warning(push,1)
  18. #endif
  19. #endif
  20. namespace
  21. boost
  22. {
  23. namespace
  24. exception_detail
  25. {
  26. template <bool ToStringAvailable>
  27. struct
  28. to_string_dispatcher
  29. {
  30. template <class T,class Stub>
  31. static
  32. std::string
  33. convert( T const & x, Stub )
  34. {
  35. return to_string(x);
  36. }
  37. };
  38. template <>
  39. struct
  40. to_string_dispatcher<false>
  41. {
  42. template <class T,class Stub>
  43. static
  44. std::string
  45. convert( T const & x, Stub s )
  46. {
  47. return s(x);
  48. }
  49. template <class T>
  50. static
  51. std::string
  52. convert( T const & x, std::string s )
  53. {
  54. return s;
  55. }
  56. template <class T>
  57. static
  58. std::string
  59. convert( T const & x, char const * s )
  60. {
  61. BOOST_ASSERT(s!=0);
  62. return s;
  63. }
  64. };
  65. namespace
  66. to_string_dispatch
  67. {
  68. template <class T,class Stub>
  69. inline
  70. std::string
  71. dispatch( T const & x, Stub s )
  72. {
  73. return to_string_dispatcher<has_to_string<T>::value>::convert(x,s);
  74. }
  75. }
  76. template <class T>
  77. inline
  78. std::string
  79. string_stub_dump( T const & x )
  80. {
  81. return "[ " + exception_detail::object_hex_dump(x) + " ]";
  82. }
  83. }
  84. template <class T>
  85. inline
  86. std::string
  87. to_string_stub( T const & x )
  88. {
  89. return exception_detail::to_string_dispatch::dispatch(x,&exception_detail::string_stub_dump<T>);
  90. }
  91. template <class T,class Stub>
  92. inline
  93. std::string
  94. to_string_stub( T const & x, Stub s )
  95. {
  96. return exception_detail::to_string_dispatch::dispatch(x,s);
  97. }
  98. template <class T,class U,class Stub>
  99. inline
  100. std::string
  101. to_string_stub( std::pair<T,U> const & x, Stub s )
  102. {
  103. return std::string("(") + to_string_stub(x.first,s) + ',' + to_string_stub(x.second,s) + ')';
  104. }
  105. }
  106. #if defined(_MSC_VER) && !defined(BOOST_EXCEPTION_ENABLE_WARNINGS)
  107. #pragma warning(pop)
  108. #endif
  109. #endif