Search code examples
c++templatesboostboost-asio

Boost asio ConstBufferSequence - c++ Templates


I am hoping for some guidance regarding C++ templates. I have been using the boost::asio library for communication over TCP. Thus far, I have been using storage containers built into the boost::asio library. For instance:

boost::array<char, 128> buf;
boost::system::error_code error;
size_t len = socket.read_some(boost::asio::buffer(buf), error);

To read from a socket, I simply wrap a buffer of type boost::asio::buffer around my boost::array object. This works fine, but now I'd like to work in the reverse direction. That is, I'd like to write back into the socket, pulling data from some custom storage classes I already have. My question is, how do I make sense of the template type requirements required to be wrappable by boost::asio::buffer, or more generally, the parameter type specified by:

template<typename ConstBufferSequence>
std::size_t send(   
   const ConstBufferSequence & buffers
);

The API lists the requirements of a ConstBufferSequence, but I can't make heads or tails of this. Can somebody help me understand? What methods would the type I want to pass to the "send" function need to expose?


Solution

  • boost::asio::buffer returns objects implementing the ConstBufferSequence and MutableBufferSequence concepts; it doesn't expect you to implement them. The concrete types you're allowed to pass to buffer are listed here.