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 having interfaces resembling the functions fgetc, fputc, fread, fwrite and fseek from <stdio.h>. Each function takes a reference to a Device instead of a pointer to a FILE structure.[1] 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.[2]
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.
| Concept | Definition |
|---|---|
| InputFilter | Refinement of Filter with io_mode convertible to input |
| OutputFilter | Refinement of Filter with io_mode convertible to output |
| BidirectionalFilter | Refinement of Filter with io_mode convertible to bidirectional |
| SeekableFilter | Refinement of Filter with io_mode convertible to seekable |
| InputSeekableFilter | Refinement of Filter with io_mode convertible to input_seekable |
| OutputSeekableFilter | Refinement of Filter with io_mode convertible to output_seekable |
| BidirectionalSeekableFilter | Refinement of Filter with io_mode convertible to bidirectional_seekable |
| DualSeekableFilter | Refinement of Filter with io_mode convertible to dual_seekable |
| Character type | The type of the characters in the filtered sequences |
| Category | A type convertible to filter_tag and to a unique most-derived mode tag |
| Mode | The unique most-derived mode tag to which Category is convertible |
F | - A type which is a model of Filter |
D | - A type which is a model of Device, with the same character type as F and with i/o mode refining the i/o mode of F |
Ch | - The character type of F |
Tr | - std::char_traits<Ch> |
f | - Object of type F |
d | - Object of type D |
c | - Object of type Ch |
s1 | - Object of type Ch* |
s2 | - Object of type const Ch* |
n | - Object of type std::streamsize |
off | - Object of type std::streamoff |
way | - Object of type std::ios_base::seekdir |
which | - Object of type std::ios_base::openmode |
| Expression | Expression Type |
|---|---|
typename io_char<F>::type |
typename of the character type |
typename io_category<F>::type |
typename of the category |
| Expression | Expression Type | Category Precondition | Semantics |
|---|---|---|---|
|
Tr::int_type |
Convertible to input but not to multichar_tag
|
Returns the next character in the input sequence controlled by f, or Tr::eof() if the end of the sequence has been reached. The input sequence controlled by d may be accessed using boost::iostreams::read and boost::iostreams::putback.
|
|
|
Convertible to input and to multichar_tag
|
Reads up to characters from the input sequence controlled by f into the buffer s1, returning the number of characters read. Returning a value less than n indicates end-of-sequence. The input sequence controlled by d may be accessed using boost::iostreams::read and boost::iostreams::putback.
|
| Expression | Expression Type | Category Precondition | Semantics |
|---|---|---|---|
|
void |
Convertible to output but not to multichar_tag
|
Writes the character c to the output sequence controlled by f. The output sequence controlled by d may be accessed using boost::iostreams::write.
|
|
|
Convertible to output and to multichar_tag
|
Writes n characters from the buffer s to the output sequence controlled by f. The output sequence controlled by d may be accessed using boost::iostreams::write.
|
| Expression | Expression Type | Category Precondition | Semantics |
|---|---|---|---|
|
std::streamoff |
Convertible to seekable but not to direct_tag
|
Advances the read/write head by
The input sequence controlled by |
|
std::streamoff |
Convertible to dual_seekable or bidirectional_seekable but not to direct_tag
|
Repositions the read head (if
The input sequence controlled by
The result is undefined if |
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 immediately to begin processing new sequences of data.
See InputFilter, OutputFilter, BidirectionalFilter and SeekableFilter.
The concept Filter was inspired by the inserters and extractors of [Kanze].
[1]There are other differences between the Filter member functions and the corresponding functions from <stdio.h>. Consult the tables, above, for details.
[2][ISO], 24.3.1. See Tag Dispatching for a discussion.
Revised 20 May, 2004
© Copyright Jonathan Turkanis, 2004
Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)