Pregunta

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?

¿Fue útil?

Solución

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.

Otros consejos

std::iostream inherits from both std::istream and std::ostream, but both of those inherit virtually from std::ios, which contains the buffer. Since they both inherit virtually from std::ios, there is only one std::ios base of std::iostream, and as such, only one internal buffer.

From the C++11 Feb 2011 Draft:

§ 27.6.3

The class template basic_streambuf serves as an abstract base class for deriving various stream buffers whose objects each control two character sequences:
— a character input sequence;
— a character output sequence.

§ 27.7.2

namespace std {  
template <class charT, class traits = char_traits<charT> >  
class basic_istream : virtual public basic_ios<charT,traits> {

§ 27.7.3

namespace std {  
template <class charT, class traits = char_traits<charT> >  
class basic_ostream : virtual public basic_ios<charT,traits> {
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top