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_DEFER_HPP
12 #define BOOST_ASIO_DEFER_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>
20 #include <boost/asio/detail/type_traits.hpp>
21 #include <boost/asio/execution_context.hpp>
22 #include <boost/asio/is_executor.hpp>
24 #include <boost/asio/detail/push_options.hpp>
29 /// Submits a completion token or function object for execution.
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>.
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.
40 * This function has the following effects:
42 * @li Constructs a function object handler of type @c Handler, initialized
43 * with <tt>handler(forward<CompletionToken>(token))</tt>.
45 * @li Constructs an object @c result of type <tt>async_result<Handler></tt>,
46 * initializing the object as <tt>result(handler)</tt>.
48 * @li Obtains the handler's associated executor object @c ex by performing
49 * <tt>get_associated_executor(handler)</tt>.
51 * @li Obtains the handler's associated allocator object @c alloc by performing
52 * <tt>get_associated_allocator(handler)</tt>.
54 * @li Performs <tt>ex.defer(std::move(handler), alloc)</tt>.
56 * @li Returns <tt>result.get()</tt>.
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);
62 /// Submits a completion token or function object for execution.
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>.
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.
73 * This function has the following effects:
75 * @li Constructs a function object handler of type @c Handler, initialized
76 * with <tt>handler(forward<CompletionToken>(token))</tt>.
78 * @li Constructs an object @c result of type <tt>async_result<Handler></tt>,
79 * initializing the object as <tt>result(handler)</tt>.
81 * @li Obtains the handler's associated executor object @c ex1 by performing
82 * <tt>get_associated_executor(handler)</tt>.
84 * @li Creates a work object @c w by performing <tt>make_work(ex1)</tt>.
86 * @li Obtains the handler's associated allocator object @c alloc by performing
87 * <tt>get_associated_allocator(handler)</tt>.
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
93 * @li Performs <tt>Executor(ex).defer(std::move(f), alloc)</tt>.
95 * @li Returns <tt>result.get()</tt>.
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(
102 BOOST_ASIO_MOVE_ARG(CompletionToken) token
103 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(Executor),
104 typename enable_if<is_executor<Executor>::value>::type* = 0);
106 /// Submits a completion token or function object for execution.
108 * @returns <tt>defer(ctx.get_executor(), forward<CompletionToken>(token))</tt>.
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);
125 #include <boost/asio/detail/pop_options.hpp>
127 #include <boost/asio/impl/defer.hpp>
129 #endif // BOOST_ASIO_DEFER_HPP