Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use boost async_write with a vector of boost const_buffers correctly?

I am having trouble to make this line here right:

boost::asio::async_write(serialPort, 
                         boost::asio::buffer(
                         boost::asio::buffer_cast<const void*>(vector_.front()),
                         boost::asio::buffer_size(vector_.front())))

vector_ holds a number of boost::asio::const_buffers

std::vector<boost::asio::const_buffer> vector_;

This stuff works, but I am quite sure that there is a way more elegant way to do this, and if not, I would like to here that from someone with a bit more experience.

So, can this solution be improved? If so, how?

like image 643
Jook Avatar asked Dec 20 '12 14:12

Jook


1 Answers

I think you're looking for this:

boost::asio::async_write(serialPort, make_buffer(vector_.front()) );

where make_buffer is defined as:

template<typename TBuffer>
boost::asio::buffer make_buffer(TBuffer & data)
{
   auto buf = boost::asio::buffer_cast<const void*>(data);
   auto size= boost::asio::buffer_size(data);
   return boost::asio::buffer(buf, size);
}

which is a generic function by the way.

like image 104
Nawaz Avatar answered Nov 04 '22 12:11

Nawaz