In addition to the comments, here's a little demo program that shows two ways of interacting with an asio::streambuf
.
One way wraps the streambuf in i/o streams, the other uses direct access with prepare/commit and data/consume.
#include <boost/asio.hpp>
#include <iostream>
#include <string>
#include <algorithm>
#include <memory>
namespace asio = boost::asio;
void direct_insert(asio::streambuf& sb, std::string const& data)
{
auto size = data.size();
auto buffer = sb.prepare(size);
std::copy(begin(data), end(data), asio::buffer_cast<char*>(buffer));
sb.commit(size);
}
void stream_insert(asio::streambuf& sb, std::string const& data)
{
std::ostream strm(std::addressof(sb));
strm << data;
}
std::string extract_istream(asio::streambuf& sb)
{
std::istream is(std::addressof(sb));
std::string line;
std::getline(is, line);
return line;
}
std::string extract_direct(asio::streambuf& sb)
{
auto buffer = sb.data();
auto first = asio::buffer_cast<const char*>(buffer);
auto bufsiz = asio::buffer_size(buffer);
auto last = first + bufsiz;
auto nlpos = std::find(first, last, '\n');
auto result = std::string(first, nlpos);
auto to_consume = std::min(std::size_t(std::distance(first, nlpos) + 1), bufsiz);
sb.consume(to_consume);
return result;
}
int main()
{
asio::streambuf buf;
direct_insert(buf, "The cat sat on the mat\n");
stream_insert(buf, "The cat sat on the mat\n");
auto s1 = extract_direct(buf);
auto s2 = extract_istream(buf);
std::cout << s1 << "\n" << s2 << "\n";
}
commit()
andconsume()
functions correctly? Can you show minimal, complete code that I can copy paste into my IDE to check? – Richard Hodgesn
characters. Since the source is an input iterator, it should be expected to be incremented (at most)n
times. en.cppreference.com/w/cpp/algorithm/copy_n – sehe