defer.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. //
  2. // impl/defer.hpp
  3. // ~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 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_IMPL_DEFER_HPP
  11. #define BOOST_ASIO_IMPL_DEFER_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_defer
  29. {
  30. public:
  31. template <typename CompletionHandler>
  32. void operator()(BOOST_ASIO_MOVE_ARG(CompletionHandler) handler,
  33. typename enable_if<
  34. execution::is_executor<
  35. typename associated_executor<
  36. typename decay<CompletionHandler>::type
  37. >::type
  38. >::value
  39. >::type* = 0) const
  40. {
  41. typedef typename decay<CompletionHandler>::type handler_t;
  42. typename associated_executor<handler_t>::type ex(
  43. (get_associated_executor)(handler));
  44. typename associated_allocator<handler_t>::type alloc(
  45. (get_associated_allocator)(handler));
  46. execution::execute(
  47. boost::asio::prefer(
  48. boost::asio::require(ex, execution::blocking.never),
  49. execution::relationship.continuation,
  50. execution::allocator(alloc)),
  51. boost::asio::detail::bind_handler(
  52. BOOST_ASIO_MOVE_CAST(CompletionHandler)(handler)));
  53. }
  54. template <typename CompletionHandler>
  55. void operator()(BOOST_ASIO_MOVE_ARG(CompletionHandler) handler,
  56. typename enable_if<
  57. !execution::is_executor<
  58. typename associated_executor<
  59. typename decay<CompletionHandler>::type
  60. >::type
  61. >::value
  62. >::type* = 0) const
  63. {
  64. typedef typename decay<CompletionHandler>::type handler_t;
  65. typename associated_executor<handler_t>::type ex(
  66. (get_associated_executor)(handler));
  67. typename associated_allocator<handler_t>::type alloc(
  68. (get_associated_allocator)(handler));
  69. ex.defer(boost::asio::detail::bind_handler(
  70. BOOST_ASIO_MOVE_CAST(CompletionHandler)(handler)), alloc);
  71. }
  72. };
  73. template <typename Executor>
  74. class initiate_defer_with_executor
  75. {
  76. public:
  77. typedef Executor executor_type;
  78. explicit initiate_defer_with_executor(const Executor& ex)
  79. : ex_(ex)
  80. {
  81. }
  82. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  83. {
  84. return ex_;
  85. }
  86. template <typename CompletionHandler>
  87. void operator()(BOOST_ASIO_MOVE_ARG(CompletionHandler) handler,
  88. typename enable_if<
  89. execution::is_executor<
  90. typename conditional<true, executor_type, CompletionHandler>::type
  91. >::value
  92. >::type* = 0,
  93. typename enable_if<
  94. !detail::is_work_dispatcher_required<
  95. typename decay<CompletionHandler>::type,
  96. Executor
  97. >::value
  98. >::type* = 0) const
  99. {
  100. typedef typename decay<CompletionHandler>::type handler_t;
  101. typename associated_allocator<handler_t>::type alloc(
  102. (get_associated_allocator)(handler));
  103. execution::execute(
  104. boost::asio::prefer(
  105. boost::asio::require(ex_, execution::blocking.never),
  106. execution::relationship.continuation,
  107. execution::allocator(alloc)),
  108. boost::asio::detail::bind_handler(
  109. BOOST_ASIO_MOVE_CAST(CompletionHandler)(handler)));
  110. }
  111. template <typename CompletionHandler>
  112. void operator()(BOOST_ASIO_MOVE_ARG(CompletionHandler) handler,
  113. typename enable_if<
  114. execution::is_executor<
  115. typename conditional<true, executor_type, CompletionHandler>::type
  116. >::value
  117. >::type* = 0,
  118. typename enable_if<
  119. detail::is_work_dispatcher_required<
  120. typename decay<CompletionHandler>::type,
  121. Executor
  122. >::value
  123. >::type* = 0) const
  124. {
  125. typedef typename decay<CompletionHandler>::type handler_t;
  126. typedef typename associated_executor<
  127. handler_t, Executor>::type handler_ex_t;
  128. handler_ex_t handler_ex((get_associated_executor)(handler, ex_));
  129. typename associated_allocator<handler_t>::type alloc(
  130. (get_associated_allocator)(handler));
  131. execution::execute(
  132. boost::asio::prefer(
  133. boost::asio::require(ex_, execution::blocking.never),
  134. execution::relationship.continuation,
  135. execution::allocator(alloc)),
  136. detail::work_dispatcher<handler_t, handler_ex_t>(
  137. BOOST_ASIO_MOVE_CAST(CompletionHandler)(handler), handler_ex));
  138. }
  139. template <typename CompletionHandler>
  140. void operator()(BOOST_ASIO_MOVE_ARG(CompletionHandler) handler,
  141. typename enable_if<
  142. !execution::is_executor<
  143. typename conditional<true, executor_type, CompletionHandler>::type
  144. >::value
  145. >::type* = 0,
  146. typename enable_if<
  147. !detail::is_work_dispatcher_required<
  148. typename decay<CompletionHandler>::type,
  149. Executor
  150. >::value
  151. >::type* = 0) const
  152. {
  153. typedef typename decay<CompletionHandler>::type handler_t;
  154. typename associated_allocator<handler_t>::type alloc(
  155. (get_associated_allocator)(handler));
  156. ex_.defer(boost::asio::detail::bind_handler(
  157. BOOST_ASIO_MOVE_CAST(CompletionHandler)(handler)), alloc);
  158. }
  159. template <typename CompletionHandler>
  160. void operator()(BOOST_ASIO_MOVE_ARG(CompletionHandler) handler,
  161. typename enable_if<
  162. !execution::is_executor<
  163. typename conditional<true, executor_type, CompletionHandler>::type
  164. >::value
  165. >::type* = 0,
  166. typename enable_if<
  167. detail::is_work_dispatcher_required<
  168. typename decay<CompletionHandler>::type,
  169. Executor
  170. >::value
  171. >::type* = 0) const
  172. {
  173. typedef typename decay<CompletionHandler>::type handler_t;
  174. typedef typename associated_executor<
  175. handler_t, Executor>::type handler_ex_t;
  176. handler_ex_t handler_ex((get_associated_executor)(handler, ex_));
  177. typename associated_allocator<handler_t>::type alloc(
  178. (get_associated_allocator)(handler));
  179. ex_.defer(detail::work_dispatcher<handler_t, handler_ex_t>(
  180. BOOST_ASIO_MOVE_CAST(CompletionHandler)(handler),
  181. handler_ex), alloc);
  182. }
  183. private:
  184. Executor ex_;
  185. };
  186. } // namespace detail
  187. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) NullaryToken>
  188. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(NullaryToken, void()) defer(
  189. BOOST_ASIO_MOVE_ARG(NullaryToken) token)
  190. {
  191. return async_initiate<NullaryToken, void()>(
  192. detail::initiate_defer(), token);
  193. }
  194. template <typename Executor,
  195. BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) NullaryToken>
  196. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(NullaryToken, void()) defer(
  197. const Executor& ex, BOOST_ASIO_MOVE_ARG(NullaryToken) token,
  198. typename constraint<
  199. execution::is_executor<Executor>::value || is_executor<Executor>::value
  200. >::type)
  201. {
  202. return async_initiate<NullaryToken, void()>(
  203. detail::initiate_defer_with_executor<Executor>(ex), token);
  204. }
  205. template <typename ExecutionContext,
  206. BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) NullaryToken>
  207. inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(NullaryToken, void()) defer(
  208. ExecutionContext& ctx, BOOST_ASIO_MOVE_ARG(NullaryToken) token,
  209. typename constraint<is_convertible<
  210. ExecutionContext&, execution_context&>::value>::type)
  211. {
  212. return async_initiate<NullaryToken, void()>(
  213. detail::initiate_defer_with_executor<
  214. typename ExecutionContext::executor_type>(
  215. ctx.get_executor()), token);
  216. }
  217. } // namespace asio
  218. } // namespace boost
  219. #include <boost/asio/detail/pop_options.hpp>
  220. #endif // BOOST_ASIO_IMPL_DEFER_HPP