What is a protected class

streambuf-Objects implement the buffer abstraction mentioned in the section about sbufpubis described. The class streambuf itself contains only the basic elements for character manipulation; usually a streambuf derived class used. This section describes the interface the programmer needs to code a derived class.

The member functions of streambuf can - simplified - be divided into two groups. Manipulate the non-virtual functions streambufObjects as required in derived classes. The descriptions show implementation details that should not appear in a public interface. Due to the virtual functions, derived classes can be used as special cases of the class streambuf designed for the respective sources and destinations (of character transmissions).

The description of the virtual functions also contains the "compulsory tasks" of the virtual functions in the derived classes. If the virtual functions behave as expected, the public interface of thestreambuf ensured. Otherwise, the behavior of streambuf however show irregularities. In this case, a also behaves iostreamObject (or other program code) that was copied from the correct streambuf-Behavior depends, different than expected.

The following descriptions assume that:

  • sb of type streambuf * is.
  • ptr, b, eb, p, ep, g and eg of type char * are.
  • c a intSign (positive or EOF).
  • pos of type streampos (please refer sbufpub) is.
  • off of type streamoff is.
  • to you of type seekdir is.
  • Fashion a int-Number is that open_mode represents.

Constructors

streambuf ()

An empty buffer is created which corresponds to an empty sequence.

streambuf (char * b, int len)

An empty buffer is created and the reservation area is opened len Bytes starting at b, set.

The areas get, put and the reservation area

The protectedElements of streambuf represent the interface to the derived classes, which are structured in three areas (byte fields). The areas are and are managed jointly by base and derived classes get, put andReservation area (or buffer) called. The areas get and put are usually not identical, but can overlap the reservation area. The reservation area is primarily a resource from the capacity for the areas put and getcan be proven. The areas get and put are changed when characters are inserted and removed from the buffer, but the reservation area is generally fixed. The areas are covered by a series of char *Values ​​are defined. The buffer abstraction is described in terms of pointers that point between characters. The char *-Values ​​show, however charObjects (characters). Creating a relationship with the char *-Values ​​can be imagined that the pointer is directed in front of the byte to which it actually points.

Functions for examining pointers

char * ptr = sb → base ()

A pointer to the first byte of the reservation area is supplied. The area between sb-> base () and sb-> ebuf () is the reservation area.

char * ptr = sb-> eback ()

A pointer to the lower limit of sb-> gptr () will be delivered. The area betweensb-> eback () and sb-> gptr () is available for "pushing back" characters.

char * ptr = sb-> ebuf ()

A pointer to the first byte after the reservation area is supplied.

char * ptr = sb-> egptr ()

A pointer to the first byte after the area get will be delivered.

char * ptr = sb-> epptr ()

A pointer to the first byte after the area put will be delivered.

char * ptr = sb-> gptr ()

A pointer to the first byte of the area get will be delivered. The characters available are between sb-> gptr () and sb-> egptr (). The next character to be extracted is * (sb-> gptr ()), provided sb-> egptr () not less than or equal tosb-> gptr () is.

char * ptr = sb-> pbase ()

It becomes a pointer to the base of the putRange delivered. The characters betweensb-> pbase () and sb-> pptr () are stored in the buffer and have not yet been used.

char * ptr = sb-> pptr ()

It becomes a pointer to the first byte of the area put delivered. The area between sb-> pptr () and sb-> epptr () is the put-Area. Characters are stored here.

Functions for setting the pointers

Note

As an indication that an area (get, put or reservation area) does not exist, all associated pointers should be set to zero.

void sb-> setb (char * b, char * eb, int i)

base() and ebuf () will be on b or. eb set. The information i determines whether the
Area for automatic deletion is provided. If i is not equal to 0, will bwhen changing base by calling setb () or when calling the destructor for * sb deleted. If both b as well as eb 0, no reservation area is available. Is b not equal to 0, a reservation area is also available if eb less than b and the reservation area therefore has a length of 0.

void sb-> setp (char * p, char * ep)

pptr () will be on p, pbase () on p and epptr () on ep set.

void sb-> setg (char * eb, char * g, char * eg)

eback () will be on eb, gptr () on G and egptr () on eg set.

Other non-virtual member functions

int i = sb-> allocate ()

An attempt is being made to create a reservation area. If a reservation area already exists or if sb-> unbuffered () is not equal to 0, the function returnsallocate () the value 0 without any further operation. If the attempt to allocate memory is unsuccessful, returns allocate () the value EOF; otherwise (i.e. if the assignment was successful) the value 1 is returned. The functionallocate () is not used by any of the non-virtual member functions of streambuf called.

int i = sb-> blen ()

The size (in charUnits) of the current reservation area is delivered.

void dbp ()

The function writes buffer status information in EBCDIC format directly to the unit associated with file descriptor 1. This function was added for test purposes; there are no specifications for the format of the output.
The function is seen as part of the protected interface, since the information output is only meaningful in connection with the interface. The function is as public declared so that they can be used on any
Position can be called.

void sb-> gbump (int n)

gptr () is about the value n incremented, where n can be positive or negative. It is not checked whether the new value of gptr () is within the permitted limits.

void sb-> pbump (int n)

pptr () is about the value n incremented; n can be positive or negative. It is not checked whether the new value of pptr () is within the permitted limits.

void sb-> unbuffered (int i)
int i = sb → unbuffered ()

There is a private-Variable indicating the status of the buffering of sb describes.sb-> unbuffered (i) sets the value of this variable i, while sb-> unbuffered ()
supplies the current variable value. This status information is independent of the actual occupancy of the reservation area. The primary purpose of the variable is to determine whether the reservation area is automatically through allocate () has been reserved.

Virtual member functions

Virtual functions can be redefined in derived classes to reflect the behavior of streambufObjects to be specified more precisely. This section describes the target behavior of the virtual functions in the derived classes. The next section describes how these functions behave in the base class streambuf.

int i = sb-> doallocate ()

The function is called when allocate () determines that space is required. doallocate () becomes the call to setb () is used to provide a reservation area or, if this is not possible, to return the value EOF. The call is only made if sb-> unbuffered () and sb-> base () are equal to 0.

int i = overflow (int c)

This function "consumes" characters. If c is not the character EOF, the function must overflow () c either save or consume. The function is usually called when the area put is filled and an attempt was made to save another character. However, it can also be called in other cases. Usually the characters between pbase () and pptr () consumed, setp () is called for a new one put-Area to be created, and c is about the function sputc () saved when c! = EOF is. sb-> overflow () should return the value EOF to indicate an error, otherwise another return should be made.

int i = sb-> pbackfail (int c)

The function is called when eback () equal gptr () is and has tried the sign c "push back". If this situation can be handled correctly (for example, by repositioning to an external file) then pbackfail () the sign c deliver; otherwise, EOF should be returned.

streampos pos = sb-> seekoff (streamoff off, seekdir dir, int mode)

seekoff () is a public virtual member function. A detailed description can be found in the Section Section sbufpub. The get- and or put- Pointers are repositioned. This positioning is not supported by all derived classes.

streampos pos = sb-> seekpos (streampos pos, int mode)

seekpos () is a public virtual member function. A detailed description can be found in the section Section sbufpub. The get- and or put- Pointers are repositioned. This positioning is not supported by all derived classes.

streambuf * sb = sb-> setbuf (char * ptr, int len)

The field that at ptr begins and len byte is long, is offered as a reservation area. Usually a call is interpreted as a request sb without buffering if ptr or len are equal to 0. The derived class can - but does not have to - use this area. The request for the non-buffered status can also be accepted or ignored. setbuf () should sb deliver when the requirement is met; otherwise the value 0 should be returned.

int i = sb → sync ()

sync () is a public virtual member function. A detailed description can be found in the section sbufpub.

int i = sb → underflow ()

The function is used to provide characters. In this way, for example, a situation can be created in which an area get is present that is not empty. The call is made when characters are in the range get are available, the function should return the first available character. In the case of an empty area get should initially be a non-empty area get and the next character (the one in the area get should remain). If no more characters are available, the function should underflow () return the value EOF and an empty area get leave.

The standard definitions of the virtual functions

int i = sb → streambuf :: doallocate ()

The allocation of storage capacity to a reservation area is carried out by the operator new tries.

int i = sb-> streambuf :: overflow (int c)

streambuf :: overflow () should be treated as if the functional behavior was not defined. This means that derived classes should always define the function.

int i = sb-> streambuf :: pbackfail (int c)

If an error occurs it will EOF and if it succeeds it will c delivered.

streampos pos = sb-> streambuf :: seekpos (streampos pos, int mode)

It will sb-> seekoff (streamoff (pos), ios :: beg, mode) delivered. To define repositioning in a derived class, it is often only necessary to seekoff () to define and the inherited function streambuf :: seekpos () to use.

streampos pos = sb-> streambuf :: seekoff (streamoff off, seekdir dir, int mode)

EOF is delivered.

streambuf * sb = sb-> streambuf :: setbuf (char * ptr, int len)

The requirement is met if there is no reservation area.

int i = sb-> streambuf :: sync ()

The value 0 is returned if the range get is empty and there are no unused characters. Otherwise, EOF is returned.

int i = sb-> streambuf :: underflow ()

streambuf :: underflow () should be treated as if the functional behavior was
not defined. This means that the function should always be defined in derived classes.