blob: 138e1757381e4f6f5820d30f8b39fcc2e1c403a1 [file] [log] [blame]
Brian Silverman8ed424f2018-08-04 23:36:27 -07001// ----------------------------------------------------------------------------
2// alt_sstream.hpp : alternative stringstream
3// ----------------------------------------------------------------------------
4
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)
8
9// See http://www.boost.org/libs/format for library home page
10
11// ----------------------------------------------------------------------------
12
13
14
15#ifndef BOOST_SK_ALT_SSTREAM_HPP
16#define BOOST_SK_ALT_SSTREAM_HPP
17
18#include <string>
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/config.hpp>
23#include <boost/assert.hpp>
24
25namespace boost {
26 namespace io {
27
28 template<class Ch, class Tr=::std::char_traits<Ch>,
29 class Alloc=::std::allocator<Ch> >
30 class basic_altstringbuf;
31
32 template<class Ch, class Tr =::std::char_traits<Ch>,
33 class Alloc=::std::allocator<Ch> >
34 class basic_oaltstringstream;
35
36
37 template<class Ch, class Tr, class Alloc>
38 class basic_altstringbuf
39 : public ::std::basic_streambuf<Ch, Tr>
40 {
41 typedef ::std::basic_streambuf<Ch, Tr> streambuf_t;
42 typedef typename CompatAlloc<Alloc>::compatible_type compat_allocator_type;
43 typedef typename CompatTraits<Tr>::compatible_type compat_traits_type;
44 public:
45 typedef Ch char_type;
46 typedef Tr traits_type;
47 typedef typename compat_traits_type::int_type int_type;
48 typedef typename compat_traits_type::pos_type pos_type;
49 typedef typename compat_traits_type::off_type off_type;
50 typedef Alloc allocator_type;
51 typedef ::std::basic_string<Ch, Tr, Alloc> string_type;
52 typedef typename string_type::size_type size_type;
53
54 typedef ::std::streamsize streamsize;
55
56
57 explicit basic_altstringbuf(std::ios_base::openmode mode
58 = std::ios_base::in | std::ios_base::out)
59 : putend_(NULL), is_allocated_(false), mode_(mode)
60 {}
61 explicit basic_altstringbuf(const string_type& s,
62 ::std::ios_base::openmode mode
63 = ::std::ios_base::in | ::std::ios_base::out)
64 : putend_(NULL), is_allocated_(false), mode_(mode)
65 { dealloc(); str(s); }
66 virtual ~basic_altstringbuf() BOOST_NOEXCEPT_OR_NOTHROW
67 { dealloc(); }
68 using streambuf_t::pbase;
69 using streambuf_t::pptr;
70 using streambuf_t::epptr;
71 using streambuf_t::eback;
72 using streambuf_t::gptr;
73 using streambuf_t::egptr;
74
75 void clear_buffer();
76 void str(const string_type& s);
77
78 // 0-copy access :
79 Ch * begin() const;
80 size_type size() const;
81 size_type cur_size() const; // stop at current pointer
82 Ch * pend() const // the highest position reached by pptr() since creation
83 { return ((putend_ < pptr()) ? pptr() : putend_); }
84 size_type pcount() const
85 { return static_cast<size_type>( pptr() - pbase()) ;}
86
87 // copy buffer to string :
88 string_type str() const
89 { return string_type(begin(), size()); }
90 string_type cur_str() const
91 { return string_type(begin(), cur_size()); }
92 protected:
93 explicit basic_altstringbuf (basic_altstringbuf * s,
94 ::std::ios_base::openmode mode
95 = ::std::ios_base::in | ::std::ios_base::out)
96 : putend_(NULL), is_allocated_(false), mode_(mode)
97 { dealloc(); str(s); }
98
99 virtual pos_type seekoff(off_type off, ::std::ios_base::seekdir way,
100 ::std::ios_base::openmode which
101 = ::std::ios_base::in | ::std::ios_base::out);
102 virtual pos_type seekpos (pos_type pos,
103 ::std::ios_base::openmode which
104 = ::std::ios_base::in | ::std::ios_base::out);
105 virtual int_type underflow();
106 virtual int_type pbackfail(int_type meta = compat_traits_type::eof());
107 virtual int_type overflow(int_type meta = compat_traits_type::eof());
108 void dealloc();
109 private:
110 enum { alloc_min = 256}; // minimum size of allocations
111
112 Ch *putend_; // remembers (over seeks) the highest value of pptr()
113 bool is_allocated_;
114 ::std::ios_base::openmode mode_;
115 compat_allocator_type alloc_; // the allocator object
116 };
117
118
119// --- class basic_oaltstringstream ----------------------------------------
120 template <class Ch, class Tr, class Alloc>
121 class basic_oaltstringstream
122 : private base_from_member< shared_ptr< basic_altstringbuf< Ch, Tr, Alloc> > >,
123 public ::std::basic_ostream<Ch, Tr>
124 {
125 class No_Op {
126 // used as no-op deleter for (not-owner) shared_pointers
127 public:
128 template<class T>
129 const T & operator()(const T & arg) { return arg; }
130 };
131 typedef ::std::basic_ostream<Ch, Tr> stream_t;
132 typedef boost::base_from_member<boost::shared_ptr<
133 basic_altstringbuf<Ch,Tr, Alloc> > >
134 pbase_type;
135 typedef ::std::basic_string<Ch, Tr, Alloc> string_type;
136 typedef typename string_type::size_type size_type;
137 typedef basic_altstringbuf<Ch, Tr, Alloc> stringbuf_t;
138 public:
139 typedef Alloc allocator_type;
140 basic_oaltstringstream()
141 : pbase_type(new stringbuf_t), stream_t(pbase_type::member.get())
142 { }
143 basic_oaltstringstream(::boost::shared_ptr<stringbuf_t> buf)
144 : pbase_type(buf), stream_t(pbase_type::member.get())
145 { }
146 basic_oaltstringstream(stringbuf_t * buf)
147 : pbase_type(buf, No_Op() ), stream_t(pbase_type::member.get())
148 { }
149 stringbuf_t * rdbuf() const
150 { return pbase_type::member.get(); }
151 void clear_buffer()
152 { rdbuf()->clear_buffer(); }
153
154 // 0-copy access :
155 Ch * begin() const
156 { return rdbuf()->begin(); }
157 size_type size() const
158 { return rdbuf()->size(); }
159 size_type cur_size() const // stops at current position
160 { return rdbuf()->cur_size(); }
161
162 // copy buffer to string :
163 string_type str() const // [pbase, epptr[
164 { return rdbuf()->str(); }
165 string_type cur_str() const // [pbase, pptr[
166 { return rdbuf()->cur_str(); }
167 void str(const string_type& s)
168 { rdbuf()->str(s); }
169 };
170
171 } // N.S. io
172} // N.S. boost
173
174#include <boost/format/alt_sstream_impl.hpp>
175
176#endif // include guard
177