5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_ASIO_COMPOSE_HPP
12 #define BOOST_ASIO_COMPOSE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/asio/async_result.hpp>
21 #include <boost/asio/detail/push_options.hpp>
26 #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES) \
27 || defined(GENERATING_DOCUMENTATION)
29 /// Launch an asynchronous operation with a stateful implementation.
31 * The async_compose function simplifies the implementation of composed
32 * asynchronous operations automatically wrapping a stateful function object
33 * with a conforming intermediate completion handler.
35 * @param implementation A function object that contains the implementation of
36 * the composed asynchronous operation. The first argument to the function
37 * object is a non-const reference to the enclosing intermediate completion
38 * handler. The remaining arguments are any arguments that originate from the
39 * completion handlers of any asynchronous operations performed by the
42 * @param token The completion token.
44 * @param io_objects_or_executors Zero or more I/O objects or I/O executors for
45 * which outstanding work must be maintained.
49 * @code struct async_echo_implementation
51 * tcp::socket& socket_;
52 * boost::asio::mutable_buffer buffer_;
53 * enum { starting, reading, writing } state_;
55 * template <typename Self>
56 * void operator()(Self& self,
57 * boost::system::error_code error = {},
64 * socket_.async_read_some(
65 * buffer_, std::move(self));
70 * self.complete(error, 0);
75 * boost::asio::async_write(socket_, buffer_,
76 * boost::asio::transfer_exactly(n),
81 * self.complete(error, n);
87 * template <typename CompletionToken>
88 * auto async_echo(tcp::socket& socket,
89 * boost::asio::mutable_buffer buffer,
90 * CompletionToken&& token) ->
91 * typename boost::asio::async_result<
92 * typename std::decay<CompletionToken>::type,
93 * void(boost::system::error_code, std::size_t)>::return_type
95 * return boost::asio::async_compose<CompletionToken,
96 * void(boost::system::error_code, std::size_t)>(
97 * async_echo_implementation{socket, buffer,
98 * async_echo_implementation::starting},
102 template <typename CompletionToken, typename Signature,
103 typename Implementation, typename... IoObjectsOrExecutors>
104 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, Signature)
105 async_compose(BOOST_ASIO_MOVE_ARG(Implementation) implementation,
106 BOOST_ASIO_NONDEDUCED_MOVE_ARG(CompletionToken) token,
107 BOOST_ASIO_MOVE_ARG(IoObjectsOrExecutors)... io_objects_or_executors);
109 #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
110 // || defined(GENERATING_DOCUMENTATION)
112 template <typename CompletionToken, typename Signature, typename Implementation>
113 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, Signature)
114 async_compose(BOOST_ASIO_MOVE_ARG(Implementation) implementation,
115 BOOST_ASIO_NONDEDUCED_MOVE_ARG(CompletionToken) token);
117 #define BOOST_ASIO_PRIVATE_ASYNC_COMPOSE_DEF(n) \
118 template <typename CompletionToken, typename Signature, \
119 typename Implementation, BOOST_ASIO_VARIADIC_TPARAMS(n)> \
120 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, Signature) \
121 async_compose(BOOST_ASIO_MOVE_ARG(Implementation) implementation, \
122 BOOST_ASIO_NONDEDUCED_MOVE_ARG(CompletionToken) token, \
123 BOOST_ASIO_VARIADIC_MOVE_PARAMS(n));
125 BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_ASYNC_COMPOSE_DEF)
126 #undef BOOST_ASIO_PRIVATE_ASYNC_COMPOSE_DEF
128 #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
129 // || defined(GENERATING_DOCUMENTATION)
134 #include <boost/asio/detail/pop_options.hpp>
136 #include <boost/asio/impl/compose.hpp>
138 #endif // BOOST_ASIO_COMPOSE_HPP