Hi there,
First of all thank you to Jonathan Turkanis for the iostreams library –
it has been very useful.
I am using a filter to calculate a hash on data as it is sent to an
ostream, using symmetric_filter. The problem is that even when I flush the
filtering_ostream, the extra bytes in the symmetric_filter destination buffer
aren’t flushed out to the sink until the filtering_ostream goes out of
scope (or reset() or pop() is called).
I found the following in the FAQ:
“Note, however, that there is no guarantee that all data written
to a filtering_stream
will be forwarded to the final Sink until the stream is closed, unless all the
Filters in the underlying chain
are Flushable.”
My questions:
1) How
does one make a symmetric_filter Flushable?
2) Is
this the best way to do this sort of on-the-fly hash calculation?
I’ve included example code below.
Thank you in advance for any advice!
Darren
#include "boost/iostreams/filtering_stream.hpp"
#include "boost/iostreams/filter/symmetric.hpp"
#include <iostream>
using namespace std;
class MySymmetricFilter
{
public:
typedef char char_type;
bool filter(const char*& src_begin,
const char* src_end,
char*& dest_begin, char* dest_end, bool flush)
{
// just copy
everything
for (;
src_begin!=src_end && dest_begin!=dest_end
; ++src_begin, ++dest_begin)
*dest_begin = *src_begin;
// do some hash calculation
on bytes copied
return false;
}
void close() {}
};
struct MyFilter
: public
boost::iostreams::symmetric_filter<MySymmetricFilter>
{
typedef boost::iostreams::symmetric_filter<MySymmetricFilter>
base_type;
MyFilter(int bufferSize) :
base_type(bufferSize) {}
};
int main()
{
boost::iostreams::filtering_ostream fos;
fos.push(MyFilter(40)); // buffer size ==
40
fos.push(cout);
fos << "The quick brown fox
jumps over the lazy dog" << flush; // length == 43
cout << "123456789 ";
flush(fos); // no effect?
return 0;
// prints “The quick brown fox
jumps over the lazy 123456789 dog”
// last 3 bytes “dog” are
printed when fos goes out of scope
}
Darren Kessner
Scientific Programmer
Spielberg Family Center for Applied Proteomics
Cedars-Sinai Medical Center