Boost logo

Boost Users :

From: Roman Perepelitsa (romka_at_[hidden])
Date: 2006-08-30 09:08:40


Hi.

I've implemented output stream using boost.iostreams library. It requires call
to flush in order to actually write data to sink. But I want to flush data to
sink automatically after I write any object with operator<<.

struct stream_sink : public boost::iostreams::sink
{
        stream_sink(std::ostream & strm) : strm_(strm) {}

        std::streamsize write(const char * s, std::streamsize n)
        {
                if (strm_.write(s, n))
                        return n;
                else
                        throw std::runtime_error("error");
        }

private:
        std::ostream & strm_;
};

std::stringstream s;
boost::iostreams::stream<stream_sink> strm(s);
strm << "abc";
strm.flush();
std::cout << "String: '" << s.str() << "'" << std::endl;

This test outputs "String: 'abc'" but if you comment strm.flush() then it
outputs "String: ''".

I want my stream to work like the stringstream does (it does not require call
to flush).

std::stringstream strm;
strm << "abc";
std::string s;
strm >> s;
std::cout << "String: '" << s << "'" << std::endl;

It outputs "String: 'abc'".

I need it to create stream which can be used to convert wide char data
(wchar_t *) to multibyte (char *). It can be used this way:

// wcstombsstream is wostream and istream
// "utf-8" is multibyte encoding
wcstombsstream strm("utf-8");
strm << L"abc";
std::string s;
strm >> s;

This code does not work at the moment because it requires to call strm.flush()
before reading from stream. Thus I need some auto flush facility :-)

Thank you for any help,
Roman Perepelitsa.


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