// // detail/work_dispatcher.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com) // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #ifndef BOOST_ASIO_DETAIL_WORK_DISPATCHER_HPP #define BOOST_ASIO_DETAIL_WORK_DISPATCHER_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include #include #include #include #include #include #include #include #include #include #include #include namespace boost { namespace asio { namespace detail { template struct is_work_dispatcher_required : true_type { }; template struct is_work_dispatcher_required::asio_associated_executor_is_unspecialised, void >::value >> : false_type { }; template class work_dispatcher { public: template work_dispatcher(CompletionHandler&& handler, const Executor& handler_ex) : handler_(static_cast(handler)), executor_(boost::asio::prefer(handler_ex, execution::outstanding_work.tracked)) { } work_dispatcher(const work_dispatcher& other) : handler_(other.handler_), executor_(other.executor_) { } work_dispatcher(work_dispatcher&& other) : handler_(static_cast(other.handler_)), executor_(static_cast(other.executor_)) { } void operator()() { associated_allocator_t alloc((get_associated_allocator)(handler_)); boost::asio::prefer(executor_, execution::allocator(alloc)).execute( boost::asio::detail::bind_handler( static_cast(handler_))); } private: typedef decay_t< prefer_result_t > work_executor_type; Handler handler_; work_executor_type executor_; }; #if !defined(BOOST_ASIO_NO_TS_EXECUTORS) template class work_dispatcher::value>> { public: template work_dispatcher(CompletionHandler&& handler, const Executor& handler_ex) : work_(handler_ex), handler_(static_cast(handler)) { } work_dispatcher(const work_dispatcher& other) : work_(other.work_), handler_(other.handler_) { } work_dispatcher(work_dispatcher&& other) : work_(static_cast&&>(other.work_)), handler_(static_cast(other.handler_)) { } void operator()() { associated_allocator_t alloc((get_associated_allocator)(handler_)); work_.get_executor().dispatch( boost::asio::detail::bind_handler( static_cast(handler_)), alloc); work_.reset(); } private: executor_work_guard work_; Handler handler_; }; #endif // !defined(BOOST_ASIO_NO_TS_EXECUTORS) } // namespace detail } // namespace asio } // namespace boost #include #endif // BOOST_ASIO_DETAIL_WORK_DISPATCHER_HPP