compose.hpp 9.9 KB

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