5 // Copyright (c) 2003-2017 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>
28 template <typename CompletionToken>
29 BOOST_ASIO_INITFN_RESULT_TYPE(CompletionToken, void()) defer(
30 BOOST_ASIO_MOVE_ARG(CompletionToken) token)
32 typedef BOOST_ASIO_HANDLER_TYPE(CompletionToken, void()) handler;
34 async_completion<CompletionToken, void()> init(token);
36 typename associated_executor<handler>::type ex(
37 (get_associated_executor)(init.completion_handler));
39 typename associated_allocator<handler>::type alloc(
40 (get_associated_allocator)(init.completion_handler));
42 ex.defer(BOOST_ASIO_MOVE_CAST(handler)(init.completion_handler), alloc);
44 return init.result.get();
47 template <typename Executor, typename CompletionToken>
48 BOOST_ASIO_INITFN_RESULT_TYPE(CompletionToken, void()) defer(
49 const Executor& ex, BOOST_ASIO_MOVE_ARG(CompletionToken) token,
50 typename enable_if<is_executor<Executor>::value>::type*)
52 typedef BOOST_ASIO_HANDLER_TYPE(CompletionToken, void()) handler;
54 async_completion<CompletionToken, void()> init(token);
56 typename associated_allocator<handler>::type alloc(
57 (get_associated_allocator)(init.completion_handler));
59 ex.defer(detail::work_dispatcher<handler>(init.completion_handler), alloc);
61 return init.result.get();
64 template <typename ExecutionContext, typename CompletionToken>
65 inline BOOST_ASIO_INITFN_RESULT_TYPE(CompletionToken, void()) defer(
66 ExecutionContext& ctx, BOOST_ASIO_MOVE_ARG(CompletionToken) token,
67 typename enable_if<is_convertible<
68 ExecutionContext&, execution_context&>::value>::type*)
70 return (defer)(ctx.get_executor(),
71 BOOST_ASIO_MOVE_CAST(CompletionToken)(token));
77 #include <boost/asio/detail/pop_options.hpp>
79 #endif // BOOST_ASIO_IMPL_DEFER_HPP