1 // ----------------------------------------------------------------------------
2 // alt_sstream.hpp : alternative stringstream
3 // ----------------------------------------------------------------------------
5 // Copyright Samuel Krempp 2003. Use, modification, and distribution are
6 // subject to the Boost Software License, Version 1.0. (See accompanying
7 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 // See http://www.boost.org/libs/format for library home page
11 // ----------------------------------------------------------------------------
15 #ifndef BOOST_SK_ALT_SSTREAM_HPP
16 #define BOOST_SK_ALT_SSTREAM_HPP
19 #include <boost/format/detail/compat_workarounds.hpp>
20 #include <boost/utility/base_from_member.hpp>
21 #include <boost/shared_ptr.hpp>
22 #include <boost/assert.hpp>
27 template<class Ch, class Tr=::std::char_traits<Ch>,
28 class Alloc=::std::allocator<Ch> >
29 class basic_altstringbuf;
31 template<class Ch, class Tr =::std::char_traits<Ch>,
32 class Alloc=::std::allocator<Ch> >
33 class basic_oaltstringstream;
36 template<class Ch, class Tr, class Alloc>
37 class basic_altstringbuf
38 : public ::std::basic_streambuf<Ch, Tr>
40 typedef ::std::basic_streambuf<Ch, Tr> streambuf_t;
41 typedef typename CompatAlloc<Alloc>::compatible_type compat_allocator_type;
42 typedef typename CompatTraits<Tr>::compatible_type compat_traits_type;
45 typedef Tr traits_type;
46 typedef typename compat_traits_type::int_type int_type;
47 typedef typename compat_traits_type::pos_type pos_type;
48 typedef typename compat_traits_type::off_type off_type;
49 typedef Alloc allocator_type;
50 typedef ::std::basic_string<Ch, Tr, Alloc> string_type;
52 typedef ::std::streamsize streamsize;
55 explicit basic_altstringbuf(std::ios_base::openmode mode
56 = std::ios_base::in | std::ios_base::out)
57 : putend_(NULL), is_allocated_(false), mode_(mode)
59 explicit basic_altstringbuf(const string_type& s,
60 ::std::ios_base::openmode mode
61 = ::std::ios_base::in | ::std::ios_base::out)
62 : putend_(NULL), is_allocated_(false), mode_(mode)
63 { dealloc(); str(s); }
64 virtual ~basic_altstringbuf()
66 using streambuf_t::pbase;
67 using streambuf_t::pptr;
68 using streambuf_t::epptr;
69 using streambuf_t::eback;
70 using streambuf_t::gptr;
71 using streambuf_t::egptr;
74 void str(const string_type& s);
78 streamsize size() const;
79 streamsize cur_size() const; // stop at current pointer
80 Ch * pend() const // the highest position reached by pptr() since creation
81 { return ((putend_ < pptr()) ? pptr() : putend_); }
82 streamsize pcount() const
83 { return static_cast<streamsize>( pptr() - pbase()) ;}
85 // copy buffer to string :
86 string_type str() const
87 { return string_type(begin(), size()); }
88 string_type cur_str() const
89 { return string_type(begin(), cur_size()); }
91 explicit basic_altstringbuf (basic_altstringbuf * s,
92 ::std::ios_base::openmode mode
93 = ::std::ios_base::in | ::std::ios_base::out)
94 : putend_(NULL), is_allocated_(false), mode_(mode)
95 { dealloc(); str(s); }
97 virtual pos_type seekoff(off_type off, ::std::ios_base::seekdir way,
98 ::std::ios_base::openmode which
99 = ::std::ios_base::in | ::std::ios_base::out);
100 virtual pos_type seekpos (pos_type pos,
101 ::std::ios_base::openmode which
102 = ::std::ios_base::in | ::std::ios_base::out);
103 virtual int_type underflow();
104 virtual int_type pbackfail(int_type meta = compat_traits_type::eof());
105 virtual int_type overflow(int_type meta = compat_traits_type::eof());
108 enum { alloc_min = 256}; // minimum size of allocations
110 Ch *putend_; // remembers (over seeks) the highest value of pptr()
112 ::std::ios_base::openmode mode_;
113 compat_allocator_type alloc_; // the allocator object
117 // --- class basic_oaltstringstream ----------------------------------------
118 template <class Ch, class Tr, class Alloc>
119 class basic_oaltstringstream
120 : private base_from_member< shared_ptr< basic_altstringbuf< Ch, Tr, Alloc> > >,
121 public ::std::basic_ostream<Ch, Tr>
124 // used as no-op deleter for (not-owner) shared_pointers
127 const T & operator()(const T & arg) { return arg; }
129 typedef ::std::basic_ostream<Ch, Tr> stream_t;
130 typedef boost::base_from_member<boost::shared_ptr<
131 basic_altstringbuf<Ch,Tr, Alloc> > >
133 typedef ::std::basic_string<Ch, Tr, Alloc> string_type;
134 typedef basic_altstringbuf<Ch, Tr, Alloc> stringbuf_t;
136 typedef Alloc allocator_type;
137 basic_oaltstringstream()
138 : pbase_type(new stringbuf_t), stream_t(rdbuf())
140 basic_oaltstringstream(::boost::shared_ptr<stringbuf_t> buf)
141 : pbase_type(buf), stream_t(rdbuf())
143 basic_oaltstringstream(stringbuf_t * buf)
144 : pbase_type(buf, No_Op() ), stream_t(rdbuf())
146 stringbuf_t * rdbuf() const
147 { return pbase_type::member.get(); }
149 { rdbuf()->clear_buffer(); }
153 { return rdbuf()->begin(); }
154 ::std::streamsize size() const
155 { return rdbuf()->size(); }
156 ::std::streamsize cur_size() const // stops at current position
157 { return rdbuf()->cur_size(); }
159 // copy buffer to string :
160 string_type str() const // [pbase, epptr[
161 { return rdbuf()->str(); }
162 string_type cur_str() const // [pbase, pptr[
163 { return rdbuf()->cur_str(); }
169 #include <boost/format/alt_sstream_impl.hpp>
171 #endif // include guard