2 // Boost format ----------------------------------------------------
3 // See http://www.boost.org for updates, documentation, and revision history.
5 // (C) Samuel Krempp 2003
6 // Permission to copy, use, modify, sell and
7 // distribute this software is granted provided this copyright notice appears
8 // in all copies. This software is provided "as is" without express or implied
9 // warranty, and with no claim as to its suitability for any purpose.
11 // ------------------------------------------------------------------------------
12 // outsstream<Ch, Tr> is a class extending stringstream by adding :
13 // clear_buffer() method, and
14 // access to the [pbase(), pptr()[ part of the 'output sequence'
15 // (see ยง27.5.1 of the C++ standard)
16 // if sstream is not available, it uses strstream and is slightly different.
17 // ------------------------------------------------------------------------------
19 #ifndef BOOST_FORMAT_OUTSSTREAM_H
20 #define BOOST_FORMAT_OUTSSTREAM_H
22 #include <boost/assert.hpp>
23 #include <boost/utility/base_from_member.hpp>
24 #include <boost/format/detail/config_macros.hpp>
26 #if !defined(BOOST_NO_STRINGSTREAM) && !defined(BOOST_FORMAT_IGNORE_STRINGSTREAM)
31 #endif // BOOST_NO_STRING_STREAM
37 #if !defined(BOOST_NO_STRINGSTREAM) && !defined(BOOST_FORMAT_IGNORE_STRINGSTREAM)
40 //---- The stringstream way ---------------------------------------------------
43 // --- class steal_basic_stringbuf : steals pbase(), pptr() & co -----------
44 template<class Ch, class Tr = BOOST_IO_STD char_traits<Ch> >
45 class steal_basic_stringbuf : public std::basic_stringbuf<Ch, Tr>
47 typedef std::basic_stringbuf<Ch, Tr> buff_t;
49 typedef std::basic_string<Ch,Tr> string_type;
51 // get [pbase, pptr[ from stringbuf::str(), which returns [pbase, epptr[ :
52 string_type cur_str() const { return string_type(this->str(), 0, pcount()); }
54 // publicize those functions (protected in stringbuf) :
55 std::streamsize pcount() const { return pptr() - pbase(); }
56 const Ch * pbase() const { return buff_t::pbase(); }
57 const Ch * pptr() const { return buff_t::pptr(); }
58 const Ch * epptr() const { return buff_t::epptr(); }
59 // added convenience function :
64 // --- class basic_outsstream -----------------------------------------------
65 template<class Ch, class Tr = BOOST_IO_STD char_traits<Ch> >
66 class basic_outsstream : boost::base_from_member<steal_basic_stringbuf<Ch, Tr> >,
67 public BOOST_IO_STD basic_ostream<Ch, Tr>
68 // use stringbuf with its stolen protected members,
69 // publicly derived from basic_ostream to make this class a stream.
72 typedef std::basic_string<Ch,Tr> string_type;
73 basic_outsstream() : pbase_type(),
74 std::basic_ostream<Ch,Tr>( & sb() ) {}
75 // buffer access via strings
76 string_type str() const { return sb().str(); } // [begin, end[
77 string_type cur_str() const { return sb().cur_str(); } // [begin, cur[
79 // copy-less access (note the pointers can be invalidated when modifying the stream)
80 std::streamsize pcount() const { return sb().pcount(); }
81 const Ch * begin() const { return sb().pbase(); }
82 const Ch * cur() const { return sb().pptr(); }
83 const Ch * end() const { return sb().epptr(); }
85 void clear_buffer() { sb().clear_buffer(); }
87 typedef boost::base_from_member<steal_basic_stringbuf<Ch, Tr> > pbase_type;
88 steal_basic_stringbuf<Ch, Tr> & sb() { return pbase_type::member; }
89 steal_basic_stringbuf<Ch, Tr> const& sb() const { return pbase_type::member; }
91 #else // BOOST_NO_STRINGSTREAM
95 //---- The strstream way ------------------------------------------------------
98 #if !( BOOST_WORKAROUND(__GNUC__, <3) && defined(__STL_CONFIG_H) )
99 class Tr = BOOST_IO_STD char_traits<Ch> >
101 class Tr = std::string_char_traits<Ch> >
103 class basic_outsstream; // we define only the <char> specialisaton
106 // --- class basic_outsstream -----------------------------------------------
108 class basic_outsstream<char, Tr> : private BOOST_IO_STD strstreambuf,
109 public std::basic_ostream<char, Tr>
113 typedef std::basic_string<Ch,Tr> string_type;
114 typedef BOOST_IO_STD strstreambuf buff_t;
115 typedef std::basic_ostream<char, Tr> stream_t;
119 // ! physically copy chars :
121 string_type cur_str() const { return string_type(begin(), cur()); }
123 int freeze() const { return buff_t::freeze(); }
124 void freeze(int x) { buff_t::freeze(x); }
126 // copy-less access (be careful, the pointer are invalidated when modifying the stream) :
127 std::streamsize pcount() const { return cur()-begin(); }
128 const Ch * begin() const { return buff_t::pbase(); }
129 const Ch * cur() const { return buff_t::pptr(); }
130 const Ch * end() const { return buff_t::epptr(); }
135 #endif // BOOST_NO_STRINGSTREAM
138 typedef basic_outsstream<char> outsstream;
140 } // namespace boost::io
144 #include <boost/format/outsstream_impl.hpp> // implementation
146 #endif // BOOST_FORMAT_OUTSSTREAM_H include guard