1 //
2 // defer.hpp
3 // ~~~~~~~~~
4 //
5 // Copyright (c) 2003-2017 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 ASIO_DEFER_HPP
12 #define 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 "asio/detail/config.hpp"
19 #include "asio/async_result.hpp"
20 #include "asio/detail/type_traits.hpp"
21 #include "asio/execution_context.hpp"
22 #include "asio/is_executor.hpp"
23 
24 #include "asio/detail/push_options.hpp"
25 
26 namespace asio {
27 
28 /// Submits a completion token or function object for execution.
29 /**
30  * This function submits an object for execution using the object's associated
31  * executor. The function object is queued for execution, and is never called
32  * from the current thread prior to returning from <tt>defer()</tt>.
33  *
34  * This function has the following effects:
35  *
36  * @li Constructs a function object handler of type @c Handler, initialized
37  * with <tt>handler(forward<CompletionToken>(token))</tt>.
38  *
39  * @li Constructs an object @c result of type <tt>async_result<Handler></tt>,
40  * initializing the object as <tt>result(handler)</tt>.
41  *
42  * @li Obtains the handler's associated executor object @c ex by performing
43  * <tt>get_associated_executor(handler)</tt>.
44  *
45  * @li Obtains the handler's associated allocator object @c alloc by performing
46  * <tt>get_associated_allocator(handler)</tt>.
47  *
48  * @li Performs <tt>ex.defer(std::move(handler), alloc)</tt>.
49  *
50  * @li Returns <tt>result.get()</tt>.
51  */
52 template <typename CompletionToken>
53 ASIO_INITFN_RESULT_TYPE(CompletionToken, void()) defer(
54     ASIO_MOVE_ARG(CompletionToken) token);
55 
56 /// Submits a completion token or function object for execution.
57 /**
58  * This function submits an object for execution using the specified executor.
59  * The function object is queued for execution, and is never called from the
60  * current thread prior to returning from <tt>defer()</tt>.
61  *
62  * This function has the following effects:
63  *
64  * @li Constructs a function object handler of type @c Handler, initialized
65  * with <tt>handler(forward<CompletionToken>(token))</tt>.
66  *
67  * @li Constructs an object @c result of type <tt>async_result<Handler></tt>,
68  * initializing the object as <tt>result(handler)</tt>.
69  *
70  * @li Obtains the handler's associated executor object @c ex1 by performing
71  * <tt>get_associated_executor(handler)</tt>.
72  *
73  * @li Creates a work object @c w by performing <tt>make_work(ex1)</tt>.
74  *
75  * @li Obtains the handler's associated allocator object @c alloc by performing
76  * <tt>get_associated_allocator(handler)</tt>.
77  *
78  * @li Constructs a function object @c f with a function call operator that
79  * performs <tt>ex1.dispatch(std::move(handler), alloc)</tt> followed by
80  * <tt>w.reset()</tt>.
81  *
82  * @li Performs <tt>Executor(ex).defer(std::move(f), alloc)</tt>.
83  *
84  * @li Returns <tt>result.get()</tt>.
85  */
86 template <typename Executor, typename CompletionToken>
87 ASIO_INITFN_RESULT_TYPE(CompletionToken, void()) defer(
88     const Executor& ex, ASIO_MOVE_ARG(CompletionToken) token,
89     typename enable_if<is_executor<Executor>::value>::type* = 0);
90 
91 /// Submits a completion token or function object for execution.
92 /**
93  * @returns <tt>defer(ctx.get_executor(), forward<CompletionToken>(token))</tt>.
94  */
95 template <typename ExecutionContext, typename CompletionToken>
96 ASIO_INITFN_RESULT_TYPE(CompletionToken, void()) defer(
97     ExecutionContext& ctx, ASIO_MOVE_ARG(CompletionToken) token,
98     typename enable_if<is_convertible<
99       ExecutionContext&, execution_context&>::value>::type* = 0);
100 
101 } // namespace asio
102 
103 #include "asio/detail/pop_options.hpp"
104 
105 #include "asio/impl/defer.hpp"
106 
107 #endif // ASIO_DEFER_HPP
108