initiate_post.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // detail/initiate_post.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_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>
  81. void operator()(CompletionHandler&& handler,
  82. enable_if_t<
  83. execution::is_executor<
  84. conditional_t<true, executor_type, CompletionHandler>
  85. >::value
  86. >* = 0,
  87. enable_if_t<
  88. !detail::is_work_dispatcher_required<
  89. decay_t<CompletionHandler>,
  90. Executor
  91. >::value
  92. >* = 0) const
  93. {
  94. associated_allocator_t<decay_t<CompletionHandler>> alloc(
  95. (get_associated_allocator)(handler));
  96. boost::asio::prefer(
  97. boost::asio::require(ex_, execution::blocking.never),
  98. execution::relationship.fork,
  99. execution::allocator(alloc)
  100. ).execute(
  101. boost::asio::detail::bind_handler(
  102. static_cast<CompletionHandler&&>(handler)));
  103. }
  104. template <typename CompletionHandler>
  105. void operator()(CompletionHandler&& handler,
  106. enable_if_t<
  107. execution::is_executor<
  108. conditional_t<true, executor_type, CompletionHandler>
  109. >::value
  110. >* = 0,
  111. enable_if_t<
  112. detail::is_work_dispatcher_required<
  113. decay_t<CompletionHandler>,
  114. Executor
  115. >::value
  116. >* = 0) const
  117. {
  118. typedef decay_t<CompletionHandler> handler_t;
  119. typedef associated_executor_t<handler_t, Executor> handler_ex_t;
  120. handler_ex_t handler_ex((get_associated_executor)(handler, ex_));
  121. associated_allocator_t<handler_t> alloc(
  122. (get_associated_allocator)(handler));
  123. boost::asio::prefer(
  124. boost::asio::require(ex_, execution::blocking.never),
  125. execution::relationship.fork,
  126. execution::allocator(alloc)
  127. ).execute(
  128. detail::work_dispatcher<handler_t, handler_ex_t>(
  129. static_cast<CompletionHandler&&>(handler), handler_ex));
  130. }
  131. template <typename CompletionHandler>
  132. void operator()(CompletionHandler&& handler,
  133. enable_if_t<
  134. !execution::is_executor<
  135. conditional_t<true, executor_type, CompletionHandler>
  136. >::value
  137. >* = 0,
  138. enable_if_t<
  139. !detail::is_work_dispatcher_required<
  140. decay_t<CompletionHandler>,
  141. Executor
  142. >::value
  143. >* = 0) const
  144. {
  145. associated_allocator_t<decay_t<CompletionHandler>> alloc(
  146. (get_associated_allocator)(handler));
  147. ex_.post(boost::asio::detail::bind_handler(
  148. static_cast<CompletionHandler&&>(handler)), alloc);
  149. }
  150. template <typename CompletionHandler>
  151. void operator()(CompletionHandler&& handler,
  152. enable_if_t<
  153. !execution::is_executor<
  154. conditional_t<true, executor_type, CompletionHandler>
  155. >::value
  156. >* = 0,
  157. enable_if_t<
  158. detail::is_work_dispatcher_required<
  159. decay_t<CompletionHandler>,
  160. Executor
  161. >::value
  162. >* = 0) const
  163. {
  164. typedef decay_t<CompletionHandler> handler_t;
  165. typedef associated_executor_t<handler_t, Executor> handler_ex_t;
  166. handler_ex_t handler_ex((get_associated_executor)(handler, ex_));
  167. associated_allocator_t<handler_t> alloc(
  168. (get_associated_allocator)(handler));
  169. ex_.post(detail::work_dispatcher<handler_t, handler_ex_t>(
  170. static_cast<CompletionHandler&&>(handler), handler_ex), alloc);
  171. }
  172. private:
  173. Executor ex_;
  174. };
  175. } // namespace detail
  176. } // namespace asio
  177. } // namespace boost
  178. #include <boost/asio/detail/pop_options.hpp>
  179. #endif // BOOST_ASIO_DETAIL_INITIATE_POST_HPP