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
;
51 typedef typename
string_type::size_type size_type
;
53 typedef ::std::streamsize streamsize
;
56 explicit basic_altstringbuf(std::ios_base::openmode mode
57 = std::ios_base::in
| std::ios_base::out
)
58 : putend_(NULL
), is_allocated_(false), mode_(mode
)
60 explicit basic_altstringbuf(const string_type
& s
,
61 ::std::ios_base::openmode mode
62 = ::std::ios_base::in
| ::std::ios_base::out
)
63 : putend_(NULL
), is_allocated_(false), mode_(mode
)
64 { dealloc(); str(s
); }
65 virtual ~basic_altstringbuf()
67 using streambuf_t::pbase
;
68 using streambuf_t::pptr
;
69 using streambuf_t::epptr
;
70 using streambuf_t::eback
;
71 using streambuf_t::gptr
;
72 using streambuf_t::egptr
;
75 void str(const string_type
& s
);
79 size_type
size() const;
80 size_type
cur_size() const; // stop at current pointer
81 Ch
* pend() const // the highest position reached by pptr() since creation
82 { return ((putend_
< pptr()) ? pptr() : putend_
); }
83 size_type
pcount() const
84 { return static_cast<size_type
>( pptr() - pbase()) ;}
86 // copy buffer to string :
87 string_type
str() const
88 { return string_type(begin(), size()); }
89 string_type
cur_str() const
90 { return string_type(begin(), cur_size()); }
92 explicit basic_altstringbuf (basic_altstringbuf
* s
,
93 ::std::ios_base::openmode mode
94 = ::std::ios_base::in
| ::std::ios_base::out
)
95 : putend_(NULL
), is_allocated_(false), mode_(mode
)
96 { dealloc(); str(s
); }
98 virtual pos_type
seekoff(off_type off
, ::std::ios_base::seekdir way
,
99 ::std::ios_base::openmode which
100 = ::std::ios_base::in
| ::std::ios_base::out
);
101 virtual pos_type
seekpos (pos_type pos
,
102 ::std::ios_base::openmode which
103 = ::std::ios_base::in
| ::std::ios_base::out
);
104 virtual int_type
underflow();
105 virtual int_type
pbackfail(int_type meta
= compat_traits_type::eof());
106 virtual int_type
overflow(int_type meta
= compat_traits_type::eof());
109 enum { alloc_min
= 256}; // minimum size of allocations
111 Ch
*putend_
; // remembers (over seeks) the highest value of pptr()
113 ::std::ios_base::openmode mode_
;
114 compat_allocator_type alloc_
; // the allocator object
118 // --- class basic_oaltstringstream ----------------------------------------
119 template <class Ch
, class Tr
, class Alloc
>
120 class basic_oaltstringstream
121 : private base_from_member
< shared_ptr
< basic_altstringbuf
< Ch
, Tr
, Alloc
> > >,
122 public ::std::basic_ostream
<Ch
, Tr
>
125 // used as no-op deleter for (not-owner) shared_pointers
128 const T
& operator()(const T
& arg
) { return arg
; }
130 typedef ::std::basic_ostream
<Ch
, Tr
> stream_t
;
131 typedef boost::base_from_member
<boost::shared_ptr
<
132 basic_altstringbuf
<Ch
,Tr
, Alloc
> > >
134 typedef ::std::basic_string
<Ch
, Tr
, Alloc
> string_type
;
135 typedef typename
string_type::size_type size_type
;
136 typedef basic_altstringbuf
<Ch
, Tr
, Alloc
> stringbuf_t
;
138 typedef Alloc allocator_type
;
139 basic_oaltstringstream()
140 : pbase_type(new stringbuf_t
), stream_t(rdbuf())
142 basic_oaltstringstream(::boost::shared_ptr
<stringbuf_t
> buf
)
143 : pbase_type(buf
), stream_t(rdbuf())
145 basic_oaltstringstream(stringbuf_t
* buf
)
146 : pbase_type(buf
, No_Op() ), stream_t(rdbuf())
148 stringbuf_t
* rdbuf() const
149 { return pbase_type::member
.get(); }
151 { rdbuf()->clear_buffer(); }
155 { return rdbuf()->begin(); }
156 size_type
size() const
157 { return rdbuf()->size(); }
158 size_type
cur_size() const // stops at current position
159 { return rdbuf()->cur_size(); }
161 // copy buffer to string :
162 string_type
str() const // [pbase, epptr[
163 { return rdbuf()->str(); }
164 string_type
cur_str() const // [pbase, pptr[
165 { return rdbuf()->cur_str(); }
166 void str(const string_type
& s
)
173 #include <boost/format/alt_sstream_impl.hpp>
175 #endif // include guard