initiate_post.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //
  2. // detail/initiate_post.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_POST_HPP
  11. #define BOOST_ASIO_DETAIL_INITIATE_POST_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/execution/relationship.hpp>
  22. #include <boost/asio/prefer.hpp>
  23. #include <boost/asio/require.hpp>
  24. #include <boost/asio/detail/push_options.hpp>
  25. namespace boost {
  26. namespace asio {
  27. namespace detail {
  28. class initiate_post
  29. {
  30. public:
  31. template <typename CompletionHandler>
  32. void operator()(CompletionHandler&& handler,
  33. enable_if_t<
  34. execution::is_executor<
  35. associated_executor_t<decay_t<CompletionHandler>>
  36. >::value
  37. >* = 0) const
  38. {
  39. associated_executor_t<decay_t<CompletionHandler>> ex(
  40. (get_associated_executor)(handler));
  41. associated_allocator_t<decay_t<CompletionHandler>> alloc(
  42. (get_associated_allocator)(handler));
  43. boost::asio::prefer(
  44. boost::asio::require(ex, execution::blocking.never),
  45. execution::relationship.fork,
  46. execution::allocator(alloc)
  47. ).execute(
  48. boost::asio::detail::bind_handler(
  49. static_cast<CompletionHandler&&>(handler)));
  50. }
  51. template <typename CompletionHandler>
  52. void operator()(CompletionHandler&& handler,
  53. enable_if_t<
  54. !execution::is_executor<
  55. associated_executor_t<decay_t<CompletionHandler>>
  56. >::value
  57. >* = 0) const
  58. {
  59. associated_executor_t<decay_t<CompletionHandler>> ex(
  60. (get_associated_executor)(handler));
  61. associated_allocator_t<decay_t<CompletionHandler>> alloc(
  62. (get_associated_allocator)(handler));
  63. ex.post(boost::asio::detail::bind_handler(
  64. static_cast<CompletionHandler&&>(handler)), alloc);
  65. }
  66. };
  67. template <typename Executor>
  68. class initiate_post_with_executor
  69. {
  70. public:
  71. typedef Executor executor_type;
  72. explicit initiate_post_with_executor(const Executor& ex)
  73. : ex_(ex)
  74. {
  75. }
  76. executor_type get_executor() const noexcept
  77. {
  78. return ex_;
  79. }
  80. template <typename CompletionHandler, typename Function>
  81. void operator()(CompletionHandler&& handler, Function&&,
  82. enable_if_t<
  83. execution::is_executor<
  84. conditional_t<true, executor_type, CompletionHandler>
  85. >::value
  86. >* = 0,
  87. enable_if_t<
  88. !is_work_dispatcher_required<
  89. decay_t<Function>,
  90. decay_t<CompletionHandler>,
  91. Executor
  92. >::value
  93. >* = 0) const
  94. {
  95. associated_allocator_t<decay_t<CompletionHandler>> alloc(
  96. (get_associated_allocator)(handler));
  97. boost::asio::prefer(
  98. boost::asio::require(ex_, execution::blocking.never),
  99. execution::relationship.fork,
  100. execution::allocator(alloc)
  101. ).execute(
  102. boost::asio::detail::bind_handler(
  103. static_cast<CompletionHandler&&>(handler)));
  104. }
  105. template <typename CompletionHandler, typename Function>
  106. void operator()(CompletionHandler&& handler, Function&& function,
  107. enable_if_t<
  108. execution::is_executor<
  109. conditional_t<true, executor_type, CompletionHandler>
  110. >::value
  111. >* = 0,
  112. enable_if_t<
  113. is_work_dispatcher_required<
  114. decay_t<Function>,
  115. decay_t<CompletionHandler>,
  116. Executor
  117. >::value
  118. >* = 0) const
  119. {
  120. typedef decay_t<CompletionHandler> handler_t;
  121. typedef decay_t<Function> function_t;
  122. typedef associated_executor_t<handler_t, Executor> handler_ex_t;
  123. handler_ex_t handler_ex((get_associated_executor)(handler, ex_));
  124. associated_allocator_t<handler_t> alloc(
  125. (get_associated_allocator)(handler));
  126. boost::asio::prefer(
  127. boost::asio::require(ex_, execution::blocking.never),
  128. execution::relationship.fork,
  129. execution::allocator(alloc)
  130. ).execute(
  131. work_dispatcher<function_t, handler_t, handler_ex_t>(
  132. static_cast<Function&&>(function),
  133. static_cast<CompletionHandler&&>(handler), handler_ex));
  134. }
  135. template <typename CompletionHandler, typename Function>
  136. void operator()(CompletionHandler&& handler, Function&&,
  137. enable_if_t<
  138. !execution::is_executor<
  139. conditional_t<true, executor_type, CompletionHandler>
  140. >::value
  141. >* = 0,
  142. enable_if_t<
  143. !is_work_dispatcher_required<
  144. decay_t<Function>,
  145. decay_t<CompletionHandler>,
  146. Executor
  147. >::value
  148. >* = 0) const
  149. {
  150. associated_allocator_t<decay_t<CompletionHandler>> alloc(
  151. (get_associated_allocator)(handler));
  152. ex_.post(boost::asio::detail::bind_handler(
  153. static_cast<CompletionHandler&&>(handler)), alloc);
  154. }
  155. template <typename CompletionHandler, typename Function>
  156. void operator()(CompletionHandler&& handler, Function&& function,
  157. enable_if_t<
  158. !execution::is_executor<
  159. conditional_t<true, executor_type, CompletionHandler>
  160. >::value
  161. >* = 0,
  162. enable_if_t<
  163. is_work_dispatcher_required<
  164. decay_t<Function>,
  165. decay_t<CompletionHandler>,
  166. Executor
  167. >::value
  168. >* = 0) const
  169. {
  170. typedef decay_t<CompletionHandler> handler_t;
  171. typedef decay_t<Function> function_t;
  172. typedef associated_executor_t<handler_t, Executor> handler_ex_t;
  173. handler_ex_t handler_ex((get_associated_executor)(handler, ex_));
  174. associated_allocator_t<handler_t> alloc(
  175. (get_associated_allocator)(handler));
  176. ex_.post(work_dispatcher<function_t, handler_t, handler_ex_t>(
  177. static_cast<Function&&>(function),
  178. static_cast<CompletionHandler&&>(handler), handler_ex), alloc);
  179. }
  180. private:
  181. Executor ex_;
  182. };
  183. } // namespace detail
  184. } // namespace asio
  185. } // namespace boost
  186. #include <boost/asio/detail/pop_options.hpp>
  187. #endif // BOOST_ASIO_DETAIL_INITIATE_POST_HPP