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_IMPL_DEFER_HPP
12 #define BOOST_ASIO_IMPL_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/associated_allocator.hpp>
20 #include <boost/asio/associated_executor.hpp>
21 #include <boost/asio/detail/work_dispatcher.hpp>
23 #include <boost/asio/detail/push_options.hpp>
32 template <typename CompletionHandler>
33 void operator()(BOOST_ASIO_MOVE_ARG(CompletionHandler) handler) const
35 typedef typename decay<CompletionHandler>::type DecayedHandler;
37 typename associated_executor<DecayedHandler>::type ex(
38 (get_associated_executor)(handler));
40 typename associated_allocator<DecayedHandler>::type alloc(
41 (get_associated_allocator)(handler));
43 ex.defer(BOOST_ASIO_MOVE_CAST(CompletionHandler)(handler), alloc);
47 template <typename Executor>
48 class initiate_defer_with_executor
51 typedef Executor executor_type;
53 explicit initiate_defer_with_executor(const Executor& ex)
58 executor_type get_executor() const BOOST_ASIO_NOEXCEPT
63 template <typename CompletionHandler>
64 void operator()(BOOST_ASIO_MOVE_ARG(CompletionHandler) handler) const
66 typedef typename decay<CompletionHandler>::type DecayedHandler;
68 typename associated_allocator<DecayedHandler>::type alloc(
69 (get_associated_allocator)(handler));
71 ex_.defer(detail::work_dispatcher<DecayedHandler>(
72 BOOST_ASIO_MOVE_CAST(CompletionHandler)(handler)), alloc);
81 template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) CompletionToken>
82 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void()) defer(
83 BOOST_ASIO_MOVE_ARG(CompletionToken) token)
85 return async_initiate<CompletionToken, void()>(
86 detail::initiate_defer(), token);
89 template <typename Executor,
90 BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) CompletionToken>
91 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void()) defer(
92 const Executor& ex, BOOST_ASIO_MOVE_ARG(CompletionToken) token,
93 typename enable_if<is_executor<Executor>::value>::type*)
95 return async_initiate<CompletionToken, void()>(
96 detail::initiate_defer_with_executor<Executor>(ex), token);
99 template <typename ExecutionContext,
100 BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) CompletionToken>
101 inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void()) defer(
102 ExecutionContext& ctx, BOOST_ASIO_MOVE_ARG(CompletionToken) token,
103 typename enable_if<is_convertible<
104 ExecutionContext&, execution_context&>::value>::type*)
106 return (defer)(ctx.get_executor(),
107 BOOST_ASIO_MOVE_CAST(CompletionToken)(token));
113 #include <boost/asio/detail/pop_options.hpp>
115 #endif // BOOST_ASIO_IMPL_DEFER_HPP