Boost logo

Boost :

From: Ferdinand Prantl (prantlf_at_[hidden])
Date: 2008-07-09 11:12:51


Hello,

I would like to wrap an output stream buffer (std::streambuf) with
boost::iostreams::code_converter to be able to use it both raw and with
ont-the-fly encoding by the boost::iostreams::default_facet.

    namespace io = boost::iostreams;
    xxxstream stream;
    stream << "raw";
    io::stream<io::code_converter<xxxodevice> > wrapper(*stream.rdbuf());
    wrapper << L" and converted";
    stream << " and another raw";

Running the code writes "raw and another raw and converted" instead of
"raw and converted and another raw" to the stream. I found that first in
destructor of io::code_converter is the conversion buffer flushed causing
the output.

I could create a new instance of io::code_converter for every output and
always close it:

    namespace io = boost::iostreams;
    xxxstream stream;
    stream << "raw";
    {
        io::stream<io::code_converter<xxxodevice> > wrapper(*stream.rdbuf());
        wrapper << L" and converted";
    }
    stream << " and another raw";

But I would feel better being able to reuse the io::code_converter.
(Actually, I have it as a private field in a class now.) I would like to
make the io::code_converter flushable to be able to combine the output
more conveniently:

    namespace io = boost::iostreams;
    xxxstream stream;
    stream << "raw";
    io::stream<io::code_converter<xxxodevice> > wrapper(*stream.rdbuf());
    wrapper << L" and converted" << std::flush;
    stream << " and another raw";

I added the support to io::code_converter rather that inheriting from it.
What do you think about it? Would you accept it to boost or is it better
to make flushable my inherited class?

I am attaching a patch to code_converter.hpp from boost 1.35.0. I could
provide a test for it too.

Thank you,
Ferda




Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk