Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / asio / impl / dispatch.hpp
1 //
2 // impl/dispatch.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_IMPL_DISPATCH_HPP
12 #define BOOST_ASIO_IMPL_DISPATCH_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/associated_allocator.hpp>
20 #include <boost/asio/associated_executor.hpp>
21 #include <boost/asio/detail/work_dispatcher.hpp>
22
23 #include <boost/asio/detail/push_options.hpp>
24
25 namespace boost {
26 namespace asio {
27 namespace detail {
28
29 class initiate_dispatch
30 {
31 public:
32   template <typename CompletionHandler>
33   void operator()(BOOST_ASIO_MOVE_ARG(CompletionHandler) handler) const
34   {
35     typedef typename decay<CompletionHandler>::type DecayedHandler;
36
37     typename associated_executor<DecayedHandler>::type ex(
38         (get_associated_executor)(handler));
39
40     typename associated_allocator<DecayedHandler>::type alloc(
41         (get_associated_allocator)(handler));
42
43     ex.dispatch(BOOST_ASIO_MOVE_CAST(CompletionHandler)(handler), alloc);
44   }
45 };
46
47 template <typename Executor>
48 class initiate_dispatch_with_executor
49 {
50 public:
51   typedef Executor executor_type;
52
53   explicit initiate_dispatch_with_executor(const Executor& ex)
54     : ex_(ex)
55   {
56   }
57
58   executor_type get_executor() const BOOST_ASIO_NOEXCEPT
59   {
60     return ex_;
61   }
62
63   template <typename CompletionHandler>
64   void operator()(BOOST_ASIO_MOVE_ARG(CompletionHandler) handler) const
65   {
66     typedef typename decay<CompletionHandler>::type DecayedHandler;
67
68     typename associated_allocator<DecayedHandler>::type alloc(
69         (get_associated_allocator)(handler));
70
71     ex_.dispatch(detail::work_dispatcher<DecayedHandler>(
72           BOOST_ASIO_MOVE_CAST(CompletionHandler)(handler)), alloc);
73   }
74
75 private:
76   Executor ex_;
77 };
78
79 } // namespace detail
80
81 template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) CompletionToken>
82 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void()) dispatch(
83     BOOST_ASIO_MOVE_ARG(CompletionToken) token)
84 {
85   return async_initiate<CompletionToken, void()>(
86       detail::initiate_dispatch(), token);
87 }
88
89 template <typename Executor,
90     BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) CompletionToken>
91 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void()) dispatch(
92     const Executor& ex, BOOST_ASIO_MOVE_ARG(CompletionToken) token,
93     typename enable_if<is_executor<Executor>::value>::type*)
94 {
95   return async_initiate<CompletionToken, void()>(
96       detail::initiate_dispatch_with_executor<Executor>(ex), token);
97 }
98
99 template <typename ExecutionContext,
100     BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) CompletionToken>
101 inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void()) dispatch(
102     ExecutionContext& ctx, BOOST_ASIO_MOVE_ARG(CompletionToken) token,
103     typename enable_if<is_convertible<
104       ExecutionContext&, execution_context&>::value>::type*)
105 {
106   return (dispatch)(ctx.get_executor(),
107       BOOST_ASIO_MOVE_CAST(CompletionToken)(token));
108 }
109
110 } // namespace asio
111 } // namespace boost
112
113 #include <boost/asio/detail/pop_options.hpp>
114
115 #endif // BOOST_ASIO_IMPL_DISPATCH_HPP