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