31 Input/output library [input.output]

31.9 Span-based streams [span.streams]

31.9.3 Class template basic_spanbuf [spanbuf]

31.9.3.1 General [spanbuf.general]

namespace std { template<class charT, class traits = char_traits<charT>> class basic_spanbuf : 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; // [spanbuf.cons], constructors basic_spanbuf() : basic_spanbuf(ios_base::in | ios_base::out) {} explicit basic_spanbuf(ios_base::openmode which) : basic_spanbuf(std::span<charT>(), which) {} explicit basic_spanbuf(std::span<charT> s, ios_base::openmode which = ios_base::in | ios_base::out); basic_spanbuf(const basic_spanbuf&) = delete; basic_spanbuf(basic_spanbuf&& rhs); // [spanbuf.assign], assignment and swap basic_spanbuf& operator=(const basic_spanbuf&) = delete; basic_spanbuf& operator=(basic_spanbuf&& rhs); void swap(basic_spanbuf& rhs); // [spanbuf.members], member functions std::span<charT> span() const noexcept; void span(std::span<charT> s) noexcept; protected: // [spanbuf.virtuals], overridden virtual functions 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 std::span<charT> buf; // exposition only }; }
The class template basic_spanbuf is derived from basic_streambuf to associate possibly the input sequence and possibly the output sequence with a sequence of arbitrary characters.
The sequence is provided by an object of class span<charT>.
For the sake of exposition, the maintained data is presented here as:
  • ios_base​::​openmode mode, has in set if the input sequence can be read, and out set if the output sequence can be written.
  • std​::​span<charT> buf is the view to the underlying character sequence.

31.9.3.2 Constructors [spanbuf.cons]

explicit basic_spanbuf(std::span<charT> s, ios_base::openmode which = ios_base::in | ios_base::out);
Effects: Initializes the base class with basic_streambuf() ([streambuf.cons]), and mode with which.
Initializes the internal pointers as if calling span(s).
basic_spanbuf(basic_spanbuf&& rhs);
Effects: Initializes the base class with std​::​move(rhs) and mode with std​::​move(rhs.mode) and buf with std​::​move(rhs.buf).
The sequence pointers in *this (eback(), gptr(), egptr(), pbase(), pptr(), epptr()) obtain the values which rhs had.
It is implementation-defined whether rhs.buf.​empty() returns true after the move.
Postconditions: Let rhs_p refer to the state of rhs just prior to this construction.
  • span().data() == rhs_p.span().data()
  • span().size() == rhs_p.span().size()
  • eback() == rhs_p.eback()
  • gptr() == rhs_p.gptr()
  • egptr() == rhs_p.egptr()
  • pbase() == rhs_p.pbase()
  • pptr() == rhs_p.pptr()
  • epptr() == rhs_p.epptr()
  • getloc() == rhs_p.getloc()

31.9.3.3 Assignment and swap [spanbuf.assign]

basic_spanbuf& operator=(basic_spanbuf&& rhs);
Effects: Equivalent to: basic_spanbuf tmp{std::move(rhs)}; this->swap(tmp); return *this;
void swap(basic_spanbuf& rhs);
Effects: Equivalent to: basic_streambuf<charT, traits>::swap(rhs); std::swap(mode, rhs.mode); std::swap(buf, rhs.buf);
template<class charT, class traits> void swap(basic_spanbuf<charT, traits>& x, basic_spanbuf<charT, traits>& y);
Effects: Equivalent to x.swap(y).

31.9.3.4 Member functions [spanbuf.members]

std::span<charT> span() const noexcept;
Returns: If ios_base​::​out is set in mode, returns std​::​span<charT>(pbase(), pptr()), otherwise returns buf.
[Note 1: 
In contrast to basic_stringbuf, the underlying sequence never grows and is not owned.
An owning copy can be obtained by converting the result to basic_string<charT>.
— end note]
void span(std::span<charT> s) noexcept;
Effects: buf = s.
Initializes the input and output sequences according to mode.
Postconditions:
  • If ios_base​::​out is set in mode, pbase() == s.data() && epptr() == pbase() + s.size() is true;
    • in addition, if ios_base​::​ate is set in mode, pptr() == pbase() + s.size() is true,
    • otherwise pptr() == pbase() is true.
  • If ios_base​::​in is set in mode, eback() == s.data() && gptr() == eback() && egptr() == eback() + s.size() is true.

31.9.3.5 Overridden virtual functions [spanbuf.virtuals]

[Note 1: 
Because the underlying buffer is of fixed size, neither overflow, underflow, nor pbackfail can provide useful behavior.
— end note]
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 or both of the controlled sequences, if possible, as follows:
  • If ios_base​::​in is set in which, positions the input sequence; xnext is gptr(), xbeg is eback().
  • If ios_base​::​out is set in which, positions the output sequence; xnext is pptr(), xbeg is pbase().
If both ios_base​::​in and ios_base​::​out are set in which and way is ios_base​::​cur, the positioning operation fails.
For a sequence to be positioned, if its next pointer xnext (either gptr() or pptr()) is a null pointer and the new offset newoff as computed below is nonzero, the positioning operation fails.
Otherwise, the function determines baseoff as a value of type off_type as follows:
  • 0 when way is ios_base​::​beg;
  • (pptr() - pbase()) for the output sequence, or (gptr() - eback()) for the input sequence when way is ios_base​::​cur;
  • when way is ios_base​::​end :
    • (pptr() - pbase()) if ios_base​::​out is set in mode and ios_base​::​in is not set in mode,
    • buf.size() otherwise.
If would overflow, or if is less than zero, or if is greater than buf.size(), the positioning operation fails.
Otherwise, the function computes off_type newoff = baseoff + off; and assigns xbeg + newoff to the next pointer xnext.
Returns: pos_type(off_type(-1)) if the positioning operation fails; pos_type(newoff) otherwise.
pos_type seekpos(pos_type sp, ios_base::openmode which = ios_base::in | ios_base::out) override;
Effects: Equivalent to: return seekoff(off_type(sp), ios_base::beg, which);
basic_streambuf<charT, traits>* setbuf(charT* s, streamsize n) override;
Effects: Equivalent to: this->span(std::span<charT>(s, n)); return this;