2 // io_object_executor.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~
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_DETAIL_IO_OBJECT_EXECUTOR_HPP
12 #define BOOST_ASIO_DETAIL_IO_OBJECT_EXECUTOR_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/detail/handler_invoke_helpers.hpp>
20 #include <boost/asio/detail/type_traits.hpp>
21 #include <boost/asio/io_context.hpp>
23 #include <boost/asio/detail/push_options.hpp>
29 // Wrap the (potentially polymorphic) executor so that we can bypass it when
30 // dispatching on a target executor that has a native I/O implementation.
31 template <typename Executor>
32 class io_object_executor
35 io_object_executor(const Executor& ex,
36 bool native_implementation) BOOST_ASIO_NOEXCEPT
38 has_native_impl_(native_implementation)
42 io_object_executor(const io_object_executor& other) BOOST_ASIO_NOEXCEPT
43 : executor_(other.executor_),
44 has_native_impl_(other.has_native_impl_)
48 template <typename Executor1>
50 const io_object_executor<Executor1>& other) BOOST_ASIO_NOEXCEPT
51 : executor_(other.inner_executor()),
52 has_native_impl_(other.has_native_implementation())
56 #if defined(BOOST_ASIO_HAS_MOVE)
57 io_object_executor(io_object_executor&& other) BOOST_ASIO_NOEXCEPT
58 : executor_(BOOST_ASIO_MOVE_CAST(Executor)(other.executor_)),
59 has_native_impl_(other.has_native_impl_)
62 #endif // defined(BOOST_ASIO_HAS_MOVE)
64 const Executor& inner_executor() const BOOST_ASIO_NOEXCEPT
69 bool has_native_implementation() const BOOST_ASIO_NOEXCEPT
71 return has_native_impl_;
74 execution_context& context() const BOOST_ASIO_NOEXCEPT
76 return executor_.context();
79 void on_work_started() const BOOST_ASIO_NOEXCEPT
81 if (is_same<Executor, io_context::executor_type>::value
84 // When using a native implementation, work is already counted by the
89 executor_.on_work_started();
93 void on_work_finished() const BOOST_ASIO_NOEXCEPT
95 if (is_same<Executor, io_context::executor_type>::value
98 // When using a native implementation, work is already counted by the
103 executor_.on_work_finished();
107 template <typename F, typename A>
108 void dispatch(BOOST_ASIO_MOVE_ARG(F) f, const A& a) const
110 if (is_same<Executor, io_context::executor_type>::value
113 // When using a native implementation, I/O completion handlers are
114 // already dispatched according to the execution context's executor's
115 // rules. We can call the function directly.
116 #if defined(BOOST_ASIO_HAS_MOVE)
117 if (is_same<F, typename decay<F>::type>::value)
119 boost_asio_handler_invoke_helpers::invoke(f, f);
122 #endif // defined(BOOST_ASIO_HAS_MOVE)
123 typename decay<F>::type function(BOOST_ASIO_MOVE_CAST(F)(f));
124 boost_asio_handler_invoke_helpers::invoke(function, function);
128 executor_.dispatch(BOOST_ASIO_MOVE_CAST(F)(f), a);
132 template <typename F, typename A>
133 void post(BOOST_ASIO_MOVE_ARG(F) f, const A& a) const
135 executor_.post(BOOST_ASIO_MOVE_CAST(F)(f), a);
138 template <typename F, typename A>
139 void defer(BOOST_ASIO_MOVE_ARG(F) f, const A& a) const
141 executor_.defer(BOOST_ASIO_MOVE_CAST(F)(f), a);
144 friend bool operator==(const io_object_executor& a,
145 const io_object_executor& b) BOOST_ASIO_NOEXCEPT
147 return a.executor_ == b.executor_
148 && a.has_native_impl_ == b.has_native_impl_;
151 friend bool operator!=(const io_object_executor& a,
152 const io_object_executor& b) BOOST_ASIO_NOEXCEPT
154 return a.executor_ != b.executor_
155 || a.has_native_impl_ != b.has_native_impl_;
160 const bool has_native_impl_;
163 } // namespace detail
167 #include <boost/asio/detail/pop_options.hpp>
169 #endif // BOOST_ASIO_DETAIL_IO_OBJECT_EXECUTOR_HPP