this_coro.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. //
  2. // this_coro.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_THIS_CORO_HPP
  11. #define ASIO_THIS_CORO_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/detail/type_traits.hpp"
  17. #include "asio/detail/push_options.hpp"
  18. namespace asio {
  19. namespace this_coro {
  20. /// Awaitable type that returns the executor of the current coroutine.
  21. struct executor_t
  22. {
  23. constexpr executor_t()
  24. {
  25. }
  26. };
  27. /// Awaitable object that returns the executor of the current coroutine.
  28. constexpr executor_t executor;
  29. /// Awaitable type that returns the cancellation state of the current coroutine.
  30. struct cancellation_state_t
  31. {
  32. constexpr cancellation_state_t()
  33. {
  34. }
  35. };
  36. /// Awaitable object that returns the cancellation state of the current
  37. /// coroutine.
  38. /**
  39. * @par Example
  40. * @code asio::awaitable<void> my_coroutine()
  41. * {
  42. * asio::cancellation_state cs
  43. * = co_await asio::this_coro::cancellation_state;
  44. *
  45. * // ...
  46. *
  47. * if (cs.cancelled() != asio::cancellation_type::none)
  48. * // ...
  49. * } @endcode
  50. */
  51. constexpr cancellation_state_t cancellation_state;
  52. #if defined(GENERATING_DOCUMENTATION)
  53. /// Returns an awaitable object that may be used to reset the cancellation state
  54. /// of the current coroutine.
  55. /**
  56. * Let <tt>P</tt> be the cancellation slot associated with the current
  57. * coroutine's @ref co_spawn completion handler. Assigns a new
  58. * asio::cancellation_state object <tt>S</tt>, constructed as
  59. * <tt>S(P)</tt>, into the current coroutine's cancellation state object.
  60. *
  61. * @par Example
  62. * @code asio::awaitable<void> my_coroutine()
  63. * {
  64. * co_await asio::this_coro::reset_cancellation_state();
  65. *
  66. * // ...
  67. * } @endcode
  68. *
  69. * @note The cancellation state is shared by all coroutines in the same "thread
  70. * of execution" that was created using asio::co_spawn.
  71. */
  72. ASIO_NODISCARD constexpr unspecified
  73. reset_cancellation_state();
  74. /// Returns an awaitable object that may be used to reset the cancellation state
  75. /// of the current coroutine.
  76. /**
  77. * Let <tt>P</tt> be the cancellation slot associated with the current
  78. * coroutine's @ref co_spawn completion handler. Assigns a new
  79. * asio::cancellation_state object <tt>S</tt>, constructed as <tt>S(P,
  80. * std::forward<Filter>(filter))</tt>, into the current coroutine's
  81. * cancellation state object.
  82. *
  83. * @par Example
  84. * @code asio::awaitable<void> my_coroutine()
  85. * {
  86. * co_await asio::this_coro::reset_cancellation_state(
  87. * asio::enable_partial_cancellation());
  88. *
  89. * // ...
  90. * } @endcode
  91. *
  92. * @note The cancellation state is shared by all coroutines in the same "thread
  93. * of execution" that was created using asio::co_spawn.
  94. */
  95. template <typename Filter>
  96. ASIO_NODISCARD constexpr unspecified
  97. reset_cancellation_state(Filter&& filter);
  98. /// Returns an awaitable object that may be used to reset the cancellation state
  99. /// of the current coroutine.
  100. /**
  101. * Let <tt>P</tt> be the cancellation slot associated with the current
  102. * coroutine's @ref co_spawn completion handler. Assigns a new
  103. * asio::cancellation_state object <tt>S</tt>, constructed as <tt>S(P,
  104. * std::forward<InFilter>(in_filter),
  105. * std::forward<OutFilter>(out_filter))</tt>, into the current coroutine's
  106. * cancellation state object.
  107. *
  108. * @par Example
  109. * @code asio::awaitable<void> my_coroutine()
  110. * {
  111. * co_await asio::this_coro::reset_cancellation_state(
  112. * asio::enable_partial_cancellation(),
  113. * asio::disable_cancellation());
  114. *
  115. * // ...
  116. * } @endcode
  117. *
  118. * @note The cancellation state is shared by all coroutines in the same "thread
  119. * of execution" that was created using asio::co_spawn.
  120. */
  121. template <typename InFilter, typename OutFilter>
  122. ASIO_NODISCARD constexpr unspecified
  123. reset_cancellation_state(
  124. InFilter&& in_filter,
  125. OutFilter&& out_filter);
  126. /// Returns an awaitable object that may be used to determine whether the
  127. /// coroutine throws if trying to suspend when it has been cancelled.
  128. /**
  129. * @par Example
  130. * @code asio::awaitable<void> my_coroutine()
  131. * {
  132. * if (co_await asio::this_coro::throw_if_cancelled)
  133. * // ...
  134. *
  135. * // ...
  136. * } @endcode
  137. */
  138. ASIO_NODISCARD constexpr unspecified
  139. throw_if_cancelled();
  140. /// Returns an awaitable object that may be used to specify whether the
  141. /// coroutine throws if trying to suspend when it has been cancelled.
  142. /**
  143. * @par Example
  144. * @code asio::awaitable<void> my_coroutine()
  145. * {
  146. * co_await asio::this_coro::throw_if_cancelled(false);
  147. *
  148. * // ...
  149. * } @endcode
  150. */
  151. ASIO_NODISCARD constexpr unspecified
  152. throw_if_cancelled(bool value);
  153. #else // defined(GENERATING_DOCUMENTATION)
  154. struct reset_cancellation_state_0_t
  155. {
  156. constexpr reset_cancellation_state_0_t()
  157. {
  158. }
  159. };
  160. ASIO_NODISCARD inline constexpr reset_cancellation_state_0_t
  161. reset_cancellation_state()
  162. {
  163. return reset_cancellation_state_0_t();
  164. }
  165. template <typename Filter>
  166. struct reset_cancellation_state_1_t
  167. {
  168. template <typename F>
  169. explicit constexpr reset_cancellation_state_1_t(
  170. F&& filt)
  171. : filter(static_cast<F&&>(filt))
  172. {
  173. }
  174. Filter filter;
  175. };
  176. template <typename Filter>
  177. ASIO_NODISCARD inline constexpr reset_cancellation_state_1_t<
  178. decay_t<Filter>>
  179. reset_cancellation_state(Filter&& filter)
  180. {
  181. return reset_cancellation_state_1_t<decay_t<Filter>>(
  182. static_cast<Filter&&>(filter));
  183. }
  184. template <typename InFilter, typename OutFilter>
  185. struct reset_cancellation_state_2_t
  186. {
  187. template <typename F1, typename F2>
  188. constexpr reset_cancellation_state_2_t(
  189. F1&& in_filt, F2&& out_filt)
  190. : in_filter(static_cast<F1&&>(in_filt)),
  191. out_filter(static_cast<F2&&>(out_filt))
  192. {
  193. }
  194. InFilter in_filter;
  195. OutFilter out_filter;
  196. };
  197. template <typename InFilter, typename OutFilter>
  198. ASIO_NODISCARD inline constexpr
  199. reset_cancellation_state_2_t<decay_t<InFilter>, decay_t<OutFilter>>
  200. reset_cancellation_state(InFilter&& in_filter, OutFilter&& out_filter)
  201. {
  202. return reset_cancellation_state_2_t<decay_t<InFilter>, decay_t<OutFilter>>(
  203. static_cast<InFilter&&>(in_filter),
  204. static_cast<OutFilter&&>(out_filter));
  205. }
  206. struct throw_if_cancelled_0_t
  207. {
  208. constexpr throw_if_cancelled_0_t()
  209. {
  210. }
  211. };
  212. ASIO_NODISCARD inline constexpr throw_if_cancelled_0_t
  213. throw_if_cancelled()
  214. {
  215. return throw_if_cancelled_0_t();
  216. }
  217. struct throw_if_cancelled_1_t
  218. {
  219. explicit constexpr throw_if_cancelled_1_t(bool val)
  220. : value(val)
  221. {
  222. }
  223. bool value;
  224. };
  225. ASIO_NODISCARD inline constexpr throw_if_cancelled_1_t
  226. throw_if_cancelled(bool value)
  227. {
  228. return throw_if_cancelled_1_t(value);
  229. }
  230. #endif // defined(GENERATING_DOCUMENTATION)
  231. } // namespace this_coro
  232. } // namespace asio
  233. #include "asio/detail/pop_options.hpp"
  234. #endif // ASIO_THIS_CORO_HPP