F
Fred Ma
Hello,
I posted previously under the thread:
How to break this up into streambuf/ostream
I've asked our library to get "C++ IOStreams and Locales..." by A.
Langer et al. Meantime, I've looked at the C++ standard (ISO/IEC
14882) from the web to get a grip on the relationship between ostream
methods and streambuf methods: flush(), operator<<(), sputc(),
sputn(), xsputn(), overflow(), sync(), and pubsync().
My problem is that I don't want to use a fixed size buffer in my
output streambuf derivative (mostreambuf), and I don't want the buffer
to flush automatically. I want the buffer to continue amassing data
until the user explicitly invokes ostream::flush(), possibly using
endl. This means mostreambuf will not use the pbase(), pptr(), or
epptr(). Instead, it will use a member ostringstream object "oss" to
buffer output. This way, the buffer can grow indefinitely as
required, until the user does a flush.
Before going further, I should point out that the standard does not
say anything about whether sync calls overload or vice-versa, so I
can't presume either.
My approach to realizing the above scheme is based on Josuttis's
example code in "The C++ Standard Library", as well as seeing that
flush() calls pubsync(), which calls the virtual (overloadable)
sync(). First, I set all 3 buffer pointers are set to NULL so that
sputc(c) calls overflow(), which I will overload so that c is written
to oss. Likewise, xsputn() will be overloaded to write to oss. I
will also overload sync() so that ostream::flush() causes the contents
of oss to be sent to its destination, followed by nulling oss to "".
Is this a reasonable approach?
Thanks for any feedback.
Fred
I posted previously under the thread:
How to break this up into streambuf/ostream
I've asked our library to get "C++ IOStreams and Locales..." by A.
Langer et al. Meantime, I've looked at the C++ standard (ISO/IEC
14882) from the web to get a grip on the relationship between ostream
methods and streambuf methods: flush(), operator<<(), sputc(),
sputn(), xsputn(), overflow(), sync(), and pubsync().
My problem is that I don't want to use a fixed size buffer in my
output streambuf derivative (mostreambuf), and I don't want the buffer
to flush automatically. I want the buffer to continue amassing data
until the user explicitly invokes ostream::flush(), possibly using
endl. This means mostreambuf will not use the pbase(), pptr(), or
epptr(). Instead, it will use a member ostringstream object "oss" to
buffer output. This way, the buffer can grow indefinitely as
required, until the user does a flush.
Before going further, I should point out that the standard does not
say anything about whether sync calls overload or vice-versa, so I
can't presume either.
My approach to realizing the above scheme is based on Josuttis's
example code in "The C++ Standard Library", as well as seeing that
flush() calls pubsync(), which calls the virtual (overloadable)
sync(). First, I set all 3 buffer pointers are set to NULL so that
sputc(c) calls overflow(), which I will overload so that c is written
to oss. Likewise, xsputn() will be overloaded to write to oss. I
will also overload sync() so that ostream::flush() causes the contents
of oss to be sent to its destination, followed by nulling oss to "".
Is this a reasonable approach?
Thanks for any feedback.
Fred