Sunday, 15 August 2010

c++ - rvalue reference to boost asio completion handler -


Promotion: It is possible to use and implement Level Semantics and Rivalue reference emulation in the ASIO, in C ++ 11 My effort is short, but I do not know what I am doing.

Class definition:

  Structure socket test: std :: enable_shared_from_this & lt; SocketTest & gt; {SocketTest (boost :: asio :: ip :: udp :: socket socket): socket_ (std :: move (socket)) {} template & lt; Typename handler & gt; Zero async_receive (dummy dummy, handler, and handler) {auto self (shared_from_this ()); Socket_.async_receive (boost :: asio :: buff_ (buf_), std :: bind ([this, self] (boost :: system :: error_code ec, std :: size_t bytes_transferred, handler and moved handler) {moved_handler (Ec, bytes_transferred);}, std :: move (handler)); } Std :: array & lt; Char, max_length & gt; Buf_; Boost :: asio :: ip :: udp :: socket socket_; };  

Example of calling the class:

  socket test socket_stest (std :: move (s)); Socket_test.async_receive (dummy ()), [] (boost :: system :: error_code ec, std :: size_t bytes_transferred) {// very good stuff done!});  

It looks like handler copy works correctly in the below version, but I am interested in learning to avoid this copy:

  struct SocketTest: std:: enable_shared_from_this & lt; SocketTest & gt; {SocketTest (boost :: asio :: ip :: udp :: socket socket): socket_ (std :: move (socket)) {} template & lt; Typename handler & gt; Zero async_receive (dummy dummy, handler handler) {auto_host_from_this ()); Socket_.async_receive (boost :: asio :: buff_ (buf_), [this, self, handler] (boost :: system :: error_code ec, std :: size_t bytes_transferred) {handler (ec, bytes_transferred);}); } Std :: array & lt; Char, max_length & gt; Buf_; Boost :: asio :: ip :: udp :: socket socket_; };   

You call bind Rivalue refers to the object to be executed, because when dam saves its logic, it is no longer a rivalry. In your case you can accept it by handler (And this is the last place where the handler is stored). You can look for more information.

In addition, you bound :

  socket_.async_receive (boost :: asio:: buffer (buf_), std :: bind ( [This, self] (boost :: system :: error_code ec, std :: size_t bytes_transferred, handler moved _handler // going through value ^^^^^^^) {moved_handler (Ec, bytes_transferred);} , Std :: placeholders :: _ 1, std :: placeholder :: _ 2, std :: move (handler)); // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ / code> 

No comments:

Post a Comment