this_coro.hpp 7.1 KB

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