Reputation: 9278
The following code compiles fine for me, but given the somewhat complex design of ASIO buffers, I'm not certain it's correct. The intention is to allow the contents of the streambuf
to be given to an HTTP parser without creating an intermediate std::string
object, which is what other ASIO code examples seem to do.
boost::string_ref makeStringRef(const boost::asio::streambuf& streambuf)
{
auto&& bufferType = streambuf.data();
return {
boost::asio::buffer_cast<const char*>(bufferType),
boost::asio::buffer_size(bufferType)
};
}
Upvotes: 1
Views: 100
Reputation: 392893
I think this is, indeed, not correct, because the streambuf may have several non-contiguous regions.
So you need to copy anyways. Alternatively, just read into a fixed buffer instead. Of course, this requires you to know the maximum size ahead of time, or read in several steps.
By the way, by taking a
const&
you risk creating astring_ref
that refers to a temporary. Always try to be explicit about life time expectations.
Upvotes: 1