compose.hpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. //
  2. // compose.hpp
  3. // ~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 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 ASIO_COMPOSE_HPP
  11. #define ASIO_COMPOSE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include "asio/associated_executor.hpp"
  17. #include "asio/async_result.hpp"
  18. #include "asio/detail/base_from_cancellation_state.hpp"
  19. #include "asio/detail/composed_work.hpp"
  20. #include "asio/detail/handler_cont_helpers.hpp"
  21. #include "asio/detail/type_traits.hpp"
  22. #include "asio/detail/push_options.hpp"
  23. namespace asio {
  24. namespace detail {
  25. template <typename Impl, typename Work, typename Handler, typename Signature>
  26. class composed_op;
  27. template <typename Impl, typename Work, typename Handler,
  28. typename R, typename... Args>
  29. class composed_op<Impl, Work, Handler, R(Args...)>
  30. : public base_from_cancellation_state<Handler>
  31. {
  32. public:
  33. template <typename I, typename W, typename H>
  34. composed_op(I&& impl,
  35. W&& work,
  36. H&& handler)
  37. : base_from_cancellation_state<Handler>(
  38. handler, enable_terminal_cancellation()),
  39. impl_(static_cast<I&&>(impl)),
  40. work_(static_cast<W&&>(work)),
  41. handler_(static_cast<H&&>(handler)),
  42. invocations_(0)
  43. {
  44. }
  45. composed_op(composed_op&& other)
  46. : base_from_cancellation_state<Handler>(
  47. static_cast<base_from_cancellation_state<Handler>&&>(other)),
  48. impl_(static_cast<Impl&&>(other.impl_)),
  49. work_(static_cast<Work&&>(other.work_)),
  50. handler_(static_cast<Handler&&>(other.handler_)),
  51. invocations_(other.invocations_)
  52. {
  53. }
  54. typedef typename composed_work_guard<
  55. typename Work::head_type>::executor_type io_executor_type;
  56. io_executor_type get_io_executor() const noexcept
  57. {
  58. return work_.head_.get_executor();
  59. }
  60. typedef associated_executor_t<Handler, io_executor_type> executor_type;
  61. executor_type get_executor() const noexcept
  62. {
  63. return (get_associated_executor)(handler_, work_.head_.get_executor());
  64. }
  65. typedef associated_allocator_t<Handler, std::allocator<void>> allocator_type;
  66. allocator_type get_allocator() const noexcept
  67. {
  68. return (get_associated_allocator)(handler_, std::allocator<void>());
  69. }
  70. template<typename... T>
  71. void operator()(T&&... t)
  72. {
  73. if (invocations_ < ~0u)
  74. ++invocations_;
  75. this->get_cancellation_state().slot().clear();
  76. impl_(*this, static_cast<T&&>(t)...);
  77. }
  78. void complete(Args... args)
  79. {
  80. this->work_.reset();
  81. static_cast<Handler&&>(this->handler_)(static_cast<Args&&>(args)...);
  82. }
  83. void reset_cancellation_state()
  84. {
  85. base_from_cancellation_state<Handler>::reset_cancellation_state(handler_);
  86. }
  87. template <typename Filter>
  88. void reset_cancellation_state(Filter&& filter)
  89. {
  90. base_from_cancellation_state<Handler>::reset_cancellation_state(handler_,
  91. static_cast<Filter&&>(filter));
  92. }
  93. template <typename InFilter, typename OutFilter>
  94. void reset_cancellation_state(InFilter&& in_filter,
  95. OutFilter&& out_filter)
  96. {
  97. base_from_cancellation_state<Handler>::reset_cancellation_state(handler_,
  98. static_cast<InFilter&&>(in_filter),
  99. static_cast<OutFilter&&>(out_filter));
  100. }
  101. cancellation_type_t cancelled() const noexcept
  102. {
  103. return base_from_cancellation_state<Handler>::cancelled();
  104. }
  105. //private:
  106. Impl impl_;
  107. Work work_;
  108. Handler handler_;
  109. unsigned invocations_;
  110. };
  111. template <typename Impl, typename Work, typename Handler, typename Signature>
  112. inline bool asio_handler_is_continuation(
  113. composed_op<Impl, Work, Handler, Signature>* this_handler)
  114. {
  115. return this_handler->invocations_ > 1 ? true
  116. : asio_handler_cont_helpers::is_continuation(
  117. this_handler->handler_);
  118. }
  119. template <typename Signature, typename Executors>
  120. class initiate_composed_op
  121. {
  122. public:
  123. typedef typename composed_io_executors<Executors>::head_type executor_type;
  124. template <typename T>
  125. explicit initiate_composed_op(int, T&& executors)
  126. : executors_(static_cast<T&&>(executors))
  127. {
  128. }
  129. executor_type get_executor() const noexcept
  130. {
  131. return executors_.head_;
  132. }
  133. template <typename Handler, typename Impl>
  134. void operator()(Handler&& handler,
  135. Impl&& impl) const
  136. {
  137. composed_op<decay_t<Impl>, composed_work<Executors>,
  138. decay_t<Handler>, Signature>(
  139. static_cast<Impl&&>(impl),
  140. composed_work<Executors>(executors_),
  141. static_cast<Handler&&>(handler))();
  142. }
  143. private:
  144. composed_io_executors<Executors> executors_;
  145. };
  146. template <typename Signature, typename Executors>
  147. inline initiate_composed_op<Signature, Executors> make_initiate_composed_op(
  148. composed_io_executors<Executors>&& executors)
  149. {
  150. return initiate_composed_op<Signature, Executors>(0,
  151. static_cast<composed_io_executors<Executors>&&>(executors));
  152. }
  153. } // namespace detail
  154. #if !defined(GENERATING_DOCUMENTATION)
  155. template <template <typename, typename> class Associator,
  156. typename Impl, typename Work, typename Handler,
  157. typename Signature, typename DefaultCandidate>
  158. struct associator<Associator,
  159. detail::composed_op<Impl, Work, Handler, Signature>,
  160. DefaultCandidate>
  161. : Associator<Handler, DefaultCandidate>
  162. {
  163. static typename Associator<Handler, DefaultCandidate>::type get(
  164. const detail::composed_op<Impl, Work, Handler, Signature>& h) noexcept
  165. {
  166. return Associator<Handler, DefaultCandidate>::get(h.handler_);
  167. }
  168. static auto get(const detail::composed_op<Impl, Work, Handler, Signature>& h,
  169. const DefaultCandidate& c) noexcept
  170. -> decltype(Associator<Handler, DefaultCandidate>::get(h.handler_, c))
  171. {
  172. return Associator<Handler, DefaultCandidate>::get(h.handler_, c);
  173. }
  174. };
  175. #endif // !defined(GENERATING_DOCUMENTATION)
  176. /// Launch an asynchronous operation with a stateful implementation.
  177. /**
  178. * The async_compose function simplifies the implementation of composed
  179. * asynchronous operations automatically wrapping a stateful function object
  180. * with a conforming intermediate completion handler.
  181. *
  182. * @param implementation A function object that contains the implementation of
  183. * the composed asynchronous operation. The first argument to the function
  184. * object is a non-const reference to the enclosing intermediate completion
  185. * handler. The remaining arguments are any arguments that originate from the
  186. * completion handlers of any asynchronous operations performed by the
  187. * implementation.
  188. *
  189. * @param token The completion token.
  190. *
  191. * @param io_objects_or_executors Zero or more I/O objects or I/O executors for
  192. * which outstanding work must be maintained.
  193. *
  194. * @par Per-Operation Cancellation
  195. * By default, terminal per-operation cancellation is enabled for
  196. * composed operations that are implemented using @c async_compose. To
  197. * disable cancellation for the composed operation, or to alter its
  198. * supported cancellation types, call the @c self object's @c
  199. * reset_cancellation_state function.
  200. *
  201. * @par Example:
  202. *
  203. * @code struct async_echo_implementation
  204. * {
  205. * tcp::socket& socket_;
  206. * asio::mutable_buffer buffer_;
  207. * enum { starting, reading, writing } state_;
  208. *
  209. * template <typename Self>
  210. * void operator()(Self& self,
  211. * asio::error_code error = {},
  212. * std::size_t n = 0)
  213. * {
  214. * switch (state_)
  215. * {
  216. * case starting:
  217. * state_ = reading;
  218. * socket_.async_read_some(
  219. * buffer_, std::move(self));
  220. * break;
  221. * case reading:
  222. * if (error)
  223. * {
  224. * self.complete(error, 0);
  225. * }
  226. * else
  227. * {
  228. * state_ = writing;
  229. * asio::async_write(socket_, buffer_,
  230. * asio::transfer_exactly(n),
  231. * std::move(self));
  232. * }
  233. * break;
  234. * case writing:
  235. * self.complete(error, n);
  236. * break;
  237. * }
  238. * }
  239. * };
  240. *
  241. * template <typename CompletionToken>
  242. * auto async_echo(tcp::socket& socket,
  243. * asio::mutable_buffer buffer,
  244. * CompletionToken&& token) ->
  245. * decltype(
  246. * asio::async_compose<CompletionToken,
  247. * void(asio::error_code, std::size_t)>(
  248. * std::declval<async_echo_implementation>(),
  249. * token, socket))
  250. * {
  251. * return asio::async_compose<CompletionToken,
  252. * void(asio::error_code, std::size_t)>(
  253. * async_echo_implementation{socket, buffer,
  254. * async_echo_implementation::starting},
  255. * token, socket);
  256. * } @endcode
  257. */
  258. template <typename CompletionToken, typename Signature,
  259. typename Implementation, typename... IoObjectsOrExecutors>
  260. auto async_compose(Implementation&& implementation,
  261. type_identity_t<CompletionToken>& token,
  262. IoObjectsOrExecutors&&... io_objects_or_executors)
  263. -> decltype(
  264. async_initiate<CompletionToken, Signature>(
  265. detail::make_initiate_composed_op<Signature>(
  266. detail::make_composed_io_executors(
  267. detail::get_composed_io_executor(
  268. static_cast<IoObjectsOrExecutors&&>(
  269. io_objects_or_executors))...)),
  270. token, static_cast<Implementation&&>(implementation)))
  271. {
  272. return async_initiate<CompletionToken, Signature>(
  273. detail::make_initiate_composed_op<Signature>(
  274. detail::make_composed_io_executors(
  275. detail::get_composed_io_executor(
  276. static_cast<IoObjectsOrExecutors&&>(
  277. io_objects_or_executors))...)),
  278. token, static_cast<Implementation&&>(implementation));
  279. }
  280. } // namespace asio
  281. #include "asio/detail/pop_options.hpp"
  282. #endif // ASIO_COMPOSE_HPP