Filter

Description

A Filter operates on the character sequence or sequences controlled by a Device, providing access to a filtered input sequence, output sequence or both. Informally, when a Filter f is used in conjunction with a Device d, data read from the input sequence of d is processed by f before being returned to the user, data written to the output sequence of d is first processed by f, and repositioning operations are processed by f before being conveyed to d.

Filters are class types which define one or more member functions get, put, read, write and seek; each function takes a reference to a Device as its first argument. Whenever a Filter is used to perform an i/o operation, a reference to the Device being filtered is passed to the Filter as a function argument.

Each Filter type has an associated character type and category. The character type is the type of the characters in the input and output sequences. The category is a tag structure which the Iostreams library relies on to determine which operations the Filter type supports. Its function is similar to the iterator_category member of std::iterator_traits.[1]

There is one refinement of Filter for each of the eight modes, and each such refinement corresponds to a refinement of Device. In order to express this corresponce cleanly, it is helpful to include the requirements of the various refinements of Filter in the definition of Filter itself, qualified by category. The various refinements of Filter can then be characterized exactly by the following definitions. For convenience, the requirements of the four most common Filter refinements are also documented individually.

Valid Expressions / Semantics — Input

Returns the next character in the input sequence controlled by f, Tr::eof() if the end of the sequence has been reached or Tr::would_block() if input is temporarily unavilable because a call to d has produced fewer characters than requested. The input sequence controlled by d may be accessed using io::get, io::read and io::putback.

Reads up to n characters from the input sequence controlled by f into the buffer s1, returning the number of characters read or -1 to indicate end-of-sequence. A value less than n may be returned only at end-of-sequence or if input is temporarily unavilable because a call to d has produced fewer characters than requested. The input sequence controlled by d may be accessed using io::get, io::read and io::putback.

Valid Expressions / Semantics — Output

Attempts to writes the character c to the output sequence controlled by f, returning false if c cannot be consumed because a call to d has consumed fewer characters than requested. The output sequence controlled by d may be accessed using io::put and io::write.

Writes up to n characters from the buffer s2 to the output sequence controlled by d, returning the number of characters written. A value less than n may be returned only if a call to d has consumed fewer characters than requested. The output sequence controlled by d may be accessed using io::put and io::write.

Advances the read head (if which is std::ios_base::in), the write head (if which is std::ios_base::out) or both heads (if which is std::ios_base::in | std::ios_base::out) by off characters, returning the new position, where the offset is calculated from

The result is undefined if way is ios_base::cur and which is (std::ios_base::in | std::ios_base::out).

Exceptions

Errors which occur during the execution of get, put, read, write or seek are indicated by throwing exceptions. Reaching the end of the input sequence is not an error, but attempting to write past the end of the output sequence is.

After an exception is thrown, a Filter must be in a consistent state; further i/o operations may throw exceptions but must have well-defined behaviour. Furthermore, unless it is Closable, it must be ready to begin processing a new character sequence.