to_string.hpp 2.2 KB

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