Boost logo

Boost :

From: Jonathan Turkanis (technews_at_[hidden])
Date: 2005-03-01 13:46:28


Brian Braatz wrote:

>> On Behalf Of Jonathan Turkanis

>> The main point of the thread was to show how to write a sink with a
>> built-in line-ending filter, so the user doesn't always have to add one
> > explicitly.
>>
>> Jonathan

> [Brian Braatz Writes:]
> Yes. (sorry if I got off base)- the question relevant to boost \
> iostreams was this-

You weren't off base -- I was summarizing the thread for Reid Sweatman, who
asked what the thread was about.

> Given a device which has different line termination than \n, how
> do you handle it in such a way that allows for that device to be able
> to interpret std::endl so it works as the user would expect. (and so
> the code you write using the sink makes sense to the user)
>
> I happen to be using win32's outputdebugstring, but the same
> issue applies for "printing" to a windows edit control. The same issue
> might also apply to a daisy wheel printer. And, personally, I think
> boost should support daisy wheel printers (grin)....

I think this accurately describes the question -- now here's my question: are
you satisfied with my answer? I had to correct my first answer, so let me
restate it for clarity:

1. Implement the device as if it had no special line-endings requirements. If
the final device will be called xxx, you might call this intermediate class
xxx_impl.

2. Your final device should have a member yyy of type detail::chain<mode>. If
you want to avoid details, a member of type filtering_stream<mode> or
filtering_streambuf<mode> will do, but you have to wrap them so they are
copy-constructible, perhaps using share_ptr< filtering_streambuf<mode> >.

3. In the constructor of your device, push a newline filter onto yyy's chain,
then push an instance of xxx_impl.

4. implement the i/o functions (read, write, seek, ...) by delegation to yyy.

For example:

       struct win32_debug_sink_impl : boost::iostreams::sink { ... };
       struct win32_debug_sink : boost::iostreams::sink {
              win32_debug_sink()
              {
                   using namespace boost::iostreams;
                   chain_.push(newline_filter(newline::windows));
                   chain_.push(win32_debug_sink_impl);
              }
              void write(const char* s, std::streamsize n)
              {
                   chain_.write(x, n);
              }
              boost::iostreams::detail::chain<output> chain_;
       };

Best,
Jonathan


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