30 Input/output library [input.output]

30.8 String-based streams [string.streams]

30.8.2 Class template basic_­stringbuf [stringbuf]

namespace std {
  template <class charT, class traits = char_traits<charT>,
            class Allocator = allocator<charT>>
  class basic_stringbuf : public basic_streambuf<charT, traits> {
  public:
    using char_type      = charT;
    using int_type       = typename traits::int_type;
    using pos_type       = typename traits::pos_type;
    using off_type       = typename traits::off_type;
    using traits_type    = traits;
    using allocator_type = Allocator;

    // [stringbuf.cons], constructors
    explicit basic_stringbuf(
      ios_base::openmode which = ios_base::in | ios_base::out);
    explicit basic_stringbuf(
      const basic_string<charT, traits, Allocator>& str,
      ios_base::openmode which = ios_base::in | ios_base::out);
    basic_stringbuf(const basic_stringbuf& rhs) = delete;
    basic_stringbuf(basic_stringbuf&& rhs);

    // [stringbuf.assign], assign and swap
    basic_stringbuf& operator=(const basic_stringbuf& rhs) = delete;
    basic_stringbuf& operator=(basic_stringbuf&& rhs);
    void swap(basic_stringbuf& rhs);

    // [stringbuf.members], get and set
    basic_string<charT, traits, Allocator> str() const;
    void str(const basic_string<charT, traits, Allocator>& s);

  protected:
    // [stringbuf.virtuals], overridden virtual functions
    int_type underflow() override;
    int_type pbackfail(int_type c = traits::eof()) override;
    int_type overflow (int_type c = traits::eof()) override;
    basic_streambuf<charT, traits>* setbuf(charT*, streamsize) override;

    pos_type seekoff(off_type off, ios_base::seekdir way,
                     ios_base::openmode which
                      = ios_base::in | ios_base::out) override;
    pos_type seekpos(pos_type sp,
                     ios_base::openmode which
                      = ios_base::in | ios_base::out) override;

  private:
    ios_base::openmode mode;  // exposition only
  };

  template <class charT, class traits, class Allocator>
    void swap(basic_stringbuf<charT, traits, Allocator>& x,
              basic_stringbuf<charT, traits, Allocator>& y);
}

The class basic_­stringbuf is derived from basic_­streambuf to associate possibly the input sequence and possibly the output sequence with a sequence of arbitrary characters. The sequence can be initialized from, or made available as, an object of class basic_­string.

For the sake of exposition, the maintained data is presented here as:

30.8.2.1 basic_­stringbuf constructors [stringbuf.cons]

explicit basic_stringbuf( ios_base::openmode which = ios_base::in | ios_base::out);

Effects: Constructs an object of class basic_­stringbuf, initializing the base class with basic_­streambuf() ([streambuf.cons]), and initializing mode with which.

Postconditions: str() == "".

explicit basic_stringbuf( const basic_string<charT, traits, Allocator>& s, ios_base::openmode which = ios_base::in | ios_base::out);

Effects: Constructs an object of class basic_­stringbuf, initializing the base class with basic_­streambuf() ([streambuf.cons]), and initializing mode with which. Then calls str(s).

basic_stringbuf(basic_stringbuf&& rhs);

Effects: Move constructs from the rvalue rhs. It is implementation-defined whether the sequence pointers in *this (eback(), gptr(), egptr(), pbase(), pptr(), epptr()) obtain the values which rhs had. Whether they do or not, *this and rhs reference separate buffers (if any at all) after the construction. The openmode, locale and any other state of rhs is also copied.

Postconditions: Let rhs_­p refer to the state of rhs just prior to this construction and let rhs_­a refer to the state of rhs just after this construction.

  • str() == rhs_­p.str()

  • gptr() - eback() == rhs_­p.gptr() - rhs_­p.eback()

  • egptr() - eback() == rhs_­p.egptr() - rhs_­p.eback()

  • pptr() - pbase() == rhs_­p.pptr() - rhs_­p.pbase()

  • epptr() - pbase() == rhs_­p.epptr() - rhs_­p.pbase()

  • if (eback()) eback() != rhs_­a.eback()

  • if (gptr()) gptr() != rhs_­a.gptr()

  • if (egptr()) egptr() != rhs_­a.egptr()

  • if (pbase()) pbase() != rhs_­a.pbase()

  • if (pptr()) pptr() != rhs_­a.pptr()

  • if (epptr()) epptr() != rhs_­a.epptr()

30.8.2.2 Assign and swap [stringbuf.assign]

basic_stringbuf& operator=(basic_stringbuf&& rhs);

Effects: After the move assignment *this has the observable state it would have had if it had been move constructed from rhs (see [stringbuf.cons]).

Returns: *this.

void swap(basic_stringbuf& rhs);

Effects: Exchanges the state of *this and rhs.

template <class charT, class traits, class Allocator> void swap(basic_stringbuf<charT, traits, Allocator>& x, basic_stringbuf<charT, traits, Allocator>& y);

Effects: As if by x.swap(y).

30.8.2.3 Member functions [stringbuf.members]

basic_string<charT, traits, Allocator> str() const;

Returns: A basic_­string object whose content is equal to the basic_­stringbuf underlying character sequence. If the basic_­stringbuf was created only in input mode, the resultant basic_­string contains the character sequence in the range [eback(), egptr()). If the basic_­stringbuf was created with which & ios_­base​::​out being nonzero then the resultant basic_­string contains the character sequence in the range [pbase(), high_­mark), where high_­mark represents the position one past the highest initialized character in the buffer. Characters can be initialized by writing to the stream, by constructing the basic_­stringbuf with a basic_­string, or by calling the str(basic_­string) member function. In the case of calling the str(basic_­string) member function, all characters initialized prior to the call are now considered uninitialized (except for those characters re-initialized by the new basic_­string). Otherwise the basic_­stringbuf has been created in neither input nor output mode and a zero length basic_­string is returned.

void str(const basic_string<charT, traits, Allocator>& s);

Effects: Copies the content of s into the basic_­stringbuf underlying character sequence and initializes the input and output sequences according to mode.

Postconditions: If mode & ios_­base​::​out is nonzero, pbase() points to the first underlying character and epptr() >= pbase() + s.size() holds; in addition, if mode & ios_­base​::​ate is nonzero, pptr() == pbase() + s.size() holds, otherwise pptr() == pbase() is true. If mode & ios_­base​::​in is nonzero, eback() points to the first underlying character, and both gptr() == eback() and egptr() == eback() + s.size() hold.

30.8.2.4 Overridden virtual functions [stringbuf.virtuals]

int_type underflow() override;

Returns: If the input sequence has a read position available, returns traits​::​to_­int_­type(*gptr()). Otherwise, returns traits​::​eof(). Any character in the underlying buffer which has been initialized is considered to be part of the input sequence.

int_type pbackfail(int_type c = traits::eof()) override;

Effects: Puts back the character designated by c to the input sequence, if possible, in one of three ways:

  • If traits​::​eq_­int_­type(c, traits​::​eof()) returns false and if the input sequence has a putback position available, and if traits​::​eq(to_­char_­type(c), gptr()[-1]) returns true, assigns gptr() - 1 to gptr().

    Returns: c.

  • If traits​::​eq_­int_­type(c, traits​::​eof()) returns false and if the input sequence has a putback position available, and if mode & ios_­base​::​out is nonzero, assigns c to *--gptr().

    Returns: c.

  • If traits​::​eq_­int_­type(c, traits​::​eof()) returns true and if the input sequence has a putback position available, assigns gptr() - 1 to gptr().

    Returns: traits​::​not_­eof(c).

Returns: traits​::​eof() to indicate failure.

Remarks: If the function can succeed in more than one of these ways, it is unspecified which way is chosen.

int_type overflow(int_type c = traits::eof()) override;

Effects: Appends the character designated by c to the output sequence, if possible, in one of two ways:

  • If traits​::​eq_­int_­type(c, traits​::​eof()) returns false and if either the output sequence has a write position available or the function makes a write position available (as described below), the function calls sputc(c).

    Signals success by returning c.

  • If traits​::​eq_­int_­type(c, traits​::​eof()) returns true, there is no character to append.

    Signals success by returning a value other than traits​::​eof().

Remarks: The function can alter the number of write positions available as a result of any call.

Returns: traits​::​eof() to indicate failure.

The function can make a write position available only if (mode & ios_­base​::​out) != 0. To make a write position available, the function reallocates (or initially allocates) an array object with a sufficient number of elements to hold the current array object (if any), plus at least one additional write position. If (mode & ios_­base​::​in) != 0, the function alters the read end pointer egptr() to point just past the new write position.

pos_type seekoff(off_type off, ios_base::seekdir way, ios_base::openmode which = ios_base::in | ios_base::out) override;

Effects: Alters the stream position within one of the controlled sequences, if possible, as indicated in Table 115.

Table 115seekoff positioning
ConditionsResult
(which & ios_­base​::​in) == ios_­base​::​in positions the input sequence
(which & ios_­base​::​out) == ios_­base​::​out positions the output sequence
(which & (ios_­base​::​in |
ios_­base​::​out)) ==
(ios_­base​::​in) |
ios_­base​::​out))
and way == either
ios_­base​::​beg or
ios_­base​::​end
positions both the input and the output sequences
Otherwise the positioning operation fails.

For a sequence to be positioned, if its next pointer (either gptr() or pptr()) is a null pointer and the new offset newoff is nonzero, the positioning operation fails. Otherwise, the function determines newoff as indicated in Table 116.

Table 116newoff values
Conditionnewoff Value
way == ios_­base​::​beg 0
way == ios_­base​::​cur the next pointer minus the beginning pointer (xnext - xbeg).
way == ios_­base​::​end the high mark pointer minus the beginning pointer (high_­mark - xbeg).

If (newoff + off) < 0, or if newoff + off refers to an uninitialized character ([stringbuf.members]), the positioning operation fails. Otherwise, the function assigns xbeg + newoff + off to the next pointer xnext.

Returns: pos_­type(newoff), constructed from the resultant offset newoff (of type off_­type), that stores the resultant stream position, if possible. If the positioning operation fails, or if the constructed object cannot represent the resultant stream position, the return value is pos_­type(off_­type(-1)).

pos_type seekpos(pos_type sp, ios_base::openmode which = ios_base::in | ios_base::out) override;

Effects: Equivalent to seekoff(off_­type(sp), ios_­base​::​beg, which).

Returns: sp to indicate success, or pos_­type(off_­type(-1)) to indicate failure.

basic_streambuf<charT, traits>* setbuf(charT* s, streamsize n);

Effects: implementation-defined, except that setbuf(0, 0) has no effect.

Returns: this.