wrap_stringstream.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // (C) Copyright Gennadiy Rozental 2001.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/test for the library home page.
  6. //
  7. // File : $RCSfile$
  8. //
  9. // Version : $Revision$
  10. //
  11. // Description : wraps strstream and stringstream (depends with one is present)
  12. // to provide the unified interface
  13. // ***************************************************************************
  14. #ifndef BOOST_TEST_UTILS_WRAP_STRINGSTREAM_HPP
  15. #define BOOST_TEST_UTILS_WRAP_STRINGSTREAM_HPP
  16. // Boost.Test
  17. #include <boost/test/detail/config.hpp>
  18. // STL
  19. #ifdef BOOST_NO_STRINGSTREAM
  20. #include <strstream> // for std::ostrstream
  21. #else
  22. #include <sstream> // for std::ostringstream
  23. #endif // BOOST_NO_STRINGSTREAM
  24. #include <boost/test/detail/suppress_warnings.hpp>
  25. //____________________________________________________________________________//
  26. namespace boost {
  27. // ************************************************************************** //
  28. // ************** basic_wrap_stringstream ************** //
  29. // ************************************************************************** //
  30. template<typename CharT>
  31. class basic_wrap_stringstream {
  32. public:
  33. #if defined(BOOST_CLASSIC_IOSTREAMS)
  34. typedef std::ostringstream wrapped_stream;
  35. #elif defined(BOOST_NO_STRINGSTREAM)
  36. typedef std::basic_ostrstream<CharT> wrapped_stream;
  37. #else
  38. typedef std::basic_ostringstream<CharT> wrapped_stream;
  39. #endif // BOOST_NO_STRINGSTREAM
  40. // Access methods
  41. basic_wrap_stringstream& ref();
  42. wrapped_stream& stream();
  43. std::basic_string<CharT> const& str();
  44. private:
  45. // Data members
  46. wrapped_stream m_stream;
  47. std::basic_string<CharT> m_str;
  48. };
  49. //____________________________________________________________________________//
  50. template <typename CharT, typename T>
  51. inline basic_wrap_stringstream<CharT>&
  52. operator<<( basic_wrap_stringstream<CharT>& targ, T const& t )
  53. {
  54. targ.stream() << t;
  55. return targ;
  56. }
  57. //____________________________________________________________________________//
  58. template <typename CharT>
  59. inline typename basic_wrap_stringstream<CharT>::wrapped_stream&
  60. basic_wrap_stringstream<CharT>::stream()
  61. {
  62. return m_stream;
  63. }
  64. //____________________________________________________________________________//
  65. template <typename CharT>
  66. inline basic_wrap_stringstream<CharT>&
  67. basic_wrap_stringstream<CharT>::ref()
  68. {
  69. return *this;
  70. }
  71. //____________________________________________________________________________//
  72. template <typename CharT>
  73. inline std::basic_string<CharT> const&
  74. basic_wrap_stringstream<CharT>::str()
  75. {
  76. #ifdef BOOST_NO_STRINGSTREAM
  77. m_str.assign( m_stream.str(), m_stream.pcount() );
  78. m_stream.freeze( false );
  79. #else
  80. m_str = m_stream.str();
  81. #endif
  82. return m_str;
  83. }
  84. //____________________________________________________________________________//
  85. template <typename CharT>
  86. inline basic_wrap_stringstream<CharT>&
  87. operator<<( basic_wrap_stringstream<CharT>& targ, basic_wrap_stringstream<CharT>& src )
  88. {
  89. targ << src.str();
  90. return targ;
  91. }
  92. //____________________________________________________________________________//
  93. #if BOOST_TEST_USE_STD_LOCALE
  94. template <typename CharT>
  95. inline basic_wrap_stringstream<CharT>&
  96. operator<<( basic_wrap_stringstream<CharT>& targ, std::ios_base& (BOOST_TEST_CALL_DECL *man)(std::ios_base&) )
  97. {
  98. targ.stream() << man;
  99. return targ;
  100. }
  101. //____________________________________________________________________________//
  102. template<typename CharT,typename Elem,typename Tr>
  103. inline basic_wrap_stringstream<CharT>&
  104. operator<<( basic_wrap_stringstream<CharT>& targ, std::basic_ostream<Elem,Tr>& (BOOST_TEST_CALL_DECL *man)(std::basic_ostream<Elem, Tr>&) )
  105. {
  106. targ.stream() << man;
  107. return targ;
  108. }
  109. //____________________________________________________________________________//
  110. template<typename CharT,typename Elem,typename Tr>
  111. inline basic_wrap_stringstream<CharT>&
  112. operator<<( basic_wrap_stringstream<CharT>& targ, std::basic_ios<Elem, Tr>& (BOOST_TEST_CALL_DECL *man)(std::basic_ios<Elem, Tr>&) )
  113. {
  114. targ.stream() << man;
  115. return targ;
  116. }
  117. //____________________________________________________________________________//
  118. #endif
  119. // ************************************************************************** //
  120. // ************** wrap_stringstream ************** //
  121. // ************************************************************************** //
  122. typedef basic_wrap_stringstream<char> wrap_stringstream;
  123. typedef basic_wrap_stringstream<wchar_t> wrap_wstringstream;
  124. } // namespace boost
  125. #include <boost/test/detail/enable_warnings.hpp>
  126. #endif // BOOST_TEST_UTILS_WRAP_STRINGSTREAM_HPP