boost::asio buffer questions, and boost::bind question
Hello: I had a couple of questions. I'm really confused about how the buffers in boost::asio work. I'm using async_read_until, and it wants a streambuf. so I looked at that, and it's a typedef for basic_streambuf. so I'm looking at how to set one of these up, and stackoverflow shows something like: boost::asio::streambuf streamBuffer; boost::asio::streambuf::mutable_buffers_type mutableBuffer = streamBuffer.prepare(max_length); so I'm curious what the mutable buffer is, and how it wraps around streambuf. I'm also curious how people generally handle buffers in their classes. I have a Session class, which will hold a buffer of data to be read, so that when I call HandleRead, it can just look at that buffer for the last line read in. Do I need to store a mutable_buffer and a streambuf? Or is the streambuf ok (and can I pull the data out of it somehow). Also, in that article it shows that it wraps the mutableBuffer in a boost::asio::buffer(what's the point in that?) Finally, my boost::bind question. I was curious how that worked out. When I use _1, _2 as placeholders, does the templating get substituted in somehow? Does it somehow construct an object that just has a () operator to take arguments? Also, if I wanted to write something to let me pull in the errors like placeholders, how do placeholders work/get translated out? Thanks, -- Take care, Ty http://tds-solutions.net The aspen project: a barebones light-weight mud engine: http://code.google.com/p/aspenmud He that will not reason is a bigot; he that cannot reason is a fool; he that dares not reason is a slave.
On Thu, Sep 06, 2012 at 09:17:38PM -0600, Littlefield, Tyler wrote:
Finally, my boost::bind question. I was curious how that worked out. When I use _1, _2 as placeholders, does the templating get substituted in somehow? Does it somehow construct an object that just has a () operator to take arguments? Also, if I wanted to write something to let me pull in the errors like placeholders, how do placeholders work/get translated out?
It indeed constructs a object of a secret callable class which stores all the concrete arguments by value inside. When invoked through operator (), the arguments, they're mapped to any corresponding placeheld slots in the original function and the stored values are passed as the parameter they're glued to. As for Asio's named placeholders "error" and "bytes_transferred", they're just aliases for Bind's _1 and _2. They just exist to make the bind site a bit more legible if you happen to use a callable-maker that can leverage Bind-style placeholders. In the end, Asio doesn't care if you use Boost.Bind, Boost.Phoenix, std::bind, Boost::Lambda, MakeAwesomeFastDelegate or some arbitrary other callable, as long as the callable satisfies the requirements put forth by ReadHandler [1], WriteHandler, etc. w.r.t. the argument types and amounts. [1] http://www.boost.org/doc/libs/1_51_0/doc/html/boost_asio/reference/ReadHandl... -- Lars Viklund | zao@acc.umu.se
participants (2)
-
Lars Viklund
-
Littlefield, Tyler