cancel_at.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. //
  2. // cancel_at.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_CANCEL_AT_HPP
  11. #define BOOST_ASIO_CANCEL_AT_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/basic_waitable_timer.hpp>
  17. #include <boost/asio/cancellation_type.hpp>
  18. #include <boost/asio/detail/chrono.hpp>
  19. #include <boost/asio/detail/type_traits.hpp>
  20. #include <boost/asio/wait_traits.hpp>
  21. #include <boost/asio/detail/push_options.hpp>
  22. namespace boost {
  23. namespace asio {
  24. /// A @ref completion_token adapter that cancels an operation at a given time.
  25. /**
  26. * The cancel_at_t class is used to indicate that an asynchronous operation
  27. * should be cancelled if not complete at the specified absolute time.
  28. */
  29. template <typename CompletionToken, typename Clock,
  30. typename WaitTraits = boost::asio::wait_traits<Clock>>
  31. class cancel_at_t
  32. {
  33. public:
  34. /// Constructor.
  35. template <typename T>
  36. cancel_at_t(T&& completion_token, const typename Clock::time_point& expiry,
  37. cancellation_type_t cancel_type = cancellation_type::terminal)
  38. : token_(static_cast<T&&>(completion_token)),
  39. expiry_(expiry),
  40. cancel_type_(cancel_type)
  41. {
  42. }
  43. //private:
  44. CompletionToken token_;
  45. typename Clock::time_point expiry_;
  46. cancellation_type_t cancel_type_;
  47. };
  48. /// A @ref completion_token adapter that cancels an operation at a given time.
  49. /**
  50. * The cancel_at_timer class is used to indicate that an asynchronous operation
  51. * should be cancelled if not complete at the specified absolute time.
  52. */
  53. template <typename CompletionToken, typename Clock,
  54. typename WaitTraits = boost::asio::wait_traits<Clock>,
  55. typename Executor = any_io_executor>
  56. class cancel_at_timer
  57. {
  58. public:
  59. /// Constructor.
  60. template <typename T>
  61. cancel_at_timer(T&& completion_token,
  62. basic_waitable_timer<Clock, WaitTraits, Executor>& timer,
  63. const typename Clock::time_point& expiry,
  64. cancellation_type_t cancel_type = cancellation_type::terminal)
  65. : token_(static_cast<T&&>(completion_token)),
  66. timer_(timer),
  67. expiry_(expiry),
  68. cancel_type_(cancel_type)
  69. {
  70. }
  71. //private:
  72. CompletionToken token_;
  73. basic_waitable_timer<Clock, WaitTraits, Executor>& timer_;
  74. typename Clock::time_point expiry_;
  75. cancellation_type_t cancel_type_;
  76. };
  77. /// A function object type that adapts a @ref completion_token to cancel an
  78. /// operation at a given time.
  79. /**
  80. * May also be used directly as a completion token, in which case it adapts the
  81. * asynchronous operation's default completion token (or boost::asio::deferred
  82. * if no default is available).
  83. */
  84. template <typename Clock, typename WaitTraits = boost::asio::wait_traits<Clock>>
  85. class partial_cancel_at
  86. {
  87. public:
  88. /// Constructor that specifies the expiry and cancellation type.
  89. explicit partial_cancel_at(const typename Clock::time_point& expiry,
  90. cancellation_type_t cancel_type = cancellation_type::terminal)
  91. : expiry_(expiry),
  92. cancel_type_(cancel_type)
  93. {
  94. }
  95. /// Adapt a @ref completion_token to specify that the completion handler
  96. /// arguments should be combined into a single tuple argument.
  97. template <typename CompletionToken>
  98. BOOST_ASIO_NODISCARD inline
  99. constexpr cancel_at_t<decay_t<CompletionToken>, Clock, WaitTraits>
  100. operator()(CompletionToken&& completion_token) const
  101. {
  102. return cancel_at_t<decay_t<CompletionToken>, Clock, WaitTraits>(
  103. static_cast<CompletionToken&&>(completion_token),
  104. expiry_, cancel_type_);
  105. }
  106. //private:
  107. typename Clock::time_point expiry_;
  108. cancellation_type_t cancel_type_;
  109. };
  110. /// A function object type that adapts a @ref completion_token to cancel an
  111. /// operation at a given time.
  112. /**
  113. * May also be used directly as a completion token, in which case it adapts the
  114. * asynchronous operation's default completion token (or boost::asio::deferred
  115. * if no default is available).
  116. */
  117. template <typename Clock, typename WaitTraits = boost::asio::wait_traits<Clock>,
  118. typename Executor = any_io_executor>
  119. class partial_cancel_at_timer
  120. {
  121. public:
  122. /// Constructor that specifies the expiry and cancellation type.
  123. explicit partial_cancel_at_timer(
  124. basic_waitable_timer<Clock, WaitTraits, Executor>& timer,
  125. const typename Clock::time_point& expiry,
  126. cancellation_type_t cancel_type = cancellation_type::terminal)
  127. : timer_(timer),
  128. expiry_(expiry),
  129. cancel_type_(cancel_type)
  130. {
  131. }
  132. /// Adapt a @ref completion_token to specify that the completion handler
  133. /// arguments should be combined into a single tuple argument.
  134. template <typename CompletionToken>
  135. BOOST_ASIO_NODISCARD inline
  136. cancel_at_timer<decay_t<CompletionToken>, Clock, WaitTraits, Executor>
  137. operator()(CompletionToken&& completion_token) const
  138. {
  139. return cancel_at_timer<decay_t<CompletionToken>,
  140. Clock, WaitTraits, Executor>(
  141. static_cast<CompletionToken&&>(completion_token),
  142. timer_, expiry_, cancel_type_);
  143. }
  144. //private:
  145. basic_waitable_timer<Clock, WaitTraits, Executor>& timer_;
  146. typename Clock::time_point expiry_;
  147. cancellation_type_t cancel_type_;
  148. };
  149. /// Create a partial completion token adapter that cancels an operation if not
  150. /// complete by the specified absolute time.
  151. /**
  152. * @par Thread Safety
  153. * When an asynchronous operation is used with cancel_at, a timer async_wait
  154. * operation is performed in parallel to the main operation. If this parallel
  155. * async_wait completes first, a cancellation request is emitted to cancel the
  156. * main operation. Consequently, the application must ensure that the
  157. * asynchronous operation is performed within an implicit or explicit strand.
  158. */
  159. template <typename Clock, typename Duration>
  160. BOOST_ASIO_NODISCARD inline partial_cancel_at<Clock>
  161. cancel_at(const chrono::time_point<Clock, Duration>& expiry,
  162. cancellation_type_t cancel_type = cancellation_type::terminal)
  163. {
  164. return partial_cancel_at<Clock>(expiry, cancel_type);
  165. }
  166. /// Create a partial completion token adapter that cancels an operation if not
  167. /// complete by the specified absolute time.
  168. /**
  169. * @par Thread Safety
  170. * When an asynchronous operation is used with cancel_at, a timer async_wait
  171. * operation is performed in parallel to the main operation. If this parallel
  172. * async_wait completes first, a cancellation request is emitted to cancel the
  173. * main operation. Consequently, the application must ensure that the
  174. * asynchronous operation is performed within an implicit or explicit strand.
  175. */
  176. template <typename Clock, typename WaitTraits,
  177. typename Executor, typename Duration>
  178. BOOST_ASIO_NODISCARD inline partial_cancel_at_timer<Clock, WaitTraits, Executor>
  179. cancel_at(basic_waitable_timer<Clock, WaitTraits, Executor>& timer,
  180. const chrono::time_point<Clock, Duration>& expiry,
  181. cancellation_type_t cancel_type = cancellation_type::terminal)
  182. {
  183. return partial_cancel_at_timer<Clock, WaitTraits, Executor>(
  184. timer, expiry, cancel_type);
  185. }
  186. /// Adapt a @ref completion_token to cancel an operation if not complete by the
  187. /// specified absolute time.
  188. /**
  189. * @par Thread Safety
  190. * When an asynchronous operation is used with cancel_at, a timer async_wait
  191. * operation is performed in parallel to the main operation. If this parallel
  192. * async_wait completes first, a cancellation request is emitted to cancel the
  193. * main operation. Consequently, the application must ensure that the
  194. * asynchronous operation is performed within an implicit or explicit strand.
  195. */
  196. template <typename CompletionToken, typename Clock, typename Duration>
  197. BOOST_ASIO_NODISCARD inline cancel_at_t<decay_t<CompletionToken>, Clock>
  198. cancel_at(const chrono::time_point<Clock, Duration>& expiry,
  199. CompletionToken&& completion_token)
  200. {
  201. return cancel_at_t<decay_t<CompletionToken>, Clock>(
  202. static_cast<CompletionToken&&>(completion_token),
  203. expiry, cancellation_type::terminal);
  204. }
  205. /// Adapt a @ref completion_token to cancel an operation if not complete by the
  206. /// specified absolute time.
  207. /**
  208. * @par Thread Safety
  209. * When an asynchronous operation is used with cancel_at, a timer async_wait
  210. * operation is performed in parallel to the main operation. If this parallel
  211. * async_wait completes first, a cancellation request is emitted to cancel the
  212. * main operation. Consequently, the application must ensure that the
  213. * asynchronous operation is performed within an implicit or explicit strand.
  214. */
  215. template <typename CompletionToken, typename Clock, typename Duration>
  216. BOOST_ASIO_NODISCARD inline cancel_at_t<decay_t<CompletionToken>, Clock>
  217. cancel_at(const chrono::time_point<Clock, Duration>& expiry,
  218. cancellation_type_t cancel_type, CompletionToken&& completion_token)
  219. {
  220. return cancel_at_t<decay_t<CompletionToken>, Clock>(
  221. static_cast<CompletionToken&&>(completion_token), expiry, cancel_type);
  222. }
  223. /// Adapt a @ref completion_token to cancel an operation if not complete by the
  224. /// specified absolute time.
  225. /**
  226. * @par Thread Safety
  227. * When an asynchronous operation is used with cancel_at, a timer async_wait
  228. * operation is performed in parallel to the main operation. If this parallel
  229. * async_wait completes first, a cancellation request is emitted to cancel the
  230. * main operation. Consequently, the application must ensure that the
  231. * asynchronous operation is performed within an implicit or explicit strand.
  232. */
  233. template <typename CompletionToken, typename Clock,
  234. typename WaitTraits, typename Executor, typename Duration>
  235. BOOST_ASIO_NODISCARD inline
  236. cancel_at_timer<decay_t<CompletionToken>, Clock, WaitTraits, Executor>
  237. cancel_at(basic_waitable_timer<Clock, WaitTraits, Executor>& timer,
  238. const chrono::time_point<Clock, Duration>& expiry,
  239. CompletionToken&& completion_token)
  240. {
  241. return cancel_at_timer<decay_t<CompletionToken>, Clock, WaitTraits, Executor>(
  242. static_cast<CompletionToken&&>(completion_token),
  243. timer, expiry, cancellation_type::terminal);
  244. }
  245. /// Adapt a @ref completion_token to cancel an operation if not complete by the
  246. /// specified absolute time.
  247. /**
  248. * @par Thread Safety
  249. * When an asynchronous operation is used with cancel_at, a timer async_wait
  250. * operation is performed in parallel to the main operation. If this parallel
  251. * async_wait completes first, a cancellation request is emitted to cancel the
  252. * main operation. Consequently, the application must ensure that the
  253. * asynchronous operation is performed within an implicit or explicit strand.
  254. */
  255. template <typename CompletionToken, typename Clock,
  256. typename WaitTraits, typename Executor, typename Duration>
  257. BOOST_ASIO_NODISCARD inline
  258. cancel_at_timer<decay_t<CompletionToken>, Clock, WaitTraits, Executor>
  259. cancel_at(basic_waitable_timer<Clock, WaitTraits, Executor>& timer,
  260. const chrono::time_point<Clock, Duration>& expiry,
  261. cancellation_type_t cancel_type, CompletionToken&& completion_token)
  262. {
  263. return cancel_at_timer<decay_t<CompletionToken>, Clock, WaitTraits, Executor>(
  264. static_cast<CompletionToken&&>(completion_token),
  265. timer, expiry, cancel_type);
  266. }
  267. } // namespace asio
  268. } // namespace boost
  269. #include <boost/asio/detail/pop_options.hpp>
  270. #include <boost/asio/impl/cancel_at.hpp>
  271. #endif // BOOST_ASIO_CANCEL_AT_HPP