File indexing completed on 2025-01-18 09:28:48
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_ASIO_DETAIL_WORK_DISPATCHER_HPP
0012 #define BOOST_ASIO_DETAIL_WORK_DISPATCHER_HPP
0013
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif
0017
0018 #include <boost/asio/detail/config.hpp>
0019 #include <boost/asio/detail/bind_handler.hpp>
0020 #include <boost/asio/detail/type_traits.hpp>
0021 #include <boost/asio/associated_executor.hpp>
0022 #include <boost/asio/associated_allocator.hpp>
0023 #include <boost/asio/executor_work_guard.hpp>
0024 #include <boost/asio/execution/executor.hpp>
0025 #include <boost/asio/execution/allocator.hpp>
0026 #include <boost/asio/execution/blocking.hpp>
0027 #include <boost/asio/execution/outstanding_work.hpp>
0028 #include <boost/asio/prefer.hpp>
0029
0030 #include <boost/asio/detail/push_options.hpp>
0031
0032 namespace boost {
0033 namespace asio {
0034 namespace detail {
0035
0036 template <typename Handler, typename Executor, typename = void>
0037 struct is_work_dispatcher_required : true_type
0038 {
0039 };
0040
0041 template <typename Handler, typename Executor>
0042 struct is_work_dispatcher_required<Handler, Executor,
0043 enable_if_t<
0044 is_same<
0045 typename associated_executor<Handler,
0046 Executor>::asio_associated_executor_is_unspecialised,
0047 void
0048 >::value
0049 >> : false_type
0050 {
0051 };
0052
0053 template <typename Handler, typename Executor, typename = void>
0054 class work_dispatcher
0055 {
0056 public:
0057 template <typename CompletionHandler>
0058 work_dispatcher(CompletionHandler&& handler,
0059 const Executor& handler_ex)
0060 : handler_(static_cast<CompletionHandler&&>(handler)),
0061 executor_(boost::asio::prefer(handler_ex,
0062 execution::outstanding_work.tracked))
0063 {
0064 }
0065
0066 work_dispatcher(const work_dispatcher& other)
0067 : handler_(other.handler_),
0068 executor_(other.executor_)
0069 {
0070 }
0071
0072 work_dispatcher(work_dispatcher&& other)
0073 : handler_(static_cast<Handler&&>(other.handler_)),
0074 executor_(static_cast<work_executor_type&&>(other.executor_))
0075 {
0076 }
0077
0078 void operator()()
0079 {
0080 associated_allocator_t<Handler> alloc((get_associated_allocator)(handler_));
0081 boost::asio::prefer(executor_, execution::allocator(alloc)).execute(
0082 boost::asio::detail::bind_handler(
0083 static_cast<Handler&&>(handler_)));
0084 }
0085
0086 private:
0087 typedef decay_t<
0088 prefer_result_t<const Executor&,
0089 execution::outstanding_work_t::tracked_t
0090 >
0091 > work_executor_type;
0092
0093 Handler handler_;
0094 work_executor_type executor_;
0095 };
0096
0097 #if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
0098
0099 template <typename Handler, typename Executor>
0100 class work_dispatcher<Handler, Executor,
0101 enable_if_t<!execution::is_executor<Executor>::value>>
0102 {
0103 public:
0104 template <typename CompletionHandler>
0105 work_dispatcher(CompletionHandler&& handler, const Executor& handler_ex)
0106 : work_(handler_ex),
0107 handler_(static_cast<CompletionHandler&&>(handler))
0108 {
0109 }
0110
0111 work_dispatcher(const work_dispatcher& other)
0112 : work_(other.work_),
0113 handler_(other.handler_)
0114 {
0115 }
0116
0117 work_dispatcher(work_dispatcher&& other)
0118 : work_(static_cast<executor_work_guard<Executor>&&>(other.work_)),
0119 handler_(static_cast<Handler&&>(other.handler_))
0120 {
0121 }
0122
0123 void operator()()
0124 {
0125 associated_allocator_t<Handler> alloc((get_associated_allocator)(handler_));
0126 work_.get_executor().dispatch(
0127 boost::asio::detail::bind_handler(
0128 static_cast<Handler&&>(handler_)), alloc);
0129 work_.reset();
0130 }
0131
0132 private:
0133 executor_work_guard<Executor> work_;
0134 Handler handler_;
0135 };
0136
0137 #endif
0138
0139 }
0140 }
0141 }
0142
0143 #include <boost/asio/detail/pop_options.hpp>
0144
0145 #endif