1*404b540aSrobert // String based streams -*- C++ -*-
2*404b540aSrobert
3*404b540aSrobert // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
4*404b540aSrobert // Free Software Foundation, Inc.
5*404b540aSrobert //
6*404b540aSrobert // This file is part of the GNU ISO C++ Library. This library is free
7*404b540aSrobert // software; you can redistribute it and/or modify it under the
8*404b540aSrobert // terms of the GNU General Public License as published by the
9*404b540aSrobert // Free Software Foundation; either version 2, or (at your option)
10*404b540aSrobert // any later version.
11*404b540aSrobert
12*404b540aSrobert // This library is distributed in the hope that it will be useful,
13*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of
14*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15*404b540aSrobert // GNU General Public License for more details.
16*404b540aSrobert
17*404b540aSrobert // You should have received a copy of the GNU General Public License along
18*404b540aSrobert // with this library; see the file COPYING. If not, write to the Free
19*404b540aSrobert // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20*404b540aSrobert // USA.
21*404b540aSrobert
22*404b540aSrobert // As a special exception, you may use this file as part of a free software
23*404b540aSrobert // library without restriction. Specifically, if other files instantiate
24*404b540aSrobert // templates or use macros or inline functions from this file, or you compile
25*404b540aSrobert // this file and link it with other files to produce an executable, this
26*404b540aSrobert // file does not by itself cause the resulting executable to be covered by
27*404b540aSrobert // the GNU General Public License. This exception does not however
28*404b540aSrobert // invalidate any other reasons why the executable file might be covered by
29*404b540aSrobert // the GNU General Public License.
30*404b540aSrobert
31*404b540aSrobert /** @file sstream
32*404b540aSrobert * This is a Standard C++ Library header.
33*404b540aSrobert */
34*404b540aSrobert
35*404b540aSrobert //
36*404b540aSrobert // ISO C++ 14882: 27.7 String-based streams
37*404b540aSrobert //
38*404b540aSrobert
39*404b540aSrobert #ifndef _GLIBCXX_SSTREAM
40*404b540aSrobert #define _GLIBCXX_SSTREAM 1
41*404b540aSrobert
42*404b540aSrobert #pragma GCC system_header
43*404b540aSrobert
44*404b540aSrobert #include <istream>
45*404b540aSrobert #include <ostream>
46*404b540aSrobert
_GLIBCXX_BEGIN_NAMESPACE(std)47*404b540aSrobert _GLIBCXX_BEGIN_NAMESPACE(std)
48*404b540aSrobert
49*404b540aSrobert // [27.7.1] template class basic_stringbuf
50*404b540aSrobert /**
51*404b540aSrobert * @brief The actual work of input and output (for std::string).
52*404b540aSrobert *
53*404b540aSrobert * This class associates either or both of its input and output sequences
54*404b540aSrobert * with a sequence of characters, which can be initialized from, or made
55*404b540aSrobert * available as, a @c std::basic_string. (Paraphrased from [27.7.1]/1.)
56*404b540aSrobert *
57*404b540aSrobert * For this class, open modes (of type @c ios_base::openmode) have
58*404b540aSrobert * @c in set if the input sequence can be read, and @c out set if the
59*404b540aSrobert * output sequence can be written.
60*404b540aSrobert */
61*404b540aSrobert template<typename _CharT, typename _Traits, typename _Alloc>
62*404b540aSrobert class basic_stringbuf : public basic_streambuf<_CharT, _Traits>
63*404b540aSrobert {
64*404b540aSrobert public:
65*404b540aSrobert // Types:
66*404b540aSrobert typedef _CharT char_type;
67*404b540aSrobert typedef _Traits traits_type;
68*404b540aSrobert // _GLIBCXX_RESOLVE_LIB_DEFECTS
69*404b540aSrobert // 251. basic_stringbuf missing allocator_type
70*404b540aSrobert typedef _Alloc allocator_type;
71*404b540aSrobert typedef typename traits_type::int_type int_type;
72*404b540aSrobert typedef typename traits_type::pos_type pos_type;
73*404b540aSrobert typedef typename traits_type::off_type off_type;
74*404b540aSrobert
75*404b540aSrobert typedef basic_streambuf<char_type, traits_type> __streambuf_type;
76*404b540aSrobert typedef basic_string<char_type, _Traits, _Alloc> __string_type;
77*404b540aSrobert typedef typename __string_type::size_type __size_type;
78*404b540aSrobert
79*404b540aSrobert protected:
80*404b540aSrobert /**
81*404b540aSrobert * @if maint
82*404b540aSrobert * Place to stash in || out || in | out settings for current stringbuf.
83*404b540aSrobert * @endif
84*404b540aSrobert */
85*404b540aSrobert ios_base::openmode _M_mode;
86*404b540aSrobert
87*404b540aSrobert // Data Members:
88*404b540aSrobert __string_type _M_string;
89*404b540aSrobert
90*404b540aSrobert public:
91*404b540aSrobert // Constructors:
92*404b540aSrobert /**
93*404b540aSrobert * @brief Starts with an empty string buffer.
94*404b540aSrobert * @param mode Whether the buffer can read, or write, or both.
95*404b540aSrobert *
96*404b540aSrobert * The default constructor initializes the parent class using its
97*404b540aSrobert * own default ctor.
98*404b540aSrobert */
99*404b540aSrobert explicit
100*404b540aSrobert basic_stringbuf(ios_base::openmode __mode = ios_base::in | ios_base::out)
101*404b540aSrobert : __streambuf_type(), _M_mode(__mode), _M_string()
102*404b540aSrobert { }
103*404b540aSrobert
104*404b540aSrobert /**
105*404b540aSrobert * @brief Starts with an existing string buffer.
106*404b540aSrobert * @param str A string to copy as a starting buffer.
107*404b540aSrobert * @param mode Whether the buffer can read, or write, or both.
108*404b540aSrobert *
109*404b540aSrobert * This constructor initializes the parent class using its
110*404b540aSrobert * own default ctor.
111*404b540aSrobert */
112*404b540aSrobert explicit
113*404b540aSrobert basic_stringbuf(const __string_type& __str,
114*404b540aSrobert ios_base::openmode __mode = ios_base::in | ios_base::out)
115*404b540aSrobert : __streambuf_type(), _M_mode(), _M_string(__str.data(), __str.size())
116*404b540aSrobert { _M_stringbuf_init(__mode); }
117*404b540aSrobert
118*404b540aSrobert // Get and set:
119*404b540aSrobert /**
120*404b540aSrobert * @brief Copying out the string buffer.
121*404b540aSrobert * @return A copy of one of the underlying sequences.
122*404b540aSrobert *
123*404b540aSrobert * "If the buffer is only created in input mode, the underlying
124*404b540aSrobert * character sequence is equal to the input sequence; otherwise, it
125*404b540aSrobert * is equal to the output sequence." [27.7.1.2]/1
126*404b540aSrobert */
127*404b540aSrobert __string_type
128*404b540aSrobert str() const
129*404b540aSrobert {
130*404b540aSrobert __string_type __ret;
131*404b540aSrobert if (this->pptr())
132*404b540aSrobert {
133*404b540aSrobert // The current egptr() may not be the actual string end.
134*404b540aSrobert if (this->pptr() > this->egptr())
135*404b540aSrobert __ret = __string_type(this->pbase(), this->pptr());
136*404b540aSrobert else
137*404b540aSrobert __ret = __string_type(this->pbase(), this->egptr());
138*404b540aSrobert }
139*404b540aSrobert else
140*404b540aSrobert __ret = _M_string;
141*404b540aSrobert return __ret;
142*404b540aSrobert }
143*404b540aSrobert
144*404b540aSrobert /**
145*404b540aSrobert * @brief Setting a new buffer.
146*404b540aSrobert * @param s The string to use as a new sequence.
147*404b540aSrobert *
148*404b540aSrobert * Deallocates any previous stored sequence, then copies @a s to
149*404b540aSrobert * use as a new one.
150*404b540aSrobert */
151*404b540aSrobert void
152*404b540aSrobert str(const __string_type& __s)
153*404b540aSrobert {
154*404b540aSrobert // Cannot use _M_string = __s, since v3 strings are COW.
155*404b540aSrobert _M_string.assign(__s.data(), __s.size());
156*404b540aSrobert _M_stringbuf_init(_M_mode);
157*404b540aSrobert }
158*404b540aSrobert
159*404b540aSrobert protected:
160*404b540aSrobert // Common initialization code goes here.
161*404b540aSrobert void
162*404b540aSrobert _M_stringbuf_init(ios_base::openmode __mode)
163*404b540aSrobert {
164*404b540aSrobert _M_mode = __mode;
165*404b540aSrobert __size_type __len = 0;
166*404b540aSrobert if (_M_mode & (ios_base::ate | ios_base::app))
167*404b540aSrobert __len = _M_string.size();
168*404b540aSrobert _M_sync(const_cast<char_type*>(_M_string.data()), 0, __len);
169*404b540aSrobert }
170*404b540aSrobert
171*404b540aSrobert virtual streamsize
172*404b540aSrobert showmanyc()
173*404b540aSrobert {
174*404b540aSrobert streamsize __ret = -1;
175*404b540aSrobert if (_M_mode & ios_base::in)
176*404b540aSrobert {
177*404b540aSrobert _M_update_egptr();
178*404b540aSrobert __ret = this->egptr() - this->gptr();
179*404b540aSrobert }
180*404b540aSrobert return __ret;
181*404b540aSrobert }
182*404b540aSrobert
183*404b540aSrobert virtual int_type
184*404b540aSrobert underflow();
185*404b540aSrobert
186*404b540aSrobert virtual int_type
187*404b540aSrobert pbackfail(int_type __c = traits_type::eof());
188*404b540aSrobert
189*404b540aSrobert virtual int_type
190*404b540aSrobert overflow(int_type __c = traits_type::eof());
191*404b540aSrobert
192*404b540aSrobert /**
193*404b540aSrobert * @brief Manipulates the buffer.
194*404b540aSrobert * @param s Pointer to a buffer area.
195*404b540aSrobert * @param n Size of @a s.
196*404b540aSrobert * @return @c this
197*404b540aSrobert *
198*404b540aSrobert * If no buffer has already been created, and both @a s and @a n are
199*404b540aSrobert * non-zero, then @c s is used as a buffer; see
200*404b540aSrobert * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2
201*404b540aSrobert * for more.
202*404b540aSrobert */
203*404b540aSrobert virtual __streambuf_type*
204*404b540aSrobert setbuf(char_type* __s, streamsize __n)
205*404b540aSrobert {
206*404b540aSrobert if (__s && __n >= 0)
207*404b540aSrobert {
208*404b540aSrobert // This is implementation-defined behavior, and assumes
209*404b540aSrobert // that an external char_type array of length __n exists
210*404b540aSrobert // and has been pre-allocated. If this is not the case,
211*404b540aSrobert // things will quickly blow up.
212*404b540aSrobert
213*404b540aSrobert // Step 1: Destroy the current internal array.
214*404b540aSrobert _M_string.clear();
215*404b540aSrobert
216*404b540aSrobert // Step 2: Use the external array.
217*404b540aSrobert _M_sync(__s, __n, 0);
218*404b540aSrobert }
219*404b540aSrobert return this;
220*404b540aSrobert }
221*404b540aSrobert
222*404b540aSrobert virtual pos_type
223*404b540aSrobert seekoff(off_type __off, ios_base::seekdir __way,
224*404b540aSrobert ios_base::openmode __mode = ios_base::in | ios_base::out);
225*404b540aSrobert
226*404b540aSrobert virtual pos_type
227*404b540aSrobert seekpos(pos_type __sp,
228*404b540aSrobert ios_base::openmode __mode = ios_base::in | ios_base::out);
229*404b540aSrobert
230*404b540aSrobert // Internal function for correctly updating the internal buffer
231*404b540aSrobert // for a particular _M_string, due to initialization or re-sizing
232*404b540aSrobert // of an existing _M_string.
233*404b540aSrobert void
234*404b540aSrobert _M_sync(char_type* __base, __size_type __i, __size_type __o);
235*404b540aSrobert
236*404b540aSrobert // Internal function for correctly updating egptr() to the actual
237*404b540aSrobert // string end.
238*404b540aSrobert void
239*404b540aSrobert _M_update_egptr()
240*404b540aSrobert {
241*404b540aSrobert const bool __testin = _M_mode & ios_base::in;
242*404b540aSrobert if (this->pptr() && this->pptr() > this->egptr())
243*404b540aSrobert if (__testin)
244*404b540aSrobert this->setg(this->eback(), this->gptr(), this->pptr());
245*404b540aSrobert else
246*404b540aSrobert this->setg(this->pptr(), this->pptr(), this->pptr());
247*404b540aSrobert }
248*404b540aSrobert };
249*404b540aSrobert
250*404b540aSrobert
251*404b540aSrobert // [27.7.2] Template class basic_istringstream
252*404b540aSrobert /**
253*404b540aSrobert * @brief Controlling input for std::string.
254*404b540aSrobert *
255*404b540aSrobert * This class supports reading from objects of type std::basic_string,
256*404b540aSrobert * using the inherited functions from std::basic_istream. To control
257*404b540aSrobert * the associated sequence, an instance of std::basic_stringbuf is used,
258*404b540aSrobert * which this page refers to as @c sb.
259*404b540aSrobert */
260*404b540aSrobert template<typename _CharT, typename _Traits, typename _Alloc>
261*404b540aSrobert class basic_istringstream : public basic_istream<_CharT, _Traits>
262*404b540aSrobert {
263*404b540aSrobert public:
264*404b540aSrobert // Types:
265*404b540aSrobert typedef _CharT char_type;
266*404b540aSrobert typedef _Traits traits_type;
267*404b540aSrobert // _GLIBCXX_RESOLVE_LIB_DEFECTS
268*404b540aSrobert // 251. basic_stringbuf missing allocator_type
269*404b540aSrobert typedef _Alloc allocator_type;
270*404b540aSrobert typedef typename traits_type::int_type int_type;
271*404b540aSrobert typedef typename traits_type::pos_type pos_type;
272*404b540aSrobert typedef typename traits_type::off_type off_type;
273*404b540aSrobert
274*404b540aSrobert // Non-standard types:
275*404b540aSrobert typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
276*404b540aSrobert typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
277*404b540aSrobert typedef basic_istream<char_type, traits_type> __istream_type;
278*404b540aSrobert
279*404b540aSrobert private:
280*404b540aSrobert __stringbuf_type _M_stringbuf;
281*404b540aSrobert
282*404b540aSrobert public:
283*404b540aSrobert // Constructors:
284*404b540aSrobert /**
285*404b540aSrobert * @brief Default constructor starts with an empty string buffer.
286*404b540aSrobert * @param mode Whether the buffer can read, or write, or both.
287*404b540aSrobert *
288*404b540aSrobert * @c ios_base::in is automatically included in @a mode.
289*404b540aSrobert *
290*404b540aSrobert * Initializes @c sb using @c mode|in, and passes @c &sb to the base
291*404b540aSrobert * class initializer. Does not allocate any buffer.
292*404b540aSrobert *
293*404b540aSrobert * @if maint
294*404b540aSrobert * That's a lie. We initialize the base class with NULL, because the
295*404b540aSrobert * string class does its own memory management.
296*404b540aSrobert * @endif
297*404b540aSrobert */
298*404b540aSrobert explicit
299*404b540aSrobert basic_istringstream(ios_base::openmode __mode = ios_base::in)
__istream_type()300*404b540aSrobert : __istream_type(), _M_stringbuf(__mode | ios_base::in)
301*404b540aSrobert { this->init(&_M_stringbuf); }
302*404b540aSrobert
303*404b540aSrobert /**
304*404b540aSrobert * @brief Starts with an existing string buffer.
305*404b540aSrobert * @param str A string to copy as a starting buffer.
306*404b540aSrobert * @param mode Whether the buffer can read, or write, or both.
307*404b540aSrobert *
308*404b540aSrobert * @c ios_base::in is automatically included in @a mode.
309*404b540aSrobert *
310*404b540aSrobert * Initializes @c sb using @a str and @c mode|in, and passes @c &sb
311*404b540aSrobert * to the base class initializer.
312*404b540aSrobert *
313*404b540aSrobert * @if maint
314*404b540aSrobert * That's a lie. We initialize the base class with NULL, because the
315*404b540aSrobert * string class does its own memory management.
316*404b540aSrobert * @endif
317*404b540aSrobert */
318*404b540aSrobert explicit
319*404b540aSrobert basic_istringstream(const __string_type& __str,
320*404b540aSrobert ios_base::openmode __mode = ios_base::in)
__istream_type()321*404b540aSrobert : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in)
322*404b540aSrobert { this->init(&_M_stringbuf); }
323*404b540aSrobert
324*404b540aSrobert /**
325*404b540aSrobert * @brief The destructor does nothing.
326*404b540aSrobert *
327*404b540aSrobert * The buffer is deallocated by the stringbuf object, not the
328*404b540aSrobert * formatting stream.
329*404b540aSrobert */
~basic_istringstream()330*404b540aSrobert ~basic_istringstream()
331*404b540aSrobert { }
332*404b540aSrobert
333*404b540aSrobert // Members:
334*404b540aSrobert /**
335*404b540aSrobert * @brief Accessing the underlying buffer.
336*404b540aSrobert * @return The current basic_stringbuf buffer.
337*404b540aSrobert *
338*404b540aSrobert * This hides both signatures of std::basic_ios::rdbuf().
339*404b540aSrobert */
340*404b540aSrobert __stringbuf_type*
rdbuf()341*404b540aSrobert rdbuf() const
342*404b540aSrobert { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
343*404b540aSrobert
344*404b540aSrobert /**
345*404b540aSrobert * @brief Copying out the string buffer.
346*404b540aSrobert * @return @c rdbuf()->str()
347*404b540aSrobert */
348*404b540aSrobert __string_type
str()349*404b540aSrobert str() const
350*404b540aSrobert { return _M_stringbuf.str(); }
351*404b540aSrobert
352*404b540aSrobert /**
353*404b540aSrobert * @brief Setting a new buffer.
354*404b540aSrobert * @param s The string to use as a new sequence.
355*404b540aSrobert *
356*404b540aSrobert * Calls @c rdbuf()->str(s).
357*404b540aSrobert */
358*404b540aSrobert void
str(const __string_type & __s)359*404b540aSrobert str(const __string_type& __s)
360*404b540aSrobert { _M_stringbuf.str(__s); }
361*404b540aSrobert };
362*404b540aSrobert
363*404b540aSrobert
364*404b540aSrobert // [27.7.3] Template class basic_ostringstream
365*404b540aSrobert /**
366*404b540aSrobert * @brief Controlling output for std::string.
367*404b540aSrobert *
368*404b540aSrobert * This class supports writing to objects of type std::basic_string,
369*404b540aSrobert * using the inherited functions from std::basic_ostream. To control
370*404b540aSrobert * the associated sequence, an instance of std::basic_stringbuf is used,
371*404b540aSrobert * which this page refers to as @c sb.
372*404b540aSrobert */
373*404b540aSrobert template <typename _CharT, typename _Traits, typename _Alloc>
374*404b540aSrobert class basic_ostringstream : public basic_ostream<_CharT, _Traits>
375*404b540aSrobert {
376*404b540aSrobert public:
377*404b540aSrobert // Types:
378*404b540aSrobert typedef _CharT char_type;
379*404b540aSrobert typedef _Traits traits_type;
380*404b540aSrobert // _GLIBCXX_RESOLVE_LIB_DEFECTS
381*404b540aSrobert // 251. basic_stringbuf missing allocator_type
382*404b540aSrobert typedef _Alloc allocator_type;
383*404b540aSrobert typedef typename traits_type::int_type int_type;
384*404b540aSrobert typedef typename traits_type::pos_type pos_type;
385*404b540aSrobert typedef typename traits_type::off_type off_type;
386*404b540aSrobert
387*404b540aSrobert // Non-standard types:
388*404b540aSrobert typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
389*404b540aSrobert typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
390*404b540aSrobert typedef basic_ostream<char_type, traits_type> __ostream_type;
391*404b540aSrobert
392*404b540aSrobert private:
393*404b540aSrobert __stringbuf_type _M_stringbuf;
394*404b540aSrobert
395*404b540aSrobert public:
396*404b540aSrobert // Constructors/destructor:
397*404b540aSrobert /**
398*404b540aSrobert * @brief Default constructor starts with an empty string buffer.
399*404b540aSrobert * @param mode Whether the buffer can read, or write, or both.
400*404b540aSrobert *
401*404b540aSrobert * @c ios_base::out is automatically included in @a mode.
402*404b540aSrobert *
403*404b540aSrobert * Initializes @c sb using @c mode|out, and passes @c &sb to the base
404*404b540aSrobert * class initializer. Does not allocate any buffer.
405*404b540aSrobert *
406*404b540aSrobert * @if maint
407*404b540aSrobert * That's a lie. We initialize the base class with NULL, because the
408*404b540aSrobert * string class does its own memory management.
409*404b540aSrobert * @endif
410*404b540aSrobert */
411*404b540aSrobert explicit
412*404b540aSrobert basic_ostringstream(ios_base::openmode __mode = ios_base::out)
__ostream_type()413*404b540aSrobert : __ostream_type(), _M_stringbuf(__mode | ios_base::out)
414*404b540aSrobert { this->init(&_M_stringbuf); }
415*404b540aSrobert
416*404b540aSrobert /**
417*404b540aSrobert * @brief Starts with an existing string buffer.
418*404b540aSrobert * @param str A string to copy as a starting buffer.
419*404b540aSrobert * @param mode Whether the buffer can read, or write, or both.
420*404b540aSrobert *
421*404b540aSrobert * @c ios_base::out is automatically included in @a mode.
422*404b540aSrobert *
423*404b540aSrobert * Initializes @c sb using @a str and @c mode|out, and passes @c &sb
424*404b540aSrobert * to the base class initializer.
425*404b540aSrobert *
426*404b540aSrobert * @if maint
427*404b540aSrobert * That's a lie. We initialize the base class with NULL, because the
428*404b540aSrobert * string class does its own memory management.
429*404b540aSrobert * @endif
430*404b540aSrobert */
431*404b540aSrobert explicit
432*404b540aSrobert basic_ostringstream(const __string_type& __str,
433*404b540aSrobert ios_base::openmode __mode = ios_base::out)
__ostream_type()434*404b540aSrobert : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out)
435*404b540aSrobert { this->init(&_M_stringbuf); }
436*404b540aSrobert
437*404b540aSrobert /**
438*404b540aSrobert * @brief The destructor does nothing.
439*404b540aSrobert *
440*404b540aSrobert * The buffer is deallocated by the stringbuf object, not the
441*404b540aSrobert * formatting stream.
442*404b540aSrobert */
~basic_ostringstream()443*404b540aSrobert ~basic_ostringstream()
444*404b540aSrobert { }
445*404b540aSrobert
446*404b540aSrobert // Members:
447*404b540aSrobert /**
448*404b540aSrobert * @brief Accessing the underlying buffer.
449*404b540aSrobert * @return The current basic_stringbuf buffer.
450*404b540aSrobert *
451*404b540aSrobert * This hides both signatures of std::basic_ios::rdbuf().
452*404b540aSrobert */
453*404b540aSrobert __stringbuf_type*
rdbuf()454*404b540aSrobert rdbuf() const
455*404b540aSrobert { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
456*404b540aSrobert
457*404b540aSrobert /**
458*404b540aSrobert * @brief Copying out the string buffer.
459*404b540aSrobert * @return @c rdbuf()->str()
460*404b540aSrobert */
461*404b540aSrobert __string_type
str()462*404b540aSrobert str() const
463*404b540aSrobert { return _M_stringbuf.str(); }
464*404b540aSrobert
465*404b540aSrobert /**
466*404b540aSrobert * @brief Setting a new buffer.
467*404b540aSrobert * @param s The string to use as a new sequence.
468*404b540aSrobert *
469*404b540aSrobert * Calls @c rdbuf()->str(s).
470*404b540aSrobert */
471*404b540aSrobert void
str(const __string_type & __s)472*404b540aSrobert str(const __string_type& __s)
473*404b540aSrobert { _M_stringbuf.str(__s); }
474*404b540aSrobert };
475*404b540aSrobert
476*404b540aSrobert
477*404b540aSrobert // [27.7.4] Template class basic_stringstream
478*404b540aSrobert /**
479*404b540aSrobert * @brief Controlling input and output for std::string.
480*404b540aSrobert *
481*404b540aSrobert * This class supports reading from and writing to objects of type
482*404b540aSrobert * std::basic_string, using the inherited functions from
483*404b540aSrobert * std::basic_iostream. To control the associated sequence, an instance
484*404b540aSrobert * of std::basic_stringbuf is used, which this page refers to as @c sb.
485*404b540aSrobert */
486*404b540aSrobert template <typename _CharT, typename _Traits, typename _Alloc>
487*404b540aSrobert class basic_stringstream : public basic_iostream<_CharT, _Traits>
488*404b540aSrobert {
489*404b540aSrobert public:
490*404b540aSrobert // Types:
491*404b540aSrobert typedef _CharT char_type;
492*404b540aSrobert typedef _Traits traits_type;
493*404b540aSrobert // _GLIBCXX_RESOLVE_LIB_DEFECTS
494*404b540aSrobert // 251. basic_stringbuf missing allocator_type
495*404b540aSrobert typedef _Alloc allocator_type;
496*404b540aSrobert typedef typename traits_type::int_type int_type;
497*404b540aSrobert typedef typename traits_type::pos_type pos_type;
498*404b540aSrobert typedef typename traits_type::off_type off_type;
499*404b540aSrobert
500*404b540aSrobert // Non-standard Types:
501*404b540aSrobert typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
502*404b540aSrobert typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
503*404b540aSrobert typedef basic_iostream<char_type, traits_type> __iostream_type;
504*404b540aSrobert
505*404b540aSrobert private:
506*404b540aSrobert __stringbuf_type _M_stringbuf;
507*404b540aSrobert
508*404b540aSrobert public:
509*404b540aSrobert // Constructors/destructors
510*404b540aSrobert /**
511*404b540aSrobert * @brief Default constructor starts with an empty string buffer.
512*404b540aSrobert * @param mode Whether the buffer can read, or write, or both.
513*404b540aSrobert *
514*404b540aSrobert * Initializes @c sb using @c mode, and passes @c &sb to the base
515*404b540aSrobert * class initializer. Does not allocate any buffer.
516*404b540aSrobert *
517*404b540aSrobert * @if maint
518*404b540aSrobert * That's a lie. We initialize the base class with NULL, because the
519*404b540aSrobert * string class does its own memory management.
520*404b540aSrobert * @endif
521*404b540aSrobert */
522*404b540aSrobert explicit
523*404b540aSrobert basic_stringstream(ios_base::openmode __m = ios_base::out | ios_base::in)
__iostream_type()524*404b540aSrobert : __iostream_type(), _M_stringbuf(__m)
525*404b540aSrobert { this->init(&_M_stringbuf); }
526*404b540aSrobert
527*404b540aSrobert /**
528*404b540aSrobert * @brief Starts with an existing string buffer.
529*404b540aSrobert * @param str A string to copy as a starting buffer.
530*404b540aSrobert * @param mode Whether the buffer can read, or write, or both.
531*404b540aSrobert *
532*404b540aSrobert * Initializes @c sb using @a str and @c mode, and passes @c &sb
533*404b540aSrobert * to the base class initializer.
534*404b540aSrobert *
535*404b540aSrobert * @if maint
536*404b540aSrobert * That's a lie. We initialize the base class with NULL, because the
537*404b540aSrobert * string class does its own memory management.
538*404b540aSrobert * @endif
539*404b540aSrobert */
540*404b540aSrobert explicit
541*404b540aSrobert basic_stringstream(const __string_type& __str,
542*404b540aSrobert ios_base::openmode __m = ios_base::out | ios_base::in)
__iostream_type()543*404b540aSrobert : __iostream_type(), _M_stringbuf(__str, __m)
544*404b540aSrobert { this->init(&_M_stringbuf); }
545*404b540aSrobert
546*404b540aSrobert /**
547*404b540aSrobert * @brief The destructor does nothing.
548*404b540aSrobert *
549*404b540aSrobert * The buffer is deallocated by the stringbuf object, not the
550*404b540aSrobert * formatting stream.
551*404b540aSrobert */
~basic_stringstream()552*404b540aSrobert ~basic_stringstream()
553*404b540aSrobert { }
554*404b540aSrobert
555*404b540aSrobert // Members:
556*404b540aSrobert /**
557*404b540aSrobert * @brief Accessing the underlying buffer.
558*404b540aSrobert * @return The current basic_stringbuf buffer.
559*404b540aSrobert *
560*404b540aSrobert * This hides both signatures of std::basic_ios::rdbuf().
561*404b540aSrobert */
562*404b540aSrobert __stringbuf_type*
rdbuf()563*404b540aSrobert rdbuf() const
564*404b540aSrobert { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
565*404b540aSrobert
566*404b540aSrobert /**
567*404b540aSrobert * @brief Copying out the string buffer.
568*404b540aSrobert * @return @c rdbuf()->str()
569*404b540aSrobert */
570*404b540aSrobert __string_type
str()571*404b540aSrobert str() const
572*404b540aSrobert { return _M_stringbuf.str(); }
573*404b540aSrobert
574*404b540aSrobert /**
575*404b540aSrobert * @brief Setting a new buffer.
576*404b540aSrobert * @param s The string to use as a new sequence.
577*404b540aSrobert *
578*404b540aSrobert * Calls @c rdbuf()->str(s).
579*404b540aSrobert */
580*404b540aSrobert void
str(const __string_type & __s)581*404b540aSrobert str(const __string_type& __s)
582*404b540aSrobert { _M_stringbuf.str(__s); }
583*404b540aSrobert };
584*404b540aSrobert
585*404b540aSrobert _GLIBCXX_END_NAMESPACE
586*404b540aSrobert
587*404b540aSrobert #ifndef _GLIBCXX_EXPORT_TEMPLATE
588*404b540aSrobert # include <bits/sstream.tcc>
589*404b540aSrobert #endif
590*404b540aSrobert
591*404b540aSrobert #endif /* _GLIBCXX_SSTREAM */
592