Apache C++ Standard Library Reference Guide

## basic_streambuf

Library: Input/output

    Does not inherit

- Local Index
- Summary
- Synopsis
- Description
- Interface
- Member Types
- Nonmember Types
- Constructor
- Destructor
- Public Member Functions
- Protected Member Functions
- See Also
- Standards Conformance

### Local Index

#### Members

basic_streambuf() char_type eback() egptr() epptr() gbump() getloc() gptr() imbue() int_type in_avail() | off_type overflow() pbackfail() pbase() pbump() pos_type pptr() pubimbue() pubseekoff() pubseekpos() pubsetbuf() | pubsync() sbumpc() seekoff() seekpos() setbuf() setg() setp() sgetc() sgetn() showmanyc() snextc() | sputbackc() sputc() sputn() sungetc() sync() traits_type uflow() underflow() xsgetn() xsputn() ~basic_streambuf()

#### Non-Members

streambuf | wstreambuf |

### Summary

Abstract base class for deriving various stream buffers to facilitate control of character sequences

### Synopsis

    #include <streambuf>

    namespace std {
      template<class charT, class traits = char_traits<charT> >
      class basic_streambuf;
    }

### Description

The class template basic_streambuf serves as an abstract base class for deriving various stream buffers to facilitate control of character sequences such as:

- a character input sequence

- a character output sequence

Each sequence is associated with three pointers (as described below), which, if non-null, all point into the same `charT` array object. The array object represents, at any moment, a segment of characters from the sequence. Operations performed on a sequence alter the values pointed to by these pointers, perform reads and writes directly to or from associated sequences, and alter the stream position and conversion state as needed to maintain this segment to sequence relationship. The three pointers are:

- the beginning pointer, or lowest element address in the array

- the next pointer, or next element address that is a current candidate for reading or writing

- the end pointer, or first element address beyond the end of the array

Stream buffers can impose various constraints on the sequences they control, including:

- The controlled input sequence may be unreadable.

- The controlled output sequence may be unwriteable.

- The controlled sequences can be associated with the contents of other representations for character sequences, such as external files.

- The controlled sequences can impose limitations on how the program can read characters from a sequence, write characters to a sequence, put characters back into an input sequence, or alter the stream position.

### Interface

    namespace std {

      template<class charT, class traits = char_traits<charT> >
      class basic_streambuf
      {

       public:

       typedef charT char_type;
       typedef traits traits_type;

       typedef typename traits::int_type int_type;
       typedef typename traits::pos_type pos_type;
       typedef typename traits::off_type off_type;

       virtual ~basic_streambuf();

       locale pubimbue(const locale& loc);
       locale getloc() const;

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

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

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

       int pubsync();

       streamsize in_avail();
       int_type snextc();
       int_type sbumpc();
       int_type sgetc();
       streamsize sgetn(char_type *, streamsize);

       int_type sputbackc(char_type);
       int sungetc();

       int_type sputc(char_type);
       streamsize sputn(const char_type *, streamsize);

      protected:

       basic_streambuf();

       char_type *eback() const;
       char_type *gptr()  const;
       char_type *egptr() const;

       void gbump(int n);
       void setg(char_type *gbeg_arg,char_type *gnext_arg,
                 char_type *gend_arg);

       char_type *pbase() const;
       char_type *pptr() const;
       char_type *epptr() const;
       void pbump(int n);
       void setp(char_type *,char_type *);

       virtual void imbue(const locale& loc);

       virtual basic_streambuf*
       setbuf(char_type*, streamsize);

       virtual pos_type seekoff(off_type,
                                ios_base::seekdir,
                                ios_base::openmode =
                                ios_base::in | ios_base::out);

       virtual pos_type seekpos(pos_type,
                                ios_base::openmode =
                                ios_base::in | ios_base::out);
       virtual int sync();

       virtual int showmanyc();
       virtual streamsize xsgetn(char_type *, streamsize);

       virtual int_type underflow();
       virtual int_type uflow();

       virtual int_type pbackfail(int_type = traits::eof());
       virtual streamsize xsputn(const char_type *,
                                 streamsize);
       virtual int_type overflow(int_type = traits::eof());

      };
    }

### Member Types

    char_type

The type `char_type` is a synonym for the template parameter `charT`.

    int_type

The type `int_type` is a synonym of type `traits_type::in_type`.

    off_type

The type`off_type` is a synonym of type `traits_type::off_type`.

    pos_type

The type `pos_type` is a synonym of type `traits_type::pos_type`.

    traits_type

The type `traits_type` is a synonym for the template parameter `traits`.

### Nonmember Types

    streambuf

The type `streambuf` is a specialization of class template basic_streambuf on type `char`:

`typedef basic_streambuf<char> streambuf;`

    wstreambuf

The type `wstreambuf` is a specialization of class template basic_streambuf on type `wchar_t`:

`typedef basic_streambuf<wchar_t> wstreambuf;`

### Constructor

    basic_streambuf();

Constructs an object of class basic_streambuf. Initializes all its pointer member objects to null pointers. The `getloc()` member function returns the value of `locale::locale()` when called on a newly constructed object.

### Destructor

    virtual ~basic_streambuf();

Destroys an object of class basic_streambuf.

### Public Member Functions

    locale
    getloc() const;

If `pubimbue()` has ever been called, returns the last value of `loc` supplied. Otherwise, returns the default (global) locale `locale::locale()` in effect at the time of construction.

    streamsize
    in_avail();

If a read position is available, returns the number of available characters in the input sequence. Otherwise, calls the protected function `showmanyc()` .

    locale
    pubimbue(const locale& loc);

Calls the protected function `imbue(loc)` and returns its result.

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

Calls the protected function `seekoff(off, way, which)` and returns its result.

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

Calls the protected function `seekpos(sp,which)` and returns its result.

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

Calls the protected function `setbuf(s,n)` and returns its result.

    int
    pubsync();

Calls the protected function `sync()` and returns its result.

    int_type
    sbumpc();

If the input sequence read position is not available, calls the function `uflow()` and returns its result. Otherwise, it returns `traits_type::to_int_type(*gptr())` and increments the next pointer for the input sequence.

    int_type
    sgetc();

If the input sequence read position is not available, calls the protected function `underflow()` and returns its result. Otherwise, it returns`traits_type::to_int_type(*gptr())`.

    streamsize
    sgetn(char_type* s, streamsize n);

Calls the protected function `xsgetn(s,n)` and returns its result.

    int_type
    snextc();

Calls the function `sbumpc()` and if it returns `traits_type::eof()`, returns `traits_type::eof()`. Otherwise, it calls the function `traits_type::to_int_type(*gptr())`.

    int_type
    sputbackc(char_type c);

If the input sequence putback position is not available or if `traits_type::eq(c,gptr() [-1])` returns `false`, calls the protected function `pbackfail(c)`. Otherwise, it decrements the next pointer for the input sequence and returns `traits_type::to_int_type (*gptr())`.

    int_type
    sputc(char_type c);

If the output sequence write position is not available, calls the protected function `overflow(traits_type::to_int_type(c))` and returns its result. Otherwise, it stores `c` at the next pointer for the output sequence, increments the pointer, and returns `traits_type::to_int_type (*gptr())`.

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

Calls the protected function `xsputn(s,n)` and returns its result.

    int_type
    sungetc();

If the input sequence putback position is not available, calls the protected function `pbackfail()` and returns its result. Otherwise, decrements the next pointer for the input sequence and returns `traits_type::to_int_type(*gptr())`.

### Protected Member Functions

    char_type*
    eback() const;

Returns the beginning pointer for the input sequence.

    char_type*
    egptr() const;

Returns the end pointer for the input sequence.

    char_type*
    epptr() const;

Returns the end pointer for the output sequence.

    void
    gbump(int n);

Advances the next pointer for the input sequence by `n`.

    char_type*
    gptr() const;

Returns the next pointer for the input sequence.

    void
    imbue(const locale&);

Changes any translations based on locale. The default behavior is to do nothing. This function has to be overloaded in the classes derived from basic_streambuf. The purpose of this function is to allow the derived class to be informed of changes in locale at the time they occur. The new imbued locale object is only used by the stream buffer; it does not affect the stream itself.

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

The member functions `sputc()` and `sputn()` call this function when not enough room can be found in the `put` buffer to accommodate the argument character sequence. The function returns `traits::eof()` if it fails to make more room available or if it fails to empty the buffer by writing the characters to their output device.

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

This function is to be called only when:

- `gptr()` is null

- `gptr() == eback()`, or

- `traits_type::eq(*gptr(),traits::to_char_type(c))` returns `false`.

If `c` is equal to `traits_type::eof()`, `gptr()` is moved back one position. Otherwise, `c` is prepended. The function returns `traits::eof()` to indicate failure.

    char_type*
    pbase() const;

Returns the beginning pointer for the output sequence.

    void
    pbump(int n);

Advances the next pointer for the output sequence by `n`.

    char_type*
    pptr() const;

Returns the next pointer for the output sequence.

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

Alters the stream positions within one or more of the controlled sequences in a way that is defined separately for each class derived from basic_streambuf. The default behavior is to return an object of type pos_type that stores an invalid stream position.

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

Alters the stream positions within one or more of the controlled sequences in a way that is defined separately for each class derived from basic_streambuf. The default behavior is to return an object of class pos_type that stores an invalid stream position.

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

Performs an operation that is defined separately for each class derived from basic_streambuf. The purpose of this function is to allow users to provide their own buffer or to resize the current buffer.

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

Sets up a private member for the following to be `true`:

`eback() == gbeg`, `gptr() == gnext, and egptr() == gend`

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

Sets up a private member for the following to be `true`:

       pbase() == pbeg, pptr() == pbeg and epptr() == pend

    streamsize
    showmanyc();

Returns the number of characters available in the internal buffer, or returns `-1`.

    int
    sync();

Synchronizes the controlled sequences with the internal buffer, in a way that is defined separately for each class derived from basic_streambuf. The default behavior is to do nothing. On failure, the return value is `-1`.

    int_type
    underflow();

The public members of basic_streambuf call this function only if `gptr()` is null or `gptr() >= egptr()`. This function returns the character pointed to by `gptr()`, if `gptr()` is not null and if `gptr() < egptr()`. Otherwise, the function tries to read characters into the buffer. If it fails, it returns `traits_type::eof()`.

    int_type
    uflow();

Calls `underflow()` and if `underflow()` returns `traits_type::eof()`, returns `traits_type::eof()`. Otherwise, does `gbump(1)` and returns the value of `*gptr()`.

    streamsize
    xsgetn(char_type* s, streamsize n);

Assigns up to `n` characters to successive elements of the array whose first element is designated by `s`. The characters are read from the input sequence. Assigning stops when either `n` characters have been assigned or a call to `sbumpc()` would return `traits_type::eof()`. The function returns the number of characters read.

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

Writes up to `n` characters to the output sequence. 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 to `sputc()` would return `traits_type::eof()`. The function returns the number of characters written.

### See Also

char_traits, basic_filebuf, basic_stringbuf, strstreambuf

### Standards Conformance

ISO/IEC 14882:1998 -- International Standard for Information Systems --Programming Language C++, Section 27.5.2