Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 1 | // (C) Copyright Gennadiy Rozental 2002-2008. |
| 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 | |
| 6 | // See http://www.boost.org/libs/test for the library home page. |
| 7 | // |
| 8 | // File : $RCSfile$ |
| 9 | // |
| 10 | // Version : $Revision: 49312 $ |
| 11 | // |
| 12 | // Description : wraps strstream and stringstream (depends with one is present) |
| 13 | // to provide the unified interface |
| 14 | // *************************************************************************** |
| 15 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 16 | #ifndef NDNBOOST_WRAP_STRINGSTREAM_HPP_071894GER |
| 17 | #define NDNBOOST_WRAP_STRINGSTREAM_HPP_071894GER |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 18 | |
| 19 | // Boost.Test |
| 20 | #include <ndnboost/test/detail/config.hpp> |
| 21 | |
| 22 | // STL |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 23 | #ifdef NDNBOOST_NO_STRINGSTREAM |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 24 | #include <strstream> // for std::ostrstream |
| 25 | #else |
| 26 | #include <sstream> // for std::ostringstream |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 27 | #endif // NDNBOOST_NO_STRINGSTREAM |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 28 | |
| 29 | #include <ndnboost/test/detail/suppress_warnings.hpp> |
| 30 | |
| 31 | //____________________________________________________________________________// |
| 32 | |
| 33 | namespace ndnboost { |
| 34 | |
| 35 | // ************************************************************************** // |
| 36 | // ************** basic_wrap_stringstream ************** // |
| 37 | // ************************************************************************** // |
| 38 | |
| 39 | template<typename CharT> |
| 40 | class basic_wrap_stringstream { |
| 41 | public: |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 42 | #if defined(NDNBOOST_CLASSIC_IOSTREAMS) |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 43 | typedef std::ostringstream wrapped_stream; |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 44 | #elif defined(NDNBOOST_NO_STRINGSTREAM) |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 45 | typedef std::basic_ostrstream<CharT> wrapped_stream; |
| 46 | #else |
| 47 | typedef std::basic_ostringstream<CharT> wrapped_stream; |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 48 | #endif // NDNBOOST_NO_STRINGSTREAM |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 49 | // Access methods |
| 50 | basic_wrap_stringstream& ref(); |
| 51 | wrapped_stream& stream(); |
| 52 | std::basic_string<CharT> const& str(); |
| 53 | |
| 54 | private: |
| 55 | // Data members |
| 56 | wrapped_stream m_stream; |
| 57 | std::basic_string<CharT> m_str; |
| 58 | }; |
| 59 | |
| 60 | //____________________________________________________________________________// |
| 61 | |
| 62 | template <typename CharT, typename T> |
| 63 | inline basic_wrap_stringstream<CharT>& |
| 64 | operator<<( basic_wrap_stringstream<CharT>& targ, T const& t ) |
| 65 | { |
| 66 | targ.stream() << t; |
| 67 | return targ; |
| 68 | } |
| 69 | |
| 70 | //____________________________________________________________________________// |
| 71 | |
| 72 | template <typename CharT> |
| 73 | inline typename basic_wrap_stringstream<CharT>::wrapped_stream& |
| 74 | basic_wrap_stringstream<CharT>::stream() |
| 75 | { |
| 76 | return m_stream; |
| 77 | } |
| 78 | |
| 79 | //____________________________________________________________________________// |
| 80 | |
| 81 | template <typename CharT> |
| 82 | inline basic_wrap_stringstream<CharT>& |
| 83 | basic_wrap_stringstream<CharT>::ref() |
| 84 | { |
| 85 | return *this; |
| 86 | } |
| 87 | |
| 88 | //____________________________________________________________________________// |
| 89 | |
| 90 | template <typename CharT> |
| 91 | inline std::basic_string<CharT> const& |
| 92 | basic_wrap_stringstream<CharT>::str() |
| 93 | { |
| 94 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 95 | #ifdef NDNBOOST_NO_STRINGSTREAM |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 96 | m_str.assign( m_stream.str(), m_stream.pcount() ); |
| 97 | m_stream.freeze( false ); |
| 98 | #else |
| 99 | m_str = m_stream.str(); |
| 100 | #endif |
| 101 | |
| 102 | return m_str; |
| 103 | } |
| 104 | |
| 105 | //____________________________________________________________________________// |
| 106 | |
| 107 | template <typename CharT> |
| 108 | inline basic_wrap_stringstream<CharT>& |
| 109 | operator<<( basic_wrap_stringstream<CharT>& targ, basic_wrap_stringstream<CharT>& src ) |
| 110 | { |
| 111 | targ << src.str(); |
| 112 | return targ; |
| 113 | } |
| 114 | |
| 115 | //____________________________________________________________________________// |
| 116 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 117 | #if NDNBOOST_TEST_USE_STD_LOCALE |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 118 | |
| 119 | template <typename CharT> |
| 120 | inline basic_wrap_stringstream<CharT>& |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 121 | operator<<( basic_wrap_stringstream<CharT>& targ, std::ios_base& (NDNBOOST_TEST_CALL_DECL *man)(std::ios_base&) ) |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 122 | { |
| 123 | targ.stream() << man; |
| 124 | return targ; |
| 125 | } |
| 126 | |
| 127 | //____________________________________________________________________________// |
| 128 | |
| 129 | template<typename CharT,typename Elem,typename Tr> |
| 130 | inline basic_wrap_stringstream<CharT>& |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 131 | operator<<( basic_wrap_stringstream<CharT>& targ, std::basic_ostream<Elem,Tr>& (NDNBOOST_TEST_CALL_DECL *man)(std::basic_ostream<Elem, Tr>&) ) |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 132 | { |
| 133 | targ.stream() << man; |
| 134 | return targ; |
| 135 | } |
| 136 | |
| 137 | //____________________________________________________________________________// |
| 138 | |
| 139 | template<typename CharT,typename Elem,typename Tr> |
| 140 | inline basic_wrap_stringstream<CharT>& |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 141 | operator<<( basic_wrap_stringstream<CharT>& targ, std::basic_ios<Elem, Tr>& (NDNBOOST_TEST_CALL_DECL *man)(std::basic_ios<Elem, Tr>&) ) |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 142 | { |
| 143 | targ.stream() << man; |
| 144 | return targ; |
| 145 | } |
| 146 | |
| 147 | //____________________________________________________________________________// |
| 148 | |
| 149 | #endif |
| 150 | |
| 151 | // ************************************************************************** // |
| 152 | // ************** wrap_stringstream ************** // |
| 153 | // ************************************************************************** // |
| 154 | |
| 155 | typedef basic_wrap_stringstream<char> wrap_stringstream; |
| 156 | typedef basic_wrap_stringstream<wchar_t> wrap_wstringstream; |
| 157 | |
| 158 | } // namespace ndnboost |
| 159 | |
| 160 | //____________________________________________________________________________// |
| 161 | |
| 162 | #include <ndnboost/test/detail/enable_warnings.hpp> |
| 163 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 164 | #endif // NDNBOOST_WRAP_STRINGSTREAM_HPP_071894GER |