Files
cppdraft_translate/cppdraft/streambuf.md
2025-10-25 03:02:53 +03:00

37 KiB
Raw Permalink Blame History

[streambuf]

31 Input/output library [input.output]

31.6 Stream buffers [stream.buffers]

31.6.3 Class template basic_streambuf [streambuf]

31.6.3.1 General [streambuf.general]

🔗

namespace std {template<class charT, class traits = char_traits>class basic_streambuf {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; virtual ~basic_streambuf(); // [streambuf.locales], locales locale pubimbue(const locale& loc); locale getloc() const; // [streambuf.buffer], buffer and positioning basic_streambuf* pubsetbuf(char_type* s, streamsize n); pos_type pubseekoff(off_type off, ios_base::seekdir way, ios_base::openmode which = ios_base::in | ios_base::out); pos_type pubseekpos(pos_type sp, ios_base::openmode which = ios_base::in | ios_base::out); int pubsync(); // get and put areas// [streambuf.pub.get], get area streamsize in_avail(); int_type snextc(); int_type sbumpc(); int_type sgetc(); streamsize sgetn(char_type* s, streamsize n); // [streambuf.pub.pback], putback int_type sputbackc(char_type c); int_type sungetc(); // [streambuf.pub.put], put area int_type sputc(char_type c); streamsize sputn(const char_type* s, streamsize n); protected: basic_streambuf(); basic_streambuf(const basic_streambuf& rhs); basic_streambuf& operator=(const basic_streambuf& rhs); void swap(basic_streambuf& rhs); // [streambuf.get.area], get area access char_type* eback() const; char_type* gptr() const; char_type* egptr() const; void gbump(int n); void setg(char_type* gbeg, char_type* gnext, char_type* gend); // [streambuf.put.area], put area access char_type* pbase() const; char_type* pptr() const; char_type* epptr() const; void pbump(int n); void setp(char_type* pbeg, char_type* pend); // [streambuf.virtuals], virtual functions// [streambuf.virt.locales], localesvirtual void imbue(const locale& loc); // [streambuf.virt.buffer], buffer management and positioningvirtual basic_streambuf* setbuf(char_type* s, streamsize n); virtual pos_type seekoff(off_type off, ios_base::seekdir way, ios_base::openmode which = ios_base::in | ios_base::out); virtual pos_type seekpos(pos_type sp, ios_base::openmode which = ios_base::in | ios_base::out); virtual int sync(); // [streambuf.virt.get], get areavirtual streamsize showmanyc(); virtual streamsize xsgetn(char_type* s, streamsize n); virtual int_type underflow(); virtual int_type uflow(); // [streambuf.virt.pback], putbackvirtual int_type pbackfail(int_type c = traits::eof()); // [streambuf.virt.put], put areavirtual streamsize xsputn(const char_type* s, streamsize n); virtual int_type overflow(int_type c = traits::eof()); };}

1

#

The class templatebasic_streambuf serves as a base class for deriving variousstream buffers whose objects each control twocharacter sequences:

a characterinput sequence;

a characteroutput sequence.

31.6.3.2 Constructors [streambuf.cons]

🔗

basic_streambuf();

1

#

Effects: Initializes:266

all pointer member objects to null pointers,

thegetloc() member to a copy of the global locale,locale(), at the time of construction.

2

#

Remarks: Once thegetloc() member is initialized, results of calling locale member functions, and of members of facets so obtained, can safely be cached until the next time the memberimbue is called.

🔗

basic_streambuf(const basic_streambuf& rhs);

3

#

Postconditions:

eback() == rhs.eback()

gptr() == rhs.gptr()

egptr() == rhs.egptr()

pbase() == rhs.pbase()

pptr() == rhs.pptr()

epptr() == rhs.epptr()

getloc() == rhs.getloc()

🔗

~basic_streambuf();

4

#

Effects: None.

266)266)

The default constructor is protected for classbasic_streambuf to assure that only objects for classes derived from this class can be constructed.

31.6.3.3 Public member functions [streambuf.members]

31.6.3.3.1 Locales [streambuf.locales]

🔗

locale pubimbue(const locale& loc);

1

#

Effects: Callsimbue(loc).

2

#

Postconditions: loc == getloc().

3

#

Returns: Previous value ofgetloc().

🔗

locale getloc() const;

4

#

Returns: Ifpubimbue() has ever been called, then the last value of loc supplied, otherwise the current global locale,locale(), in effect at the time of construction.

If called afterpubimbue() has been called but beforepubimbue has returned (i.e., from within the call ofimbue()) then it returns the previous value.

31.6.3.3.2 Buffer management and positioning [streambuf.buffer]

🔗

basic_streambuf* pubsetbuf(char_type* s, streamsize n);

1

#

Returns: setbuf(s, n).

🔗

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

2

#

Returns: seekoff(off, way, which).

🔗

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

3

#

Returns: seekpos(sp, which).

🔗

int pubsync();

4

#

Returns: sync().

31.6.3.3.3 Get area [streambuf.pub.get]

🔗

streamsize in_avail();

1

#

Returns: If a read position is available, returnsegptr() - gptr().

Otherwise returnsshowmanyc().

🔗

int_type snextc();

2

#

Effects: Callssbumpc().

3

#

Returns: If that function returnstraits::eof(), returnstraits::eof().

Otherwise, returnssgetc().

🔗

int_type sbumpc();

4

#

Effects: If the input sequence read position is not available, returnsuflow().

Otherwise, returnstraits::to_int_type(*gptr()) and increments the next pointer for the input sequence.

🔗

int_type sgetc();

5

#

Returns: If the input sequence read position is not available, returnsunderflow().

Otherwise, returnstraits::to_int_type(*gptr()).

🔗

streamsize sgetn(char_type* s, streamsize n);

6

#

Returns: xsgetn(s, n).

31.6.3.3.4 Putback [streambuf.pub.pback]

🔗

int_type sputbackc(char_type c);

1

#

Effects: If the input sequence putback position is not available, or iftraits::eq(c, gptr()[-1]) is false, returnspbackfail(traits::to_int_type(c)).

Otherwise, decrements the next pointer for the input sequence and returnstraits::to_int_type(*gptr()).

🔗

int_type sungetc();

2

#

Effects: If the input sequence putback position is not available, returnspbackfail().

Otherwise, decrements the next pointer for the input sequence and returnstraits::to_int_type(*gptr()).

31.6.3.3.5 Put area [streambuf.pub.put]

🔗

int_type sputc(char_type c);

1

#

Effects: If the output sequence write position is not available, returnsoverflow(traits::to_int_type(c)).

Otherwise, stores c at the next pointer for the output sequence, increments the pointer, and returnstraits::to_int_type(c).

🔗

streamsize sputn(const char_type* s, streamsize n);

2

#

Returns: xsputn(s, n).

31.6.3.4 Protected member functions [streambuf.protected]

31.6.3.4.1 Assignment [streambuf.assign]

🔗

basic_streambuf& operator=(const basic_streambuf& rhs);

1

#

Postconditions:

eback() == rhs.eback()

gptr() == rhs.gptr()

egptr() == rhs.egptr()

pbase() == rhs.pbase()

pptr() == rhs.pptr()

epptr() == rhs.epptr()

getloc() == rhs.getloc()

2

#

Returns: *this.

🔗

void swap(basic_streambuf& rhs);

3

#

Effects: Swaps the data members of rhs and *this.

31.6.3.4.2 Get area access [streambuf.get.area]

🔗

char_type* eback() const;

1

#

Returns: The beginning pointer for the input sequence.

🔗

char_type* gptr() const;

2

#

Returns: The next pointer for the input sequence.

🔗

char_type* egptr() const;

3

#

Returns: The end pointer for the input sequence.

🔗

void gbump(int n);

4

#

Effects: Adds n to the next pointer for the input sequence.

🔗

void setg(char_type* gbeg, char_type* gnext, char_type* gend);

5

#

Preconditions: [gbeg, gnext), [gbeg, gend), and [gnext, gend) are all valid ranges.

6

#

Postconditions: gbeg == eback(),gnext == gptr(), andgend == egptr() are all true.

31.6.3.4.3 Put area access [streambuf.put.area]

🔗

char_type* pbase() const;

1

#

Returns: The beginning pointer for the output sequence.

🔗

char_type* pptr() const;

2

#

Returns: The next pointer for the output sequence.

🔗

char_type* epptr() const;

3

#

Returns: The end pointer for the output sequence.

🔗

void pbump(int n);

4

#

Effects: Adds n to the next pointer for the output sequence.

🔗

void setp(char_type* pbeg, char_type* pend);

5

#

Preconditions: [pbeg, pend) is a valid range.

6

#

Postconditions: pbeg == pbase(),pbeg == pptr(), andpend == epptr() are all true.

31.6.3.5 Virtual functions [streambuf.virtuals]

31.6.3.5.1 Locales [streambuf.virt.locales]

🔗

void imbue(const locale&);

1

#

Effects: Change any translations based on locale.

2

#

Remarks: Allows the derived class to be informed of changes in locale at the time they occur.

Between invocations of this function a class derived from streambuf can safely cache results of calls to locale functions and to members of facets so obtained.

3

#

Default behavior: Does nothing.

31.6.3.5.2 Buffer management and positioning [streambuf.virt.buffer]

🔗

basic_streambuf* setbuf(char_type* s, streamsize n);

1

#

Effects: Influences stream buffering in a way that is defined separately for each class derived frombasic_streambuf in this Clause ([stringbuf.virtuals], [filebuf.virtuals]).

2

#

Default behavior: Does nothing.

Returnsthis.

🔗

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

3

#

Effects: Alters the stream positions within one or more of the controlled sequences in a way that is defined separately for each class derived frombasic_streambuf in this Clause ([stringbuf.virtuals], [filebuf.virtuals]).

4

#

Default behavior: Returnspos_type(off_type(-1)).

🔗

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

5

#

Effects: Alters the stream positions within one or more of the controlled sequences in a way that is defined separately for each class derived frombasic_streambuf in this Clause ([stringbuf], [filebuf]).

6

#

Default behavior: Returnspos_type(off_type(-1)).

🔗

int sync();

7

#

Effects: Synchronizes the controlled sequences with the arrays.

That is, ifpbase() is non-null the characters betweenpbase() andpptr() are written to the controlled sequence.

The pointers may then be reset as appropriate.

8

#

Returns: -1 on failure.

What constitutes failure is determined by each derived class ([filebuf.virtuals]).

9

#

Default behavior: Returns zero.

31.6.3.5.3 Get area [streambuf.virt.get]

🔗

streamsize showmanyc();[267](#footnote-267 "The morphemes of showmanyc are “es-how-many-see”, not “show-manic”.")

1

#

Returns: An estimate of the number of characters available in the sequence, or −1.

If it returns a positive value, then successive calls tounderflow() will not returntraits::eof() until at least that number of characters have been extracted from the stream.

Ifshowmanyc() returns −1, then calls tounderflow() oruflow() will fail.268

2

#

Default behavior: Returns zero.

3

#

Remarks: Usestraits::eof().

🔗

streamsize xsgetn(char_type* s, streamsize n);

4

#

Effects: Assigns up to n characters to successive elements of the array whose first element is designated by s.

The characters assigned are read from the input sequence as if by repeated calls tosbumpc().

Assigning stops when either n characters have been assigned or a call tosbumpc() would returntraits::eof().

5

#

Returns: The number of characters assigned.269

6

#

Remarks: Usestraits::eof().

🔗

int_type underflow();

7

#

Thepending sequence of characters is defined as the concatenation of

the empty sequence if gptr() is null, otherwise the characters in [gptr(), egptr()), followed by

some (possibly empty) sequence of characters read from the input sequence.

8

#

Theresult character is the first character of the pending sequence if it is non-empty, otherwise the next character that would be read from the input sequence.

9

#

Thebackup sequence is the empty sequence if eback() is null, otherwise the characters in [eback(), gptr()).

10

#

Effects: The function sets up thegptr() andegptr() such that if the pending sequence is non-empty, thenegptr() is non-null and the characters in [gptr(), egptr()) are the characters in the pending sequence, otherwise either gptr() is null orgptr() == egptr().

11

#

Ifeback() andgptr() are non-null then the function is not constrained as to their contents, but the “usual backup condition” is that either

the backup sequence contains at leastgptr() - eback() characters, in which case the characters in [eback(), gptr()) agree with the lastgptr() - eback() characters of the backup sequence, or

the characters in [gptr() - n, gptr()) agree with the backup sequence (where n is the length of the backup sequence).

12

#

Returns: traits::to_int_type(c), where c is the firstcharacter of thepending sequence, without moving the input sequence position past it.

If the pending sequence is null then the function returnstraits::eof() to indicate failure.

13

#

Default behavior: Returnstraits::eof().

14

#

Remarks: The public members ofbasic_streambuf call this virtual function only ifgptr() is null orgptr() >= egptr().

🔗

int_type uflow();

15

#

Preconditions: The constraints are the same as forunderflow(), except that the result character is transferred from the pending sequence to the backup sequence, and the pending sequence is not empty before the transfer.

16

#

Default behavior: Callsunderflow().

Ifunderflow() returnstraits::eof(), returnstraits::eof().

Otherwise, returns the value oftraits::to_int_type(*gptr()) and increments the value of the next pointer for the input sequence.

17

#

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

267)267)

The morphemes of showmanyc are “es-how-many-see”, not “show-manic”.

268)268)

underflow oruflow can fail by throwing an exception prematurely.

The intention is not only that the calls will not returneof() but that they will return “immediately”.

269)269)

Classes derived frombasic_streambuf can provide more efficient ways to implementxsgetn() andxsputn() by overriding these definitions from the base class.

31.6.3.5.4 Putback [streambuf.virt.pback]

🔗

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

1

#

Thepending sequence is defined as forunderflow(), with the modifications that

  • (1.1)

    Iftraits::eq_int_type(c, traits::eof()) returnstrue, then the input sequence is backed up one character before the pending sequence is determined.

  • (1.2)

    Iftraits::eq_int_type(c, traits::eof()) returns false, then c is prepended. Whether the input sequence is backed up or modified in any other way is unspecified.

2

#

Postconditions: On return, the constraints ofgptr(),eback(), andpptr() are the same as forunderflow().

3

#

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

Failure may occur because the input sequence could not be backed up, or if for some other reason the pointers cannot be set consistent with the constraints.

pbackfail() is called only when put back has really failed.

4

#

Returns some value other thantraits::eof() to indicate success.

5

#

Default behavior: Returnstraits::eof().

6

#

Remarks: The public functions ofbasic_streambuf call this virtual function only whengptr() is null,gptr() == eback(), ortraits::eq(traits::to_char_type(c), gptr()[-1]) returnsfalse.

Other calls shall also satisfy that constraint.

31.6.3.5.5 Put area [streambuf.virt.put]

🔗

streamsize xsputn(const char_type* s, streamsize n);

1

#

Effects: Writes up to n characters to the output sequence as if by repeated calls tosputc(c).

The characters written are obtained from successive elements of the array whose first element is designated by s.

Writing stops when either n characters have been written or a call tosputc(c) would returntraits::eof().

It is unspecified whether the function calls overflow() when pptr() == epptr() becomes true or whether it achieves the same effects by other means.

2

#

Returns: The number of characters written.

🔗

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

3

#

Effects: Consumes some initial subsequence of the characters of thepending sequence.

The pending sequence is defined as the concatenation of

the empty sequence if pbase() is null, otherwise thepptr() - pbase() characters beginning atpbase(), followed by

the empty sequence iftraits::eq_int_type(c, traits::eof()) returnstrue, otherwise the sequence consisting of c.

4

#

Preconditions: Every overriding definition of this virtual function obeys the following constraints:

  • (4.1)

    The effect of consuming a character on the associated output sequence is specified.270

  • (4.2)

    Letr be the number of characters in the pending sequence not consumed. Ifr is nonzero thenpbase() andpptr() are set so that:pptr() - pbase() == r and the r characters starting atpbase() are the associated output stream. In case r is zero (all characters of the pending sequence have been consumed) then eitherpbase() is set tonullptr, orpbase() andpptr() are both set to the same non-null value.

  • (4.3)

    The function may fail if either appending some character to the associated output stream fails or if it is unable to establishpbase() andpptr() according to the above rules.

5

#

Returns: traits::eof() or throws an exception if the function fails.

Otherwise, returns some value other thantraits::eof() to indicate success.271

6

#

Default behavior: Returnstraits::eof().

7

#

Remarks: The member functionssputc() andsputn() call this function in case that no room can be found in the put buffer enough to accommodate the argument character sequence.

270)270)

That is, for each class derived from a specialization ofbasic_streambuf in this Clause ([stringbuf], [filebuf]), a specification of how consuming a character effects the associated output sequence is given.

There is no requirement on a program-defined class.

271)271)

Typically,overflow returns c to indicate success, except whentraits::eq_int_type(c, traits::eof()) returnstrue, in which case it returnstraits::not_eof(c).