Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / asio / compose.hpp
1 //
2 // compose.hpp
3 // ~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
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)
9 //
10
11 #ifndef BOOST_ASIO_COMPOSE_HPP
12 #define BOOST_ASIO_COMPOSE_HPP
13
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/asio/async_result.hpp>
20
21 #include <boost/asio/detail/push_options.hpp>
22
23 namespace boost {
24 namespace asio {
25
26 #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES) \
27   || defined(GENERATING_DOCUMENTATION)
28
29 /// Launch an asynchronous operation with a stateful implementation.
30 /**
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.
34  *
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
40  * implementation.
41
42  * @param token The completion token.
43  *
44  * @param io_objects_or_executors Zero or more I/O objects or I/O executors for
45  * which outstanding work must be maintained.
46  *
47  * @par Example:
48  *
49  * @code struct async_echo_implementation
50  * {
51  *   tcp::socket& socket_;
52  *   boost::asio::mutable_buffer buffer_;
53  *   enum { starting, reading, writing } state_;
54  *
55  *   template <typename Self>
56  *   void operator()(Self& self,
57  *       boost::system::error_code error = {},
58  *       std::size_t n = 0)
59  *   {
60  *     switch (state_)
61  *     {
62  *     case starting:
63  *       state_ = reading;
64  *       socket_.async_read_some(
65  *           buffer_, std::move(self));
66  *       break;
67  *     case reading:
68  *       if (error)
69  *       {
70  *         self.complete(error, 0);
71  *       }
72  *       else
73  *       {
74  *         state_ = writing;
75  *         boost::asio::async_write(socket_, buffer_,
76  *             boost::asio::transfer_exactly(n),
77  *             std::move(self));
78  *       }
79  *       break;
80  *     case writing:
81  *       self.complete(error, n);
82  *       break;
83  *     }
84  *   }
85  * };
86  *
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
94  * {
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},
99  *       token, socket);
100  * } @endcode
101  */
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);
108
109 #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
110       //   || defined(GENERATING_DOCUMENTATION)
111
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);
116
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));
124   /**/
125   BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_ASYNC_COMPOSE_DEF)
126 #undef BOOST_ASIO_PRIVATE_ASYNC_COMPOSE_DEF
127
128 #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
129        //   || defined(GENERATING_DOCUMENTATION)
130
131 } // namespace asio
132 } // namespace boost
133
134 #include <boost/asio/detail/pop_options.hpp>
135
136 #include <boost/asio/impl/compose.hpp>
137
138 #endif // BOOST_ASIO_COMPOSE_HPP