initiate_dispatch.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // detail/initiate_dispatch.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2025 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  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)
  9. //
  10. #ifndef BOOST_ASIO_DETAIL_INITIATE_DISPATCH_HPP
  11. #define BOOST_ASIO_DETAIL_INITIATE_DISPATCH_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <boost/asio/associated_allocator.hpp>
  17. #include <boost/asio/associated_executor.hpp>
  18. #include <boost/asio/detail/work_dispatcher.hpp>
  19. #include <boost/asio/execution/allocator.hpp>
  20. #include <boost/asio/execution/blocking.hpp>
  21. #include <boost/asio/prefer.hpp>
  22. #include <boost/asio/detail/push_options.hpp>
  23. namespace boost {
  24. namespace asio {
  25. namespace detail {
  26. class initiate_dispatch
  27. {
  28. public:
  29. template <typename CompletionHandler>
  30. void operator()(CompletionHandler&& handler,
  31. enable_if_t<
  32. execution::is_executor<
  33. associated_executor_t<decay_t<CompletionHandler>>
  34. >::value
  35. >* = 0) const
  36. {
  37. associated_executor_t<decay_t<CompletionHandler>> ex(
  38. (get_associated_executor)(handler));
  39. associated_allocator_t<decay_t<CompletionHandler>> alloc(
  40. (get_associated_allocator)(handler));
  41. boost::asio::prefer(ex, execution::allocator(alloc)).execute(
  42. boost::asio::detail::bind_handler(
  43. static_cast<CompletionHandler&&>(handler)));
  44. }
  45. template <typename CompletionHandler>
  46. void operator()(CompletionHandler&& handler,
  47. enable_if_t<
  48. !execution::is_executor<
  49. associated_executor_t<decay_t<CompletionHandler>>
  50. >::value
  51. >* = 0) const
  52. {
  53. associated_executor_t<decay_t<CompletionHandler>> ex(
  54. (get_associated_executor)(handler));
  55. associated_allocator_t<decay_t<CompletionHandler>> alloc(
  56. (get_associated_allocator)(handler));
  57. ex.dispatch(boost::asio::detail::bind_handler(
  58. static_cast<CompletionHandler&&>(handler)), alloc);
  59. }
  60. };
  61. template <typename Executor>
  62. class initiate_dispatch_with_executor
  63. {
  64. public:
  65. typedef Executor executor_type;
  66. explicit initiate_dispatch_with_executor(const Executor& ex)
  67. : ex_(ex)
  68. {
  69. }
  70. executor_type get_executor() const noexcept
  71. {
  72. return ex_;
  73. }
  74. template <typename CompletionHandler, typename Function>
  75. void operator()(CompletionHandler&& handler, Function&&,
  76. enable_if_t<
  77. execution::is_executor<
  78. conditional_t<true, executor_type, CompletionHandler>
  79. >::value
  80. >* = 0,
  81. enable_if_t<
  82. !is_work_dispatcher_required<
  83. decay_t<Function>,
  84. decay_t<CompletionHandler>,
  85. Executor
  86. >::value
  87. >* = 0) const
  88. {
  89. associated_allocator_t<decay_t<CompletionHandler>> alloc(
  90. (get_associated_allocator)(handler));
  91. boost::asio::prefer(ex_, execution::allocator(alloc)).execute(
  92. boost::asio::detail::bind_handler(
  93. static_cast<CompletionHandler&&>(handler)));
  94. }
  95. template <typename CompletionHandler, typename Function>
  96. void operator()(CompletionHandler&& handler, Function&& function,
  97. enable_if_t<
  98. execution::is_executor<
  99. conditional_t<true, executor_type, CompletionHandler>
  100. >::value
  101. >* = 0,
  102. enable_if_t<
  103. is_work_dispatcher_required<
  104. decay_t<Function>,
  105. decay_t<CompletionHandler>,
  106. Executor
  107. >::value
  108. >* = 0) const
  109. {
  110. typedef decay_t<CompletionHandler> handler_t;
  111. typedef decay_t<Function> function_t;
  112. typedef associated_executor_t<handler_t, Executor> handler_ex_t;
  113. handler_ex_t handler_ex((get_associated_executor)(handler, ex_));
  114. associated_allocator_t<handler_t> alloc(
  115. (get_associated_allocator)(handler));
  116. boost::asio::prefer(ex_, execution::allocator(alloc)).execute(
  117. work_dispatcher<function_t, handler_t, handler_ex_t>(
  118. static_cast<Function&&>(function),
  119. static_cast<CompletionHandler&&>(handler), handler_ex));
  120. }
  121. template <typename CompletionHandler, typename Function>
  122. void operator()(CompletionHandler&& handler, Function&&,
  123. enable_if_t<
  124. !execution::is_executor<
  125. conditional_t<true, executor_type, CompletionHandler>
  126. >::value
  127. >* = 0,
  128. enable_if_t<
  129. !is_work_dispatcher_required<
  130. decay_t<Function>,
  131. decay_t<CompletionHandler>,
  132. Executor
  133. >::value
  134. >* = 0) const
  135. {
  136. associated_allocator_t<decay_t<CompletionHandler>> alloc(
  137. (get_associated_allocator)(handler));
  138. ex_.dispatch(boost::asio::detail::bind_handler(
  139. static_cast<CompletionHandler&&>(handler)), alloc);
  140. }
  141. template <typename CompletionHandler, typename Function>
  142. void operator()(CompletionHandler&& handler, Function&& function,
  143. enable_if_t<
  144. !execution::is_executor<
  145. conditional_t<true, executor_type, CompletionHandler>
  146. >::value
  147. >* = 0,
  148. enable_if_t<
  149. is_work_dispatcher_required<
  150. decay_t<Function>,
  151. decay_t<CompletionHandler>,
  152. Executor
  153. >::value
  154. >* = 0) const
  155. {
  156. typedef decay_t<CompletionHandler> handler_t;
  157. typedef decay_t<Function> function_t;
  158. typedef associated_executor_t<handler_t, Executor> handler_ex_t;
  159. handler_ex_t handler_ex((get_associated_executor)(handler, ex_));
  160. associated_allocator_t<handler_t> alloc(
  161. (get_associated_allocator)(handler));
  162. ex_.dispatch(work_dispatcher<function_t, handler_t, handler_ex_t>(
  163. static_cast<Function&&>(function),
  164. static_cast<CompletionHandler&&>(handler), handler_ex), alloc);
  165. }
  166. private:
  167. Executor ex_;
  168. };
  169. } // namespace detail
  170. } // namespace asio
  171. } // namespace boost
  172. #include <boost/asio/detail/pop_options.hpp>
  173. #endif // BOOST_ASIO_DETAIL_INITIATE_DISPATCH_HPP