defer.hpp 7.8 KB

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