to_string.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_7E48761AD92811DC9011477D56D89593
  5. #define BOOST_EXCEPTION_7E48761AD92811DC9011477D56D89593
  6. #include <boost/utility/enable_if.hpp>
  7. #include <boost/exception/detail/is_output_streamable.hpp>
  8. #include <sstream>
  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. template <class T,class U>
  24. std::string to_string( std::pair<T,U> const & );
  25. std::string to_string( std::exception const & );
  26. namespace
  27. to_string_detail
  28. {
  29. template <class T>
  30. typename disable_if<is_output_streamable<T>,char>::type to_string( T const & );
  31. using boost::to_string;
  32. template <class,bool IsOutputStreamable>
  33. struct has_to_string_impl;
  34. template <class T>
  35. struct
  36. has_to_string_impl<T,true>
  37. {
  38. enum e { value=1 };
  39. };
  40. template <class T>
  41. struct
  42. has_to_string_impl<T,false>
  43. {
  44. static T const & f();
  45. enum e { value=1!=sizeof(to_string(f())) };
  46. };
  47. }
  48. template <class T>
  49. inline
  50. typename enable_if<is_output_streamable<T>,std::string>::type
  51. to_string( T const & x )
  52. {
  53. std::ostringstream out;
  54. out << x;
  55. return out.str();
  56. }
  57. template <class T>
  58. struct
  59. has_to_string
  60. {
  61. enum e { value=to_string_detail::has_to_string_impl<T,is_output_streamable<T>::value>::value };
  62. };
  63. template <class T,class U>
  64. inline
  65. std::string
  66. to_string( std::pair<T,U> const & x )
  67. {
  68. return std::string("(") + to_string(x.first) + ',' + to_string(x.second) + ')';
  69. }
  70. inline
  71. std::string
  72. to_string( std::exception const & x )
  73. {
  74. return x.what();
  75. }
  76. }
  77. #if defined(_MSC_VER) && !defined(BOOST_EXCEPTION_ENABLE_WARNINGS)
  78. #pragma warning(pop)
  79. #endif
  80. #endif