2 // detail/handler_work.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~
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_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, typename Executor
32 = typename associated_executor<Handler>::type>
36 explicit handler_work(Handler& handler) BOOST_ASIO_NOEXCEPT
37 : executor_(associated_executor<Handler>::get(handler))
41 static void start(Handler& handler) BOOST_ASIO_NOEXCEPT
43 Executor ex(associated_executor<Handler>::get(handler));
49 executor_.on_work_finished();
52 template <typename Function>
53 void complete(Function& function, Handler& handler)
55 executor_.dispatch(BOOST_ASIO_MOVE_CAST(Function)(function),
56 associated_allocator<Handler>::get(handler));
60 // Disallow copying and assignment.
61 handler_work(const handler_work&);
62 handler_work& operator=(const handler_work&);
64 typename associated_executor<Handler>::type executor_;
67 // This specialisation dispatches a handler through the old invocation hook.
68 // The specialisation is not strictly required for correctness, as the
69 // system_executor will dispatch through the hook anyway. However, by doing
70 // this we avoid an extra copy of the handler.
71 template <typename Handler>
72 class handler_work<Handler, system_executor>
75 explicit handler_work(Handler&) BOOST_ASIO_NOEXCEPT {}
76 static void start(Handler&) BOOST_ASIO_NOEXCEPT {}
79 template <typename Function>
80 void complete(Function& function, Handler& handler)
82 boost_asio_handler_invoke_helpers::invoke(function, handler);
86 // Disallow copying and assignment.
87 handler_work(const handler_work&);
88 handler_work& operator=(const handler_work&);
95 #include <boost/asio/detail/pop_options.hpp>
97 #endif // BOOST_ASIO_DETAIL_HANDLER_WORK_HPP