2 // detail/handler_work.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2020 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_DETAIL_HANDLER_WORK_HPP
12 #define BOOST_ASIO_DETAIL_HANDLER_WORK_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_executor.hpp>
20 #include <boost/asio/detail/handler_invoke_helpers.hpp>
22 #include <boost/asio/detail/push_options.hpp>
28 // A helper class template to allow completion handlers to be dispatched
29 // through either the new executors framework or the old invocaton hook. The
30 // primary template uses the new executors framework.
31 template <typename Handler,
32 typename IoExecutor = system_executor, typename HandlerExecutor
33 = typename associated_executor<Handler, IoExecutor>::type>
37 explicit handler_work(Handler& handler) BOOST_ASIO_NOEXCEPT
39 executor_(boost::asio::get_associated_executor(handler, io_executor_))
43 handler_work(Handler& handler, const IoExecutor& io_ex) BOOST_ASIO_NOEXCEPT
44 : io_executor_(io_ex),
45 executor_(boost::asio::get_associated_executor(handler, io_executor_))
49 static void start(Handler& handler) BOOST_ASIO_NOEXCEPT
51 HandlerExecutor ex(boost::asio::get_associated_executor(handler));
55 static void start(Handler& handler,
56 const IoExecutor& io_ex) BOOST_ASIO_NOEXCEPT
58 HandlerExecutor ex(boost::asio::get_associated_executor(handler, io_ex));
60 io_ex.on_work_started();
65 io_executor_.on_work_finished();
66 executor_.on_work_finished();
69 template <typename Function>
70 void complete(Function& function, Handler& handler)
72 executor_.dispatch(BOOST_ASIO_MOVE_CAST(Function)(function),
73 boost::asio::get_associated_allocator(handler));
77 // Disallow copying and assignment.
78 handler_work(const handler_work&);
79 handler_work& operator=(const handler_work&);
81 IoExecutor io_executor_;
82 HandlerExecutor executor_;
85 // This specialisation dispatches a handler through the old invocation hook.
86 // The specialisation is not strictly required for correctness, as the
87 // system_executor will dispatch through the hook anyway. However, by doing
88 // this we avoid an extra copy of the handler.
89 template <typename Handler>
90 class handler_work<Handler, system_executor, system_executor>
93 explicit handler_work(Handler&) BOOST_ASIO_NOEXCEPT {}
94 static void start(Handler&) BOOST_ASIO_NOEXCEPT {}
97 template <typename Function>
98 void complete(Function& function, Handler& handler)
100 boost_asio_handler_invoke_helpers::invoke(function, handler);
104 // Disallow copying and assignment.
105 handler_work(const handler_work&);
106 handler_work& operator=(const handler_work&);
109 } // namespace detail
113 #include <boost/asio/detail/pop_options.hpp>
115 #endif // BOOST_ASIO_DETAIL_HANDLER_WORK_HPP