|
|
The positions of the put pointer after operations that store characters and position of the get pointer after operations that fetch characters are well defined by the sequence abstraction. But the location of the get pointer after stores, and the location of the put pointer after fetches is not. Most specializations of streambuf (i.e., classes derived from it) follow one of two patterns. Either the class is queuelike, which means that the put pointer and the get pointer are independent and moving one has no effect on the other. Or the class is filelike, which means that when one pointer moves the other is adjusted to point to the same place. So a filelike class behaves as if there were only one pointer. Other possibilities are logically possible, but do not seem to be as useful.
A queuelike streambuf, may be shared between two streams. For example:
strstreambuf b ;
ostream ins(&b) ;
istream extr(&b) ;
while ( ... ) {
ins << x ; ... ;
extr >> x ; ... ;
}
This example explicitly uses the
strstreambuf
class (declared in
strstream.h)
which is also used (implicitly) by the
istrstream
and
ostrstream
classes.
The
istream()
and
ostream()
constructors
require a
streambuf
argument.
They use that
streambuf
as a producer or consumer of characters.
The characters inserted into
ins
may later be extracted from
extr.
If an attempt is ever made to extract more characters than have been
inserted, the extraction will fail.
If more
Because of the dynamic allocation performed by strstreambufs the queue is unbounded, but there is a serious drawback. Space is not reclaimed until b is destroyed.