initiate_dispatch.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // detail/initiate_dispatch.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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>
  75. void operator()(CompletionHandler&& handler,
  76. enable_if_t<
  77. execution::is_executor<
  78. conditional_t<true, executor_type, CompletionHandler>
  79. >::value
  80. >* = 0,
  81. enable_if_t<
  82. !detail::is_work_dispatcher_required<
  83. decay_t<CompletionHandler>,
  84. Executor
  85. >::value
  86. >* = 0) const
  87. {
  88. associated_allocator_t<decay_t<CompletionHandler>> alloc(
  89. (get_associated_allocator)(handler));
  90. boost::asio::prefer(ex_, execution::allocator(alloc)).execute(
  91. boost::asio::detail::bind_handler(
  92. static_cast<CompletionHandler&&>(handler)));
  93. }
  94. template <typename CompletionHandler>
  95. void operator()(CompletionHandler&& handler,
  96. enable_if_t<
  97. execution::is_executor<
  98. conditional_t<true, executor_type, CompletionHandler>
  99. >::value
  100. >* = 0,
  101. enable_if_t<
  102. detail::is_work_dispatcher_required<
  103. decay_t<CompletionHandler>,
  104. Executor
  105. >::value
  106. >* = 0) const
  107. {
  108. typedef decay_t<CompletionHandler> handler_t;
  109. typedef associated_executor_t<handler_t, Executor> handler_ex_t;
  110. handler_ex_t handler_ex((get_associated_executor)(handler, ex_));
  111. associated_allocator_t<handler_t> alloc(
  112. (get_associated_allocator)(handler));
  113. boost::asio::prefer(ex_, execution::allocator(alloc)).execute(
  114. detail::work_dispatcher<handler_t, handler_ex_t>(
  115. static_cast<CompletionHandler&&>(handler), handler_ex));
  116. }
  117. template <typename CompletionHandler>
  118. void operator()(CompletionHandler&& handler,
  119. enable_if_t<
  120. !execution::is_executor<
  121. conditional_t<true, executor_type, CompletionHandler>
  122. >::value
  123. >* = 0,
  124. enable_if_t<
  125. !detail::is_work_dispatcher_required<
  126. decay_t<CompletionHandler>,
  127. Executor
  128. >::value
  129. >* = 0) const
  130. {
  131. associated_allocator_t<decay_t<CompletionHandler>> alloc(
  132. (get_associated_allocator)(handler));
  133. ex_.dispatch(boost::asio::detail::bind_handler(
  134. static_cast<CompletionHandler&&>(handler)), alloc);
  135. }
  136. template <typename CompletionHandler>
  137. void operator()(CompletionHandler&& handler,
  138. enable_if_t<
  139. !execution::is_executor<
  140. conditional_t<true, executor_type, CompletionHandler>
  141. >::value
  142. >* = 0,
  143. enable_if_t<
  144. detail::is_work_dispatcher_required<
  145. decay_t<CompletionHandler>,
  146. Executor
  147. >::value
  148. >* = 0) const
  149. {
  150. typedef decay_t<CompletionHandler> handler_t;
  151. typedef associated_executor_t<handler_t, Executor> handler_ex_t;
  152. handler_ex_t handler_ex((get_associated_executor)(handler, ex_));
  153. associated_allocator_t<handler_t> alloc(
  154. (get_associated_allocator)(handler));
  155. ex_.dispatch(detail::work_dispatcher<handler_t, handler_ex_t>(
  156. static_cast<CompletionHandler&&>(handler), handler_ex), alloc);
  157. }
  158. private:
  159. Executor ex_;
  160. };
  161. } // namespace detail
  162. } // namespace asio
  163. } // namespace boost
  164. #include <boost/asio/detail/pop_options.hpp>
  165. #endif // BOOST_ASIO_DETAIL_INITIATE_DISPATCH_HPP