[stringbuf] (original) (raw)

31 Input/output library [input.output]

31.8 String-based streams [string.streams]

31.8.2 Class template basic_stringbuf [stringbuf]

31.8.2.1 General [stringbuf.general]

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; basic_stringbuf() : basic_stringbuf(ios_base::in | ios_base::out) {} explicit basic_stringbuf(ios_base::openmode which);explicit basic_stringbuf( const basic_string<charT, traits, Allocator>& s, ios_base::openmode which = ios_base::in | ios_base::out);explicit basic_stringbuf(const Allocator& a) : basic_stringbuf(ios_base::in | ios_base::out, a) {} basic_stringbuf(ios_base::openmode which, const Allocator& a);explicit basic_stringbuf( basic_string<charT, traits, Allocator>&& s, ios_base::openmode which = ios_base::in | ios_base::out);template<class SAlloc> basic_stringbuf( const basic_string<charT, traits, SAlloc>& s, const Allocator& a) : basic_stringbuf(s, ios_base::in | ios_base::out, a) {} template<class SAlloc> basic_stringbuf( const basic_string<charT, traits, SAlloc>& s, ios_base::openmode which, const Allocator& a);template<class SAlloc> explicit basic_stringbuf( const basic_string<charT, traits, SAlloc>& s, ios_base::openmode which = ios_base::in | ios_base::out);template<class T> explicit basic_stringbuf(const T& t, ios_base::openmode which = ios_base::in | ios_base::out);template<class T> basic_stringbuf(const T& t, const Allocator& a);template<class T> basic_stringbuf(const T& t, ios_base::openmode which, const Allocator& a); basic_stringbuf(const basic_stringbuf&) = delete; basic_stringbuf(basic_stringbuf&& rhs); basic_stringbuf(basic_stringbuf&& rhs, const Allocator& a); basic_stringbuf& operator=(const basic_stringbuf&) = delete; basic_stringbuf& operator=(basic_stringbuf&& rhs);void swap(basic_stringbuf& rhs) noexcept(see below); allocator_type get_allocator() const noexcept; basic_string<charT, traits, Allocator> str() const &;template<class SAlloc> basic_string<charT,traits,SAlloc> str(const SAlloc& sa) const; basic_string<charT, traits, Allocator> str() &&; basic_string_view<charT, traits> view() const noexcept;void str(const basic_string<charT, traits, Allocator>& s);template<class SAlloc> void str(const basic_string<charT, traits, SAlloc>& s);void str(basic_string<charT, traits, Allocator>&& s);template<class T> void str(const T& t);protected: 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; basic_string<charT, traits, Allocator> buf; void init-buf-ptrs(); };}

The classbasic_stringbufis derived frombasic_streambufto associate possibly the input sequence and possibly the output sequence with a sequence of arbitrarycharacters.

The sequence can be initialized from, or made available as, an object of classbasic_string.

For the sake of exposition, the maintained data and internal pointer initialization is presented here as:

31.8.2.2 Constructors [stringbuf.cons]

explicit basic_stringbuf(ios_base::openmode which);

Effects: Initializes the base class withbasic_streambuf() ([streambuf.cons]), and_mode_with which.

It isimplementation-defined whether the sequence pointers (eback(), gptr(), egptr(),pbase(), pptr(), epptr()) are initialized to null pointers.

Postconditions: str().empty() is true.

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

Effects: Initializes the base class withbasic_streambuf() ([streambuf.cons]),mode with which, and_buf_ with s, then calls init-buf-ptrs().

basic_stringbuf(ios_base::openmode which, const Allocator& a);

Effects: Initializes the base class withbasic_streambuf() ([streambuf.cons]),mode with which, and_buf_ with a, then calls init-buf-ptrs().

Postconditions: str().empty() is true.

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

Effects: Initializes the base class with basic_streambuf() ([streambuf.cons]),mode with which, and_buf_ with std​::​move(s), then calls init-buf-ptrs().

template<class SAlloc> basic_stringbuf( const basic_string<charT, traits, SAlloc>& s, ios_base::openmode which, const Allocator& a);

Effects: Initializes the base class with basic_streambuf() ([streambuf.cons]),mode with which, and_buf_ with {s,a}, then calls init-buf-ptrs().

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

Constraints: is_same_v<SAlloc, Allocator> is false.

Effects: Initializes the base class with basic_streambuf() ([streambuf.cons]),mode with which, and_buf_ with s, then calls init-buf-ptrs().

template<class T> explicit basic_stringbuf(const T& t, ios_base::openmode which = ios_base::in | ios_base::out);template<class T> basic_stringbuf(const T& t, const Allocator& a);template<class T> basic_stringbuf(const T& t, ios_base::openmode which, const Allocator& a);

Let which be ios_base​::​in | ios_base​::​outfor the overload with no parameter which, anda be Allocator() for the overload with no parameter a.

Constraints: is_convertible_v<const T&, basic_string_view<charT, traits>>is true.

Effects: Creates a variable sv as if bybasic_string_view<charT, traits> sv = t, then value-initializes the base class, initializes mode with which, and direct-non-list-initializes buf with sv, a, then calls init-buf-ptrs().

basic_stringbuf(basic_stringbuf&& rhs); basic_stringbuf(basic_stringbuf&& rhs, const Allocator& a);

Effects: Copy constructs the base class from rhs and initializes mode with rhs.mode.

In the first form buf is initialized from std​::​move(rhs).str().

In the second form buf is initialized from {std​::​move(rhs).str(), a}.

It isimplementation-defined whether the sequence pointers in *this(eback(), gptr(), egptr(),pbase(), pptr(), epptr()) obtain the values which rhs had.

Postconditions: Let rhs_p refer to the state ofrhs just prior to this construction and let rhs_arefer to the state of rhs just after this construction.

31.8.2.3 Assignment 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]).

void swap(basic_stringbuf& rhs) noexcept(_see below_);

Preconditions: allocator_traits<Allocator>​::​propagate_on_container_swap​::​valueis true orget_allocator() == rhs.get_allocator() is true.

Effects: Exchanges the state of *thisand rhs.

Remarks: The exception specification is equivalent to:

allocator_traits<Allocator>​::​propagate_on_container_swap​::​value ||

allocator_traits<Allocator>​::​is_always_equal​::​value

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

Effects: Equivalent to x.swap(y).

31.8.2.4 Member functions [stringbuf.members]

The member functions getting the underlying character sequence all refer to a high_mark value, 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_stringbufpassing a basic_string argument, or by calling one of the str member functions passing a basic_string as an argument.

In the latter case, all characters initialized prior to the call are now considered uninitialized (except for those characters re-initialized by the new basic_string).

void _init-buf-ptrs_(); // _exposition only_

Effects: Initializes the input and output sequences from _buf_according to mode.

Postconditions:

[Note 1:

For efficiency reasons, stream buffer operations can violate invariants of _buf_while it is held encapsulated in the basic_stringbuf, e.g., by writing to characters in the range [_buf_.data() + _buf_.size(), _buf_.data() + _buf_.capacity()).

All operations retrieving a basic_string from bufensure that the basic_string invariants hold on the returned value.

— _end note_]

allocator_type get_allocator() const noexcept;

Returns: buf.get_allocator().

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

Effects: Equivalent to:return basic_string<charT, traits, Allocator>(view(), get_allocator());

template<class SAlloc> basic_string<charT, traits, SAlloc> str(const SAlloc& sa) const;

Effects: Equivalent to:return basic_string<charT, traits, SAlloc>(view(), sa);

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

Postconditions: The underlying character sequence buf is empty andpbase(), pptr(), epptr(), eback(),gptr(), and egptr()are initialized as if by calling init-buf-ptrs()with an empty buf.

Returns: A basic_string<charT, traits, Allocator> object move constructed from the basic_stringbuf's underlying character sequence in buf.

This can be achieved by first adjusting buf to have the same content as view().

basic_string_view<charT, traits> view() const noexcept;

Let sv be basic_string_view<charT, traits>.

Returns: A sv object referring to the basic_stringbuf's underlying character sequence in buf:

[Note 2:

Using the returned sv object after destruction or invalidation of the character sequence underlying *thisis undefined behavior, unless sv.empty() is true.

— _end note_]

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

Effects: Equivalent to:buf = s;init-buf-ptrs();

template<class SAlloc> void str(const basic_string<charT, traits, SAlloc>& s);

Constraints: is_same_v<SAlloc,Allocator> is false.

Effects: Equivalent to:buf = s;init-buf-ptrs();

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

Effects: Equivalent to:buf = std::move(s);init-buf-ptrs();

template<class T> void str(const T& t);

Constraints: is_convertible_v<const T&, basic_string_view<charT, traits>>is true.

Effects: Equivalent to:basic_string_view<charT, traits> sv = t;buf = sv;init-buf-ptrs();

31.8.2.5 Overridden virtual functions [stringbuf.virtuals]

int_type underflow() override;

Returns: If the input sequence has a read position available, returnstraits​::​to_int_type(*gptr()).

Otherwise, returnstraits​::​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:

Returns: As specified above, ortraits​::​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:

Returns: As specified above, ortraits​::​eof()to indicate failure.

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

The function can make a write position available only ifios_base​::​out is set in mode.

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 ios_base​::​in is set in mode, the function alters the read end pointeregptr()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 142.

Table 142 — seekoff positioning [tab:stringbuf.seekoff.pos]

ios_base​::​in is set in which positions the input sequence
ios_base​::​out is set in which positions the output sequence
both ios_base​::​in and ios_base​::​out are set in which and eitherway == ios_base​::​beg orway == ios_base​::​end positions both the input and the output sequences
the positioning operation fails.

For a sequence to be positioned, the function determines newoff as indicated in Table 143.

If the sequence's next pointer (eithergptr()orpptr()) is a null pointer and newoff is nonzero, the positioning operation fails.

Table 143 — newoff values [tab:stringbuf.seekoff.newoff]

the next pointer minus the beginning pointer (xnext - xbeg).
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 assignsxbeg + newoff + offto the next pointer xnext.

Returns: pos_type(newoff), constructed from the resultant offset newoff(of typeoff_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 ispos_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: spto indicate success, orpos_type(off_type(-1))to indicate failure.

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

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