< cpp‎ | io
Revision as of 14:51, 11 January 2013 by (Talk)

Defined in header <streambuf>

    class CharT,
    class Traits = std::char_traits<CharT>

> class basic_streambuf;

The class basic_streambuf controls input and output to a character sequence. It includes and provides access to

1) The controlled character sequence, also called the buffer, which may contain input sequence (also called get area) for buffering the input operations and/or output sequence (also called put area) for buffering the output operations.
2) The associated character sequence, also called source (for input) or sink (for output). This may be an entity that is accessed through OS API (file, TCP socket, serial port, other character device), or it may be an object (std::vector, array, string literal), that can be interpreted as a character source or sink.

The I/O stream objects std::basic_istream and std::basic_ostream, as well as all objects derived from them (std::ofstream, std::stringstream, etc), are implemented entirely in terms of std::basic_streambuf.

The controlled character sequence is an array of CharT which, at all times, represents a subsequence, or a "window" into the associated character sequence. Its state is described by three pointers:

1) The beginning pointer, always points at the lowest element of the buffer
2) The next pointer, points at the element that is the next candidate for reading or writing
3) The end pointer, points one past the end of the buffer.

A basic_streambuf object may support input (in which case the buffer described by the beginning, next, and end pointers is called get area), output (put area), or input and output simultaneously. In latter case, six pointers are tracked, which may all point to elements of the same character array or two individual arrays.

If the next pointer is less than the end pointer in the put area, a write position is available. The next pointer can be dereferenced and assigned to.

If the next pointer is less than the end pointer in the get area, a read position is available. The next pointer can be dereferenced and read from.

If the next pointer is greater than the beginning pointer in a get area, a putback position is available, and the next pointer may be decremented, dereferenced, and assigned to, in order to put a character back into the get area.

The character representation and encoding in the controlled sequence may be different from the character representations in the associated sequence, in which case a std::codecvt locale facet is typically used to perform the conversion. Common examples are UTF-8 (or other multibyte) files accessed through std::wfstream objects: the controlled sequence consists of wchar_t characters, but the associated sequence consists of bytes.

Typical implementation of the std::basic_streambuf base class holds only the six CharT* pointers and a copy of std::locale as data members. In addition, implementations may keep cached copies of locale facets, which are invalidated whenever imbue() is called. The concrete buffers such as std::basic_filebuf or std::basic_stringbuf are derived from std::basic_streambuf.

Two convenience typedefs are provided by the standard library

Defined in header <streambuf>
Type Definition
streambuf basic_streambuf<char>
wstreambuf basic_streambuf<wchar_t>


Member types

Template:cpp/io/dcl list char typeTemplate:cpp/io/dcl list traits typeTemplate:cpp/io/dcl list int typeTemplate:cpp/io/dcl list pos typeTemplate:cpp/io/dcl list off type
Member type Definition

Member functions

Template:cpp/io/basic streambuf/dcl list ~basic streambufTemplate:cpp/io/basic streambuf/dcl list pubimbueTemplate:cpp/io/basic streambuf/dcl list getlocTemplate:cpp/io/basic streambuf/dcl list pubsetbufTemplate:cpp/io/basic streambuf/dcl list pubseekoffTemplate:cpp/io/basic streambuf/dcl list pubseekposTemplate:cpp/io/basic streambuf/dcl list pubsyncTemplate:cpp/io/basic streambuf/dcl list in availTemplate:cpp/io/basic streambuf/dcl list snextcTemplate:cpp/io/basic streambuf/dcl list sbumpcTemplate:cpp/io/basic streambuf/dcl list stosscTemplate:cpp/io/basic streambuf/dcl list sgetcTemplate:cpp/io/basic streambuf/dcl list sgetnTemplate:cpp/io/basic streambuf/dcl list sputcTemplate:cpp/io/basic streambuf/dcl list sputnTemplate:cpp/io/basic streambuf/dcl list sputbackcTemplate:cpp/io/basic streambuf/dcl list sungetcTemplate:cpp/io/basic streambuf/dcl list basic streambufTemplate:cpp/io/basic streambuf/dcl list operator=Template:cpp/io/basic streambuf/dcl list swapTemplate:cpp/io/basic streambuf/dcl list imbueTemplate:cpp/io/basic streambuf/dcl list setbufTemplate:cpp/io/basic streambuf/dcl list seekoffTemplate:cpp/io/basic streambuf/dcl list seekposTemplate:cpp/io/basic streambuf/dcl list syncTemplate:cpp/io/basic streambuf/dcl list showmanycTemplate:cpp/io/basic streambuf/dcl list underflowTemplate:cpp/io/basic streambuf/dcl list uflowTemplate:cpp/io/basic streambuf/dcl list xsgetnTemplate:cpp/io/basic streambuf/dcl list gptrTemplate:cpp/io/basic streambuf/dcl list gbumpTemplate:cpp/io/basic streambuf/dcl list setgTemplate:cpp/io/basic streambuf/dcl list xsputnTemplate:cpp/io/basic streambuf/dcl list overflowTemplate:cpp/io/basic streambuf/dcl list pptrTemplate:cpp/io/basic streambuf/dcl list pbumpTemplate:cpp/io/basic streambuf/dcl list setpTemplate:cpp/io/basic streambuf/dcl list pbackfail
Get area
Put area

Protected member functions

Get area
Put area