I need to read data from boost::beast::flat_buffer
into a std::string
, then modify the string object and write the result back into boost::beast::flat_buffer
.
To convert boost::beast::flat_buffer
into a string, I suppose I can use the following code
boost::beast::flat_buffer buf;
// ...
std::string buf_data(boost::asio::buffers_begin(buf.data()), boost::asio::buffers_end(buf.data()));
buf_data = "new data";
// how to change boost::beast::flat_buffer buf;?
Then I need to modify flat_buffer
so that its contents are equivalent to the string buf_data
. How can this be implemented?
Generically, use asio::buffer_copy
:
#include <boost/asio.hpp>
#include <boost/beast/core.hpp>
namespace asio = boost::asio;
namespace beast = boost::beast;
int main() {
beast::flat_buffer buf;
// read into buf
std::string buf_data = beast::buffers_to_string(buf.data());
// something else
buf_data = "new data";
buf.clear();
asio::buffer_copy(buf.data(), asio::buffer(buf_data));
}