to_string_stub.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 UUID_E788439ED9F011DCB181F25B55D89593
  5. #define UUID_E788439ED9F011DCB181F25B55D89593
  6. #include <boost/exception/to_string.hpp>
  7. #include <boost/exception/detail/object_hex_dump.hpp>
  8. #include <boost/assert.hpp>
  9. namespace
  10. boost
  11. {
  12. namespace
  13. exception_detail
  14. {
  15. template <bool ToStringAvailable>
  16. struct
  17. to_string_dispatcher
  18. {
  19. template <class T,class Stub>
  20. static
  21. std::string
  22. convert( T const & x, Stub )
  23. {
  24. return to_string(x);
  25. }
  26. };
  27. template <>
  28. struct
  29. to_string_dispatcher<false>
  30. {
  31. template <class T,class Stub>
  32. static
  33. std::string
  34. convert( T const & x, Stub s )
  35. {
  36. return s(x);
  37. }
  38. template <class T>
  39. static
  40. std::string
  41. convert( T const & x, std::string s )
  42. {
  43. return s;
  44. }
  45. template <class T>
  46. static
  47. std::string
  48. convert( T const & x, char const * s )
  49. {
  50. BOOST_ASSERT(s!=0);
  51. return s;
  52. }
  53. };
  54. namespace
  55. to_string_dispatch
  56. {
  57. template <class T,class Stub>
  58. inline
  59. std::string
  60. dispatch( T const & x, Stub s )
  61. {
  62. return to_string_dispatcher<has_to_string<T>::value>::convert(x,s);
  63. }
  64. }
  65. template <class T>
  66. inline
  67. std::string
  68. string_stub_dump( T const & x )
  69. {
  70. return "[ " + exception_detail::object_hex_dump(x) + " ]";
  71. }
  72. }
  73. template <class T>
  74. inline
  75. std::string
  76. to_string_stub( T const & x )
  77. {
  78. return exception_detail::to_string_dispatch::dispatch(x,&exception_detail::string_stub_dump<T>);
  79. }
  80. template <class T,class Stub>
  81. inline
  82. std::string
  83. to_string_stub( T const & x, Stub s )
  84. {
  85. return exception_detail::to_string_dispatch::dispatch(x,s);
  86. }
  87. }
  88. #endif