| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // | ||
| 2 | // Copyright (c) 2025 Vinnie Falco (vinnie dot falco at gmail dot com) | ||
| 3 | // | ||
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
| 6 | // | ||
| 7 | // Official repository: https://github.com/cppalliance/capy | ||
| 8 | // | ||
| 9 | |||
| 10 | #ifndef BOOST_CAPY_EX_GET_STOP_TOKEN_HPP | ||
| 11 | #define BOOST_CAPY_EX_GET_STOP_TOKEN_HPP | ||
| 12 | |||
| 13 | #include <boost/capy/detail/config.hpp> | ||
| 14 | |||
| 15 | namespace boost { | ||
| 16 | namespace capy { | ||
| 17 | |||
| 18 | #if BOOST_CAPY_HAS_STOP_TOKEN | ||
| 19 | |||
| 20 | /** Tag type for coroutine stop token retrieval. | ||
| 21 | |||
| 22 | This tag is returned by @ref get_stop_token and intercepted by a | ||
| 23 | promise type's `await_transform` to yield the coroutine's current | ||
| 24 | stop token. The tag itself carries no data; it serves only as a | ||
| 25 | sentinel for compile-time dispatch. | ||
| 26 | |||
| 27 | @see get_stop_token | ||
| 28 | @see stop_token_support | ||
| 29 | */ | ||
| 30 | struct get_stop_token_tag {}; | ||
| 31 | |||
| 32 | /** Return a tag that yields the current stop token when awaited. | ||
| 33 | |||
| 34 | Use `co_await get_stop_token()` inside a coroutine whose promise | ||
| 35 | type supports stop token access (e.g., inherits from | ||
| 36 | @ref stop_token_support). The returned stop token reflects whatever | ||
| 37 | token was passed to this coroutine when it was awaited. | ||
| 38 | |||
| 39 | @par Example | ||
| 40 | @code | ||
| 41 | task<void> cancellable_work() | ||
| 42 | { | ||
| 43 | auto token = co_await get_stop_token(); | ||
| 44 | for (int i = 0; i < 1000; ++i) | ||
| 45 | { | ||
| 46 | if (token.stop_requested()) | ||
| 47 | co_return; // Exit gracefully on cancellation | ||
| 48 | co_await process_chunk(i); | ||
| 49 | } | ||
| 50 | } | ||
| 51 | @endcode | ||
| 52 | |||
| 53 | @par Behavior | ||
| 54 | @li If no stop token was propagated, returns a default-constructed | ||
| 55 | `std::stop_token` (where `stop_possible()` returns `false`). | ||
| 56 | @li The returned token remains valid for the coroutine's lifetime. | ||
| 57 | @li This operation never suspends; `await_ready()` always returns `true`. | ||
| 58 | |||
| 59 | @return A tag that `await_transform` intercepts to return the stop token. | ||
| 60 | |||
| 61 | @see get_stop_token_tag | ||
| 62 | @see stop_token_support | ||
| 63 | */ | ||
| 64 | 15 | inline get_stop_token_tag get_stop_token() noexcept | |
| 65 | { | ||
| 66 | 15 | return {}; | |
| 67 | } | ||
| 68 | |||
| 69 | #endif // BOOST_CAPY_HAS_STOP_TOKEN | ||
| 70 | |||
| 71 | } // namespace capy | ||
| 72 | } // namespace boost | ||
| 73 | |||
| 74 | #endif | ||
| 75 |