1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>async_compose</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="async_completion/result.html" title="async_completion::result"> 10<link rel="next" href="async_connect.html" title="async_connect"> 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="async_completion/result.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="async_connect.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.async_compose"></a><a class="link" href="async_compose.html" title="async_compose">async_compose</a> 28</h3></div></div></div> 29<p> 30 <a class="indexterm" name="boost_asio.indexterm.async_compose"></a> 31Launch an asynchronous operation 32 with a stateful implementation. 33 </p> 34<pre class="programlisting">template< 35 typename CompletionToken, 36 typename Signature, 37 typename Implementation, 38 typename... IoObjectsOrExecutors> 39<a class="link" href="asynchronous_operations.html#boost_asio.reference.asynchronous_operations.automatic_deduction_of_initiating_function_return_type"><span class="emphasis"><em>DEDUCED</em></span></a> async_compose( 40 Implementation && implementation, 41 CompletionToken & token, 42 IoObjectsOrExecutors &&... io_objects_or_executors); 43</pre> 44<p> 45 The async_compose function simplifies the implementation of composed asynchronous 46 operations automatically wrapping a stateful function object with a conforming 47 intermediate completion handler. 48 </p> 49<h5> 50<a name="boost_asio.reference.async_compose.h0"></a> 51 <span class="phrase"><a name="boost_asio.reference.async_compose.parameters"></a></span><a class="link" href="async_compose.html#boost_asio.reference.async_compose.parameters">Parameters</a> 52 </h5> 53<div class="variablelist"> 54<p class="title"><b></b></p> 55<dl class="variablelist"> 56<dt><span class="term">implementation</span></dt> 57<dd><p> 58 A function object that contains the implementation of the composed 59 asynchronous operation. The first argument to the function object is 60 a non-const reference to the enclosing intermediate completion handler. 61 The remaining arguments are any arguments that originate from the completion 62 handlers of any asynchronous operations performed by the implementation. 63 </p></dd> 64<dt><span class="term">token</span></dt> 65<dd><p> 66 The completion token. 67 </p></dd> 68<dt><span class="term">io_objects_or_executors</span></dt> 69<dd><p> 70 Zero or more I/O objects or I/O executors for which outstanding work 71 must be maintained. 72 </p></dd> 73</dl> 74</div> 75<h5> 76<a name="boost_asio.reference.async_compose.h1"></a> 77 <span class="phrase"><a name="boost_asio.reference.async_compose.example_"></a></span><a class="link" href="async_compose.html#boost_asio.reference.async_compose.example_">Example:</a> 78 </h5> 79<pre class="programlisting">struct async_echo_implementation 80{ 81 tcp::socket& socket_; 82 boost::asio::mutable_buffer buffer_; 83 enum { starting, reading, writing } state_; 84 85 template <typename Self> 86 void operator()(Self& self, 87 boost::system::error_code error = {}, 88 std::size_t n = 0) 89 { 90 switch (state_) 91 { 92 case starting: 93 state_ = reading; 94 socket_.async_read_some( 95 buffer_, std::move(self)); 96 break; 97 case reading: 98 if (error) 99 { 100 self.complete(error, 0); 101 } 102 else 103 { 104 state_ = writing; 105 boost::asio::async_write(socket_, buffer_, 106 boost::asio::transfer_exactly(n), 107 std::move(self)); 108 } 109 break; 110 case writing: 111 self.complete(error, n); 112 break; 113 } 114 } 115}; 116 117template <typename CompletionToken> 118auto async_echo(tcp::socket& socket, 119 boost::asio::mutable_buffer buffer, 120 CompletionToken&& token) -> 121 typename boost::asio::async_result< 122 typename std::decay<CompletionToken>::type, 123 void(boost::system::error_code, std::size_t)>::return_type 124{ 125 return boost::asio::async_compose<CompletionToken, 126 void(boost::system::error_code, std::size_t)>( 127 async_echo_implementation{socket, buffer, 128 async_echo_implementation::starting}, 129 token, socket); 130} 131</pre> 132<h5> 133<a name="boost_asio.reference.async_compose.h2"></a> 134 <span class="phrase"><a name="boost_asio.reference.async_compose.requirements"></a></span><a class="link" href="async_compose.html#boost_asio.reference.async_compose.requirements">Requirements</a> 135 </h5> 136<p> 137 <span class="emphasis"><em>Header: </em></span><code class="literal">boost/asio/compose.hpp</code> 138 </p> 139<p> 140 <span class="emphasis"><em>Convenience header: </em></span><code class="literal">boost/asio.hpp</code> 141 </p> 142</div> 143<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 144<td align="left"></td> 145<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M. 146 Kohlhoff<p> 147 Distributed under the Boost Software License, Version 1.0. (See accompanying 148 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>) 149 </p> 150</div></td> 151</tr></table> 152<hr> 153<div class="spirit-nav"> 154<a accesskey="p" href="async_completion/result.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="async_connect.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a> 155</div> 156</body> 157</html> 158