197403Sobrien// String based streams -*- C++ -*-
297403Sobrien
3169691Skan// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
4132720Skan// Free Software Foundation, Inc.
597403Sobrien//
697403Sobrien// This file is part of the GNU ISO C++ Library.  This library is free
797403Sobrien// software; you can redistribute it and/or modify it under the
897403Sobrien// terms of the GNU General Public License as published by the
997403Sobrien// Free Software Foundation; either version 2, or (at your option)
1097403Sobrien// any later version.
1197403Sobrien
1297403Sobrien// This library is distributed in the hope that it will be useful,
1397403Sobrien// but WITHOUT ANY WARRANTY; without even the implied warranty of
1497403Sobrien// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1597403Sobrien// GNU General Public License for more details.
1697403Sobrien
1797403Sobrien// You should have received a copy of the GNU General Public License along
1897403Sobrien// with this library; see the file COPYING.  If not, write to the Free
19169691Skan// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
2097403Sobrien// USA.
2197403Sobrien
2297403Sobrien// As a special exception, you may use this file as part of a free software
2397403Sobrien// library without restriction.  Specifically, if other files instantiate
2497403Sobrien// templates or use macros or inline functions from this file, or you compile
2597403Sobrien// this file and link it with other files to produce an executable, this
2697403Sobrien// file does not by itself cause the resulting executable to be covered by
2797403Sobrien// the GNU General Public License.  This exception does not however
2897403Sobrien// invalidate any other reasons why the executable file might be covered by
2997403Sobrien// the GNU General Public License.
3097403Sobrien
31169691Skan/** @file sstream
32169691Skan *  This is a Standard C++ Library header.
33169691Skan */
34169691Skan
3597403Sobrien//
3697403Sobrien// ISO C++ 14882: 27.7  String-based streams
3797403Sobrien//
3897403Sobrien
39132720Skan#ifndef _GLIBCXX_SSTREAM
40132720Skan#define _GLIBCXX_SSTREAM 1
4197403Sobrien
4297403Sobrien#pragma GCC system_header
4397403Sobrien
4497403Sobrien#include <istream>
4597403Sobrien#include <ostream>
4697403Sobrien
47169691Skan_GLIBCXX_BEGIN_NAMESPACE(std)
48169691Skan
49117397Skan  // [27.7.1] template class basic_stringbuf
50117397Skan  /**
51117397Skan   *  @brief  The actual work of input and output (for std::string).
52117397Skan   *
53117397Skan   *  This class associates either or both of its input and output sequences
54117397Skan   *  with a sequence of characters, which can be initialized from, or made
55117397Skan   *  available as, a @c std::basic_string.  (Paraphrased from [27.7.1]/1.)
56117397Skan   *
57117397Skan   *  For this class, open modes (of type @c ios_base::openmode) have
58117397Skan   *  @c in set if the input sequence can be read, and @c out set if the
59117397Skan   *  output sequence can be written.
60117397Skan  */
6197403Sobrien  template<typename _CharT, typename _Traits, typename _Alloc>
6297403Sobrien    class basic_stringbuf : public basic_streambuf<_CharT, _Traits>
6397403Sobrien    {
6497403Sobrien    public:
6597403Sobrien      // Types:
6697403Sobrien      typedef _CharT 					char_type;
6797403Sobrien      typedef _Traits 					traits_type;
68132720Skan      // _GLIBCXX_RESOLVE_LIB_DEFECTS
69132720Skan      // 251. basic_stringbuf missing allocator_type
7097403Sobrien      typedef _Alloc				       	allocator_type;
7197403Sobrien      typedef typename traits_type::int_type 		int_type;
7297403Sobrien      typedef typename traits_type::pos_type 		pos_type;
7397403Sobrien      typedef typename traits_type::off_type 		off_type;
7497403Sobrien
7597403Sobrien      typedef basic_streambuf<char_type, traits_type>  	__streambuf_type;
7697403Sobrien      typedef basic_string<char_type, _Traits, _Alloc> 	__string_type;
7797403Sobrien      typedef typename __string_type::size_type		__size_type;
7897403Sobrien
7997403Sobrien    protected:
80132720Skan      /**
81132720Skan       *  @if maint
82132720Skan       *  Place to stash in || out || in | out settings for current stringbuf.
83132720Skan       *  @endif
84132720Skan      */
85132720Skan      ios_base::openmode 	_M_mode;
86132720Skan
8797403Sobrien      // Data Members:
8897403Sobrien      __string_type 		_M_string;
8997403Sobrien
9097403Sobrien    public:
9197403Sobrien      // Constructors:
92117397Skan      /**
93117397Skan       *  @brief  Starts with an empty string buffer.
94117397Skan       *  @param  mode  Whether the buffer can read, or write, or both.
95117397Skan       *
96117397Skan       *  The default constructor initializes the parent class using its
97117397Skan       *  own default ctor.
98117397Skan      */
9997403Sobrien      explicit
10097403Sobrien      basic_stringbuf(ios_base::openmode __mode = ios_base::in | ios_base::out)
101146897Skan      : __streambuf_type(), _M_mode(__mode), _M_string()
102146897Skan      { }
10397403Sobrien
104117397Skan      /**
105117397Skan       *  @brief  Starts with an existing string buffer.
106117397Skan       *  @param  str  A string to copy as a starting buffer.
107117397Skan       *  @param  mode  Whether the buffer can read, or write, or both.
108117397Skan       *
109117397Skan       *  This constructor initializes the parent class using its
110117397Skan       *  own default ctor.
111117397Skan      */
11297403Sobrien      explicit
11397403Sobrien      basic_stringbuf(const __string_type& __str,
11497403Sobrien		      ios_base::openmode __mode = ios_base::in | ios_base::out)
115132720Skan      : __streambuf_type(), _M_mode(), _M_string(__str.data(), __str.size())
11697403Sobrien      { _M_stringbuf_init(__mode); }
11797403Sobrien
11897403Sobrien      // Get and set:
119117397Skan      /**
120117397Skan       *  @brief  Copying out the string buffer.
121117397Skan       *  @return  A copy of one of the underlying sequences.
122117397Skan       *
123117397Skan       *  "If the buffer is only created in input mode, the underlying
124117397Skan       *  character sequence is equal to the input sequence; otherwise, it
125117397Skan       *  is equal to the output sequence." [27.7.1.2]/1
126117397Skan      */
12797403Sobrien      __string_type
12897403Sobrien      str() const
12997403Sobrien      {
130169691Skan	__string_type __ret;
131146897Skan	if (this->pptr())
13297403Sobrien	  {
133132720Skan	    // The current egptr() may not be the actual string end.
134132720Skan	    if (this->pptr() > this->egptr())
135169691Skan	      __ret = __string_type(this->pbase(), this->pptr());
136132720Skan	    else
137169691Skan 	      __ret = __string_type(this->pbase(), this->egptr());
13897403Sobrien	  }
13997403Sobrien	else
140169691Skan	  __ret = _M_string;
141169691Skan	return __ret;
14297403Sobrien      }
14397403Sobrien
144117397Skan      /**
145117397Skan       *  @brief  Setting a new buffer.
146117397Skan       *  @param  s  The string to use as a new sequence.
147117397Skan       *
148117397Skan       *  Deallocates any previous stored sequence, then copies @a s to
149117397Skan       *  use as a new one.
150117397Skan      */
15197403Sobrien      void
15297403Sobrien      str(const __string_type& __s)
15397403Sobrien      {
154107606Sobrien	// Cannot use _M_string = __s, since v3 strings are COW.
155107606Sobrien	_M_string.assign(__s.data(), __s.size());
156169691Skan	_M_stringbuf_init(_M_mode);
15797403Sobrien      }
15897403Sobrien
15997403Sobrien    protected:
160146897Skan      // Common initialization code goes here.
16197403Sobrien      void
16297403Sobrien      _M_stringbuf_init(ios_base::openmode __mode)
16397403Sobrien      {
164169691Skan	_M_mode = __mode;
165132720Skan	__size_type __len = 0;
166169691Skan	if (_M_mode & (ios_base::ate | ios_base::app))
167132720Skan	  __len = _M_string.size();
168132720Skan	_M_sync(const_cast<char_type*>(_M_string.data()), 0, __len);
16997403Sobrien      }
17097403Sobrien
171169691Skan      virtual streamsize
172169691Skan      showmanyc()
173169691Skan      {
174169691Skan	streamsize __ret = -1;
175169691Skan	if (_M_mode & ios_base::in)
176169691Skan	  {
177169691Skan	    _M_update_egptr();
178169691Skan	    __ret = this->egptr() - this->gptr();
179169691Skan	  }
180169691Skan	return __ret;
181169691Skan      }
182169691Skan
18397403Sobrien      virtual int_type
184132720Skan      underflow();
18597403Sobrien
18697403Sobrien      virtual int_type
18797403Sobrien      pbackfail(int_type __c = traits_type::eof());
18897403Sobrien
18997403Sobrien      virtual int_type
19097403Sobrien      overflow(int_type __c = traits_type::eof());
19197403Sobrien
192117397Skan      /**
193117397Skan       *  @brief  Manipulates the buffer.
194117397Skan       *  @param  s  Pointer to a buffer area.
195117397Skan       *  @param  n  Size of @a s.
196117397Skan       *  @return  @c this
197117397Skan       *
198117397Skan       *  If no buffer has already been created, and both @a s and @a n are
199117397Skan       *  non-zero, then @c s is used as a buffer; see
200117397Skan       *  http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2
201117397Skan       *  for more.
202117397Skan      */
20397403Sobrien      virtual __streambuf_type*
20497403Sobrien      setbuf(char_type* __s, streamsize __n)
20597403Sobrien      {
206132720Skan	if (__s && __n >= 0)
20797403Sobrien	  {
208132720Skan	    // This is implementation-defined behavior, and assumes
209132720Skan	    // that an external char_type array of length __n exists
210132720Skan	    // and has been pre-allocated. If this is not the case,
211132720Skan	    // things will quickly blow up.
212132720Skan
213132720Skan	    // Step 1: Destroy the current internal array.
214169691Skan	    _M_string.clear();
215132720Skan
216132720Skan	    // Step 2: Use the external array.
217169691Skan	    _M_sync(__s, __n, 0);
21897403Sobrien	  }
21997403Sobrien	return this;
22097403Sobrien      }
22197403Sobrien
22297403Sobrien      virtual pos_type
22397403Sobrien      seekoff(off_type __off, ios_base::seekdir __way,
22497403Sobrien	      ios_base::openmode __mode = ios_base::in | ios_base::out);
22597403Sobrien
22697403Sobrien      virtual pos_type
22797403Sobrien      seekpos(pos_type __sp,
22897403Sobrien	      ios_base::openmode __mode = ios_base::in | ios_base::out);
22997403Sobrien
23097403Sobrien      // Internal function for correctly updating the internal buffer
231169691Skan      // for a particular _M_string, due to initialization or re-sizing
232169691Skan      // of an existing _M_string.
233132720Skan      void
234169691Skan      _M_sync(char_type* __base, __size_type __i, __size_type __o);
23597403Sobrien
236132720Skan      // Internal function for correctly updating egptr() to the actual
237132720Skan      // string end.
238132720Skan      void
239132720Skan      _M_update_egptr()
240132720Skan      {
241169691Skan	const bool __testin = _M_mode & ios_base::in;
242146897Skan	if (this->pptr() && this->pptr() > this->egptr())
243241957Sdim	  {
244241957Sdim	    if (__testin)
245241957Sdim	      this->setg(this->eback(), this->gptr(), this->pptr());
246241957Sdim	    else
247241957Sdim	      this->setg(this->pptr(), this->pptr(), this->pptr());
248241957Sdim	  }
249132720Skan      }
25097403Sobrien    };
25197403Sobrien
25297403Sobrien
253117397Skan  // [27.7.2] Template class basic_istringstream
254117397Skan  /**
255117397Skan   *  @brief  Controlling input for std::string.
256117397Skan   *
257117397Skan   *  This class supports reading from objects of type std::basic_string,
258117397Skan   *  using the inherited functions from std::basic_istream.  To control
259117397Skan   *  the associated sequence, an instance of std::basic_stringbuf is used,
260117397Skan   *  which this page refers to as @c sb.
261117397Skan  */
26297403Sobrien  template<typename _CharT, typename _Traits, typename _Alloc>
26397403Sobrien    class basic_istringstream : public basic_istream<_CharT, _Traits>
26497403Sobrien    {
26597403Sobrien    public:
26697403Sobrien      // Types:
26797403Sobrien      typedef _CharT 					char_type;
26897403Sobrien      typedef _Traits 					traits_type;
269132720Skan      // _GLIBCXX_RESOLVE_LIB_DEFECTS
270132720Skan      // 251. basic_stringbuf missing allocator_type
27197403Sobrien      typedef _Alloc				       	allocator_type;
27297403Sobrien      typedef typename traits_type::int_type 		int_type;
27397403Sobrien      typedef typename traits_type::pos_type 		pos_type;
27497403Sobrien      typedef typename traits_type::off_type 		off_type;
27597403Sobrien
27697403Sobrien      // Non-standard types:
27797403Sobrien      typedef basic_string<_CharT, _Traits, _Alloc> 	__string_type;
27897403Sobrien      typedef basic_stringbuf<_CharT, _Traits, _Alloc> 	__stringbuf_type;
27997403Sobrien      typedef basic_istream<char_type, traits_type>	__istream_type;
28097403Sobrien
28197403Sobrien    private:
28297403Sobrien      __stringbuf_type	_M_stringbuf;
28397403Sobrien
28497403Sobrien    public:
28597403Sobrien      // Constructors:
286117397Skan      /**
287117397Skan       *  @brief  Default constructor starts with an empty string buffer.
288117397Skan       *  @param  mode  Whether the buffer can read, or write, or both.
289117397Skan       *
290117397Skan       *  @c ios_base::in is automatically included in @a mode.
291117397Skan       *
292117397Skan       *  Initializes @c sb using @c mode|in, and passes @c &sb to the base
293117397Skan       *  class initializer.  Does not allocate any buffer.
294117397Skan       *
295117397Skan       *  @if maint
296117397Skan       *  That's a lie.  We initialize the base class with NULL, because the
297117397Skan       *  string class does its own memory management.
298117397Skan       *  @endif
299117397Skan      */
30097403Sobrien      explicit
30197403Sobrien      basic_istringstream(ios_base::openmode __mode = ios_base::in)
302132720Skan      : __istream_type(), _M_stringbuf(__mode | ios_base::in)
30397403Sobrien      { this->init(&_M_stringbuf); }
30497403Sobrien
305117397Skan      /**
306117397Skan       *  @brief  Starts with an existing string buffer.
307117397Skan       *  @param  str  A string to copy as a starting buffer.
308117397Skan       *  @param  mode  Whether the buffer can read, or write, or both.
309117397Skan       *
310117397Skan       *  @c ios_base::in is automatically included in @a mode.
311117397Skan       *
312117397Skan       *  Initializes @c sb using @a str and @c mode|in, and passes @c &sb
313117397Skan       *  to the base class initializer.
314117397Skan       *
315117397Skan       *  @if maint
316117397Skan       *  That's a lie.  We initialize the base class with NULL, because the
317117397Skan       *  string class does its own memory management.
318117397Skan       *  @endif
319117397Skan      */
32097403Sobrien      explicit
32197403Sobrien      basic_istringstream(const __string_type& __str,
32297403Sobrien			  ios_base::openmode __mode = ios_base::in)
323132720Skan      : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in)
32497403Sobrien      { this->init(&_M_stringbuf); }
32597403Sobrien
326117397Skan      /**
327117397Skan       *  @brief  The destructor does nothing.
328117397Skan       *
329117397Skan       *  The buffer is deallocated by the stringbuf object, not the
330117397Skan       *  formatting stream.
331117397Skan      */
33297403Sobrien      ~basic_istringstream()
33397403Sobrien      { }
33497403Sobrien
33597403Sobrien      // Members:
336117397Skan      /**
337117397Skan       *  @brief  Accessing the underlying buffer.
338117397Skan       *  @return  The current basic_stringbuf buffer.
339117397Skan       *
340117397Skan       *  This hides both signatures of std::basic_ios::rdbuf().
341117397Skan      */
34297403Sobrien      __stringbuf_type*
34397403Sobrien      rdbuf() const
34497403Sobrien      { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
34597403Sobrien
346117397Skan      /**
347117397Skan       *  @brief  Copying out the string buffer.
348117397Skan       *  @return  @c rdbuf()->str()
349117397Skan      */
35097403Sobrien      __string_type
35197403Sobrien      str() const
35297403Sobrien      { return _M_stringbuf.str(); }
35397403Sobrien
354117397Skan      /**
355117397Skan       *  @brief  Setting a new buffer.
356117397Skan       *  @param  s  The string to use as a new sequence.
357117397Skan       *
358117397Skan       *  Calls @c rdbuf()->str(s).
359117397Skan      */
36097403Sobrien      void
36197403Sobrien      str(const __string_type& __s)
36297403Sobrien      { _M_stringbuf.str(__s); }
36397403Sobrien    };
36497403Sobrien
36597403Sobrien
366117397Skan  // [27.7.3] Template class basic_ostringstream
367117397Skan  /**
368117397Skan   *  @brief  Controlling output for std::string.
369117397Skan   *
370117397Skan   *  This class supports writing to objects of type std::basic_string,
371117397Skan   *  using the inherited functions from std::basic_ostream.  To control
372117397Skan   *  the associated sequence, an instance of std::basic_stringbuf is used,
373117397Skan   *  which this page refers to as @c sb.
374117397Skan  */
37597403Sobrien  template <typename _CharT, typename _Traits, typename _Alloc>
37697403Sobrien    class basic_ostringstream : public basic_ostream<_CharT, _Traits>
37797403Sobrien    {
37897403Sobrien    public:
37997403Sobrien      // Types:
38097403Sobrien      typedef _CharT 					char_type;
38197403Sobrien      typedef _Traits 					traits_type;
382132720Skan      // _GLIBCXX_RESOLVE_LIB_DEFECTS
383132720Skan      // 251. basic_stringbuf missing allocator_type
38497403Sobrien      typedef _Alloc				       	allocator_type;
38597403Sobrien      typedef typename traits_type::int_type 		int_type;
38697403Sobrien      typedef typename traits_type::pos_type 		pos_type;
38797403Sobrien      typedef typename traits_type::off_type 		off_type;
38897403Sobrien
38997403Sobrien      // Non-standard types:
39097403Sobrien      typedef basic_string<_CharT, _Traits, _Alloc> 	__string_type;
39197403Sobrien      typedef basic_stringbuf<_CharT, _Traits, _Alloc> 	__stringbuf_type;
39297403Sobrien      typedef basic_ostream<char_type, traits_type>	__ostream_type;
39397403Sobrien
39497403Sobrien    private:
39597403Sobrien      __stringbuf_type	_M_stringbuf;
39697403Sobrien
39797403Sobrien    public:
398117397Skan      // Constructors/destructor:
399117397Skan      /**
400117397Skan       *  @brief  Default constructor starts with an empty string buffer.
401117397Skan       *  @param  mode  Whether the buffer can read, or write, or both.
402117397Skan       *
403117397Skan       *  @c ios_base::out is automatically included in @a mode.
404117397Skan       *
405117397Skan       *  Initializes @c sb using @c mode|out, and passes @c &sb to the base
406117397Skan       *  class initializer.  Does not allocate any buffer.
407117397Skan       *
408117397Skan       *  @if maint
409117397Skan       *  That's a lie.  We initialize the base class with NULL, because the
410117397Skan       *  string class does its own memory management.
411117397Skan       *  @endif
412117397Skan      */
41397403Sobrien      explicit
41497403Sobrien      basic_ostringstream(ios_base::openmode __mode = ios_base::out)
415132720Skan      : __ostream_type(), _M_stringbuf(__mode | ios_base::out)
41697403Sobrien      { this->init(&_M_stringbuf); }
41797403Sobrien
418117397Skan      /**
419117397Skan       *  @brief  Starts with an existing string buffer.
420117397Skan       *  @param  str  A string to copy as a starting buffer.
421117397Skan       *  @param  mode  Whether the buffer can read, or write, or both.
422117397Skan       *
423117397Skan       *  @c ios_base::out is automatically included in @a mode.
424117397Skan       *
425117397Skan       *  Initializes @c sb using @a str and @c mode|out, and passes @c &sb
426117397Skan       *  to the base class initializer.
427117397Skan       *
428117397Skan       *  @if maint
429117397Skan       *  That's a lie.  We initialize the base class with NULL, because the
430117397Skan       *  string class does its own memory management.
431117397Skan       *  @endif
432117397Skan      */
43397403Sobrien      explicit
43497403Sobrien      basic_ostringstream(const __string_type& __str,
43597403Sobrien			  ios_base::openmode __mode = ios_base::out)
436132720Skan      : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out)
43797403Sobrien      { this->init(&_M_stringbuf); }
43897403Sobrien
439117397Skan      /**
440117397Skan       *  @brief  The destructor does nothing.
441117397Skan       *
442117397Skan       *  The buffer is deallocated by the stringbuf object, not the
443117397Skan       *  formatting stream.
444117397Skan      */
44597403Sobrien      ~basic_ostringstream()
44697403Sobrien      { }
44797403Sobrien
44897403Sobrien      // Members:
449117397Skan      /**
450117397Skan       *  @brief  Accessing the underlying buffer.
451117397Skan       *  @return  The current basic_stringbuf buffer.
452117397Skan       *
453117397Skan       *  This hides both signatures of std::basic_ios::rdbuf().
454117397Skan      */
45597403Sobrien      __stringbuf_type*
45697403Sobrien      rdbuf() const
45797403Sobrien      { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
45897403Sobrien
459117397Skan      /**
460117397Skan       *  @brief  Copying out the string buffer.
461117397Skan       *  @return  @c rdbuf()->str()
462117397Skan      */
46397403Sobrien      __string_type
46497403Sobrien      str() const
46597403Sobrien      { return _M_stringbuf.str(); }
46697403Sobrien
467117397Skan      /**
468117397Skan       *  @brief  Setting a new buffer.
469117397Skan       *  @param  s  The string to use as a new sequence.
470117397Skan       *
471117397Skan       *  Calls @c rdbuf()->str(s).
472117397Skan      */
47397403Sobrien      void
47497403Sobrien      str(const __string_type& __s)
47597403Sobrien      { _M_stringbuf.str(__s); }
47697403Sobrien    };
47797403Sobrien
47897403Sobrien
479117397Skan  // [27.7.4] Template class basic_stringstream
480117397Skan  /**
481117397Skan   *  @brief  Controlling input and output for std::string.
482117397Skan   *
483117397Skan   *  This class supports reading from and writing to objects of type
484117397Skan   *  std::basic_string, using the inherited functions from
485117397Skan   *  std::basic_iostream.  To control the associated sequence, an instance
486117397Skan   *  of std::basic_stringbuf is used, which this page refers to as @c sb.
487117397Skan  */
48897403Sobrien  template <typename _CharT, typename _Traits, typename _Alloc>
48997403Sobrien    class basic_stringstream : public basic_iostream<_CharT, _Traits>
49097403Sobrien    {
49197403Sobrien    public:
49297403Sobrien      // Types:
49397403Sobrien      typedef _CharT 					char_type;
49497403Sobrien      typedef _Traits 					traits_type;
495132720Skan      // _GLIBCXX_RESOLVE_LIB_DEFECTS
496132720Skan      // 251. basic_stringbuf missing allocator_type
49797403Sobrien      typedef _Alloc				       	allocator_type;
49897403Sobrien      typedef typename traits_type::int_type 		int_type;
49997403Sobrien      typedef typename traits_type::pos_type 		pos_type;
50097403Sobrien      typedef typename traits_type::off_type 		off_type;
50197403Sobrien
50297403Sobrien      // Non-standard Types:
50397403Sobrien      typedef basic_string<_CharT, _Traits, _Alloc> 	__string_type;
50497403Sobrien      typedef basic_stringbuf<_CharT, _Traits, _Alloc> 	__stringbuf_type;
50597403Sobrien      typedef basic_iostream<char_type, traits_type>	__iostream_type;
50697403Sobrien
50797403Sobrien    private:
50897403Sobrien      __stringbuf_type	_M_stringbuf;
50997403Sobrien
51097403Sobrien    public:
51197403Sobrien      // Constructors/destructors
512117397Skan      /**
513117397Skan       *  @brief  Default constructor starts with an empty string buffer.
514117397Skan       *  @param  mode  Whether the buffer can read, or write, or both.
515117397Skan       *
516117397Skan       *  Initializes @c sb using @c mode, and passes @c &sb to the base
517117397Skan       *  class initializer.  Does not allocate any buffer.
518117397Skan       *
519117397Skan       *  @if maint
520117397Skan       *  That's a lie.  We initialize the base class with NULL, because the
521117397Skan       *  string class does its own memory management.
522117397Skan       *  @endif
523117397Skan      */
52497403Sobrien      explicit
52597403Sobrien      basic_stringstream(ios_base::openmode __m = ios_base::out | ios_base::in)
526132720Skan      : __iostream_type(), _M_stringbuf(__m)
52797403Sobrien      { this->init(&_M_stringbuf); }
52897403Sobrien
529117397Skan      /**
530117397Skan       *  @brief  Starts with an existing string buffer.
531117397Skan       *  @param  str  A string to copy as a starting buffer.
532117397Skan       *  @param  mode  Whether the buffer can read, or write, or both.
533117397Skan       *
534117397Skan       *  Initializes @c sb using @a str and @c mode, and passes @c &sb
535117397Skan       *  to the base class initializer.
536117397Skan       *
537117397Skan       *  @if maint
538117397Skan       *  That's a lie.  We initialize the base class with NULL, because the
539117397Skan       *  string class does its own memory management.
540117397Skan       *  @endif
541117397Skan      */
54297403Sobrien      explicit
54397403Sobrien      basic_stringstream(const __string_type& __str,
54497403Sobrien			 ios_base::openmode __m = ios_base::out | ios_base::in)
545132720Skan      : __iostream_type(), _M_stringbuf(__str, __m)
54697403Sobrien      { this->init(&_M_stringbuf); }
54797403Sobrien
548117397Skan      /**
549117397Skan       *  @brief  The destructor does nothing.
550117397Skan       *
551117397Skan       *  The buffer is deallocated by the stringbuf object, not the
552117397Skan       *  formatting stream.
553117397Skan      */
55497403Sobrien      ~basic_stringstream()
55597403Sobrien      { }
55697403Sobrien
55797403Sobrien      // Members:
558117397Skan      /**
559117397Skan       *  @brief  Accessing the underlying buffer.
560117397Skan       *  @return  The current basic_stringbuf buffer.
561117397Skan       *
562117397Skan       *  This hides both signatures of std::basic_ios::rdbuf().
563117397Skan      */
56497403Sobrien      __stringbuf_type*
56597403Sobrien      rdbuf() const
56697403Sobrien      { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
56797403Sobrien
568117397Skan      /**
569117397Skan       *  @brief  Copying out the string buffer.
570117397Skan       *  @return  @c rdbuf()->str()
571117397Skan      */
57297403Sobrien      __string_type
57397403Sobrien      str() const
57497403Sobrien      { return _M_stringbuf.str(); }
57597403Sobrien
576117397Skan      /**
577117397Skan       *  @brief  Setting a new buffer.
578117397Skan       *  @param  s  The string to use as a new sequence.
579117397Skan       *
580117397Skan       *  Calls @c rdbuf()->str(s).
581117397Skan      */
58297403Sobrien      void
58397403Sobrien      str(const __string_type& __s)
58497403Sobrien      { _M_stringbuf.str(__s); }
58597403Sobrien    };
58697403Sobrien
587169691Skan_GLIBCXX_END_NAMESPACE
588169691Skan
589132720Skan#ifndef _GLIBCXX_EXPORT_TEMPLATE
59097403Sobrien# include <bits/sstream.tcc>
59197403Sobrien#endif
59297403Sobrien
593132720Skan#endif /* _GLIBCXX_SSTREAM */
594