I am working on a basic_streambuf
to handle reading and writing from/to a Winsock socket. Just like basic_filebuf
, I am internally using a std::codecvt
object to convert bytes read from the underlying socket to the char type of the "socket streambuf" as well as to convert chars written to the socket streambuf to bytes that can be written to the underlying socket. In order to do this, I am finding that I need to maintain buffers for both the reading and writing functionality of the streambuf.
The logical issue that I am encountering is that a streambuf implementation is intended to be used for both reading and writing (the std::iostream
constructor takes a single pointer-to-streambuf), yet there is only one overridable member function that can be customized for setting an underlying char buffer: setbuf
. If I want to allow users of my socket streambuf template to set the underlying buffer, should setbuf
set the read buffer or the write one? Which option makes more sense?
The standard streams only have two specified behaviors with setbuf. The first is that setbuf(0,0) may have no effect, and the second is for basic_filebuf where, if setbuf(0,0) is called before any IO then the IO is unbuffered. Otherwise the results are implementation defined. So just do what makes sense for your implementation, and then document it.