Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / asio / defer.hpp
1 //
2 // defer.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_DEFER_HPP
12 #define BOOST_ASIO_DEFER_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 #include <boost/asio/detail/type_traits.hpp>
21 #include <boost/asio/execution_context.hpp>
22 #include <boost/asio/is_executor.hpp>
23
24 #include <boost/asio/detail/push_options.hpp>
25
26 namespace boost {
27 namespace asio {
28
29 /// Submits a completion token or function object for execution.
30 /**
31  * This function submits an object for execution using the object's associated
32  * executor. The function object is queued for execution, and is never called
33  * from the current thread prior to returning from <tt>defer()</tt>.
34  *
35  * The use of @c defer(), rather than @ref post(), indicates the caller's
36  * preference that the executor defer the queueing of the function object. This
37  * may allow the executor to optimise queueing for cases when the function
38  * object represents a continuation of the current call context.
39  *
40  * This function has the following effects:
41  *
42  * @li Constructs a function object handler of type @c Handler, initialized
43  * with <tt>handler(forward<CompletionToken>(token))</tt>.
44  *
45  * @li Constructs an object @c result of type <tt>async_result<Handler></tt>,
46  * initializing the object as <tt>result(handler)</tt>.
47  *
48  * @li Obtains the handler's associated executor object @c ex by performing
49  * <tt>get_associated_executor(handler)</tt>.
50  *
51  * @li Obtains the handler's associated allocator object @c alloc by performing
52  * <tt>get_associated_allocator(handler)</tt>.
53  *
54  * @li Performs <tt>ex.defer(std::move(handler), alloc)</tt>.
55  *
56  * @li Returns <tt>result.get()</tt>.
57  */
58 template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) CompletionToken>
59 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void()) defer(
60     BOOST_ASIO_MOVE_ARG(CompletionToken) token);
61
62 /// Submits a completion token or function object for execution.
63 /**
64  * This function submits an object for execution using the specified executor.
65  * The function object is queued for execution, and is never called from the
66  * current thread prior to returning from <tt>defer()</tt>.
67  *
68  * The use of @c defer(), rather than @ref post(), indicates the caller's
69  * preference that the executor defer the queueing of the function object. This
70  * may allow the executor to optimise queueing for cases when the function
71  * object represents a continuation of the current call context.
72  *
73  * This function has the following effects:
74  *
75  * @li Constructs a function object handler of type @c Handler, initialized
76  * with <tt>handler(forward<CompletionToken>(token))</tt>.
77  *
78  * @li Constructs an object @c result of type <tt>async_result<Handler></tt>,
79  * initializing the object as <tt>result(handler)</tt>.
80  *
81  * @li Obtains the handler's associated executor object @c ex1 by performing
82  * <tt>get_associated_executor(handler)</tt>.
83  *
84  * @li Creates a work object @c w by performing <tt>make_work(ex1)</tt>.
85  *
86  * @li Obtains the handler's associated allocator object @c alloc by performing
87  * <tt>get_associated_allocator(handler)</tt>.
88  *
89  * @li Constructs a function object @c f with a function call operator that
90  * performs <tt>ex1.dispatch(std::move(handler), alloc)</tt> followed by
91  * <tt>w.reset()</tt>.
92  *
93  * @li Performs <tt>Executor(ex).defer(std::move(f), alloc)</tt>.
94  *
95  * @li Returns <tt>result.get()</tt>.
96  */
97 template <typename Executor,
98     BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) CompletionToken
99       BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(Executor)>
100 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void()) defer(
101     const Executor& ex,
102     BOOST_ASIO_MOVE_ARG(CompletionToken) token
103       BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(Executor),
104     typename enable_if<is_executor<Executor>::value>::type* = 0);
105
106 /// Submits a completion token or function object for execution.
107 /**
108  * @returns <tt>defer(ctx.get_executor(), forward<CompletionToken>(token))</tt>.
109  */
110 template <typename ExecutionContext,
111     BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) CompletionToken
112       BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(
113         typename ExecutionContext::executor_type)>
114 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void()) defer(
115     ExecutionContext& ctx,
116     BOOST_ASIO_MOVE_ARG(CompletionToken) token
117       BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(
118         typename ExecutionContext::executor_type),
119     typename enable_if<is_convertible<
120       ExecutionContext&, execution_context&>::value>::type* = 0);
121
122 } // namespace asio
123 } // namespace boost
124
125 #include <boost/asio/detail/pop_options.hpp>
126
127 #include <boost/asio/impl/defer.hpp>
128
129 #endif // BOOST_ASIO_DEFER_HPP