this_coro.hpp 7.4 KB

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