views:

50

answers:

1

I have a project that requires me to insert a filter into a stream so that outgoing data will be modified according to the filter. After some research, it seems that what I want to do is create a filtered_streambuf like this:

template <class StreamBuf>
class filtered_streambuf: public StreamBuf
{ ... }

And then insert a filtered_streambuf<> into whichever stream I need to be filtered. My problem is that I don't know what invariants I need to maintain while filtering a stream, in order to ensure that

  • Derived classes can work as expected. In particular, I may find I have filtered_streambufs built over other filtered_streambufs.
  • All the various stream inserters, extractors and manipulators work as expected.

The trouble is that I just can't seem to work out what the minimal interface is that I need to supply in order to guarantee that an iostream will have what it needs to work correctly.

In particular, do I need to fake the movement of the protected pointer variables, or not? Do I need a fake data buffer, or not? Can I just override the public functions, rewriting them in terms of the base streambuf, or is that too simplistic?

+1  A: 

Boost.Iostreams may be useful to you.

From the documentation:

Boost.Iostreams has three aims:

  • To make it easy to create standard C++ streams and stream buffers for accessing new Sources and Sinks.
  • To provide a framework for defining Filters and attaching them to standard streams and stream buffers.
  • To provide a collection of ready-to-use Filters, Sources and Sinks.

I've barely used that libary myself, so I can't comment any further.

Emile Cormier
Yes, I've seen that Boost has a facility for defining filters, and I may end up transitioning to it later, but now that I've uncovered a major gap in my knowledge, I'd like to fill it!
swestrup
Okay, I'm accepting this answer. It seems that an actual solution to the problem of filtering ostreams is a LOT harder than I had hoped and my best clues I've gotten about how to do it have come from reverse-engineering the boost code. I am going to transition to the boost libraries at my first opportunity.
swestrup