1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Read handler requirements</title> 5<link rel="stylesheet" href="../../../../doc/src/boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../boost_asio.html" title="Boost.Asio"> 8<link rel="up" href="../reference.html" title="Reference"> 9<link rel="prev" href="RangeConnectHandler.html" title="Range connect handler requirements"> 10<link rel="next" href="Receiver.html" title="Receiver concepts"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../boost.png"></td> 15<td align="center"><a href="../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="RangeConnectHandler.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../reference.html"><img src="../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../boost_asio.html"><img src="../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="Receiver.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h3 class="title"> 27<a name="boost_asio.reference.ReadHandler"></a><a class="link" href="ReadHandler.html" title="Read handler requirements">Read handler requirements</a> 28</h3></div></div></div> 29<p> 30 A read handler must meet the requirements for a <a class="link" href="Handler.html" title="Handlers">handler</a>. 31 A value <code class="computeroutput">h</code> of a read handler class should work correctly in the 32 expression <code class="computeroutput">h(ec, s)</code>, where <code class="computeroutput">ec</code> is an lvalue of type 33 <code class="computeroutput">const error_code</code> and <code class="computeroutput">s</code> is an lvalue of type <code class="computeroutput">const 34 size_t</code>. 35 </p> 36<h5> 37<a name="boost_asio.reference.ReadHandler.h0"></a> 38 <span class="phrase"><a name="boost_asio.reference.ReadHandler.examples"></a></span><a class="link" href="ReadHandler.html#boost_asio.reference.ReadHandler.examples">Examples</a> 39 </h5> 40<p> 41 A free function as a read handler: 42 </p> 43<pre class="programlisting">void read_handler( 44 const boost::system::error_code& ec, 45 std::size_t bytes_transferred) 46{ 47 ... 48} 49</pre> 50<p> 51 A read handler function object: 52 </p> 53<pre class="programlisting">struct read_handler 54{ 55 ... 56 void operator()( 57 const boost::system::error_code& ec, 58 std::size_t bytes_transferred) 59 { 60 ... 61 } 62 ... 63}; 64</pre> 65<p> 66 A lambda as a read handler: 67 </p> 68<pre class="programlisting">socket.async_read(... 69 [](const boost::system::error_code& ec, 70 std::size_t bytes_transferred) 71 { 72 ... 73 }); 74</pre> 75<p> 76 A non-static class member function adapted to a read handler using <code class="computeroutput">std::bind()</code>: 77 </p> 78<pre class="programlisting">void my_class::read_handler( 79 const boost::system::error_code& ec, 80 std::size_t bytes_transferred) 81{ 82 ... 83} 84... 85socket.async_read(..., 86 std::bind(&my_class::read_handler, 87 this, std::placeholders::_1, 88 std::placeholders::_2)); 89</pre> 90<p> 91 A non-static class member function adapted to a read handler using <code class="computeroutput">boost::bind()</code>: 92 </p> 93<pre class="programlisting">void my_class::read_handler( 94 const boost::system::error_code& ec, 95 std::size_t bytes_transferred) 96{ 97 ... 98} 99... 100socket.async_read(..., 101 boost::bind(&my_class::read_handler, 102 this, boost::asio::placeholders::error, 103 boost::asio::placeholders::bytes_transferred)); 104</pre> 105</div> 106<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 107<td align="left"></td> 108<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M. 109 Kohlhoff<p> 110 Distributed under the Boost Software License, Version 1.0. (See accompanying 111 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 112 </p> 113</div></td> 114</tr></table> 115<hr> 116<div class="spirit-nav"> 117<a accesskey="p" href="RangeConnectHandler.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../reference.html"><img src="../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../boost_asio.html"><img src="../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="Receiver.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a> 118</div> 119</body> 120</html> 121