Boost logo

Boost Users :

Subject: [Boost-users] [iostreams] Can you flush yourself from within a stream?
From: Adam Nielsen (a.nielsen_at_[hidden])
Date: 2009-10-19 06:42:34


Hi all,

I have a class used as a Boost iostream:

  boost::iostreams::stream<my_stream> ms;

The my_stream class implements a couple of extra methods (one example being to
insert a block of data in the middle of the stream) however the stream does
not get flushed before the call to these extra functions, which results in
corrupted data:

  ms.write("1234", 4);
  ms.seekp(-2, std::ios::cur);
  ms->insert(2); // my_stream method

This should result in "12__34", but unfortunately because of buffering, the
insert() ends up happening before the write(), leaving you with just "1234".

The problem is solved by flushing the cache all over the place:

  ms.write("1234", 4);
  ms.flush();
  ms.seekp(-2, std::ios::cur);
  ms->insert(2);
  ms.flush();

However this isn't really ideal. I would rather the insert() function was
able to flush itself before manipulating the underlying stream.

Is this possible? Or am I going about this in entirely the wrong way?? ;-)

Many thanks,
Adam.

--
class my_stream {
  public:
    typedef char char_type;
    typedef io::seekable_device_tag category;
...

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net