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