WriteHandler.qbk 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. [/
  2. / Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  3. /
  4. / Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. /]
  7. [section:WriteHandler Write handler requirements]
  8. A write handler must meet the requirements for a [link
  9. boost_asio.reference.Handler handler]. A value `h` of a write handler class
  10. should work correctly in the expression `h(ec, s)`, where `ec` is an lvalue of
  11. type `const error_code` and `s` is an lvalue of type `const size_t`.
  12. [heading Examples]
  13. A free function as a write handler:
  14. void write_handler(
  15. const boost::system::error_code& ec,
  16. std::size_t bytes_transferred)
  17. {
  18. ...
  19. }
  20. A write handler function object:
  21. struct write_handler
  22. {
  23. ...
  24. void operator()(
  25. const boost::system::error_code& ec,
  26. std::size_t bytes_transferred)
  27. {
  28. ...
  29. }
  30. ...
  31. };
  32. A lambda as a write handler:
  33. socket.async_write(...
  34. [](const boost::system::error_code& ec,
  35. std::size_t bytes_transferred)
  36. {
  37. ...
  38. });
  39. A non-static class member function adapted to a write handler using
  40. `std::bind()`:
  41. void my_class::write_handler(
  42. const boost::system::error_code& ec,
  43. std::size_t bytes_transferred)
  44. {
  45. ...
  46. }
  47. ...
  48. socket.async_write(...,
  49. std::bind(&my_class::write_handler,
  50. this, std::placeholders::_1,
  51. std::placeholders::_2));
  52. A non-static class member function adapted to a write handler using
  53. `boost::bind()`:
  54. void my_class::write_handler(
  55. const boost::system::error_code& ec,
  56. std::size_t bytes_transferred)
  57. {
  58. ...
  59. }
  60. ...
  61. socket.async_write(...,
  62. boost::bind(&my_class::write_handler,
  63. this, boost::asio::placeholders::error,
  64. boost::asio::placeholders::bytes_transferred));
  65. [endsect]