to_string.hpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. namespace
  10. boost
  11. {
  12. namespace
  13. to_string_detail
  14. {
  15. template <class T>
  16. typename disable_if<is_output_streamable<T>,char>::type to_string( T const & );
  17. template <class,bool IsOutputStreamable>
  18. struct has_to_string_impl;
  19. template <class T>
  20. struct
  21. has_to_string_impl<T,true>
  22. {
  23. enum e { value=1 };
  24. };
  25. template <class T>
  26. struct
  27. has_to_string_impl<T,false>
  28. {
  29. static T const & f();
  30. enum e { value=1!=sizeof(to_string(f())) };
  31. };
  32. }
  33. template <class T>
  34. inline
  35. typename enable_if<is_output_streamable<T>,std::string>::type
  36. to_string( T const & x )
  37. {
  38. std::ostringstream out;
  39. out << x;
  40. return out.str();
  41. }
  42. template <class T>
  43. struct
  44. has_to_string
  45. {
  46. enum e { value=to_string_detail::has_to_string_impl<T,is_output_streamable<T>::value>::value };
  47. };
  48. template <class T,class U>
  49. inline
  50. std::string
  51. to_string( std::pair<T,U> const & x )
  52. {
  53. return std::string("(") + to_string(x.first) + ',' + to_string(x.second) + ')';
  54. }
  55. inline
  56. std::string
  57. to_string( std::exception const & x )
  58. {
  59. return x.what();
  60. }
  61. }
  62. #endif