aui::impl::future::CoPromiseTypeBase#
Promise type base for C++20 coroutines returning AFuture
| Header: | #include <AUI/Thread/AFutureCpp20Coro.h> |
| CMake: | aui_link(my_target PUBLIC aui::core) |
Detailed Description#
CoPromiseTypeBase serves as the coroutine promise object for functions that return
AFuture
The promise object is constructed when a coroutine function is called and manages the coroutine's lifecycle, result delivery, exception propagation, and co_await integration.
Public fields and Signals#
future#
AFuture<T> future
The future object that will be returned to the caller of the coroutine.
This future is fulfilled when the coroutine completes (via return_value or return_void) or when an unhandled exception occurs.
Public Methods#
await_transform#
template<Cpp20Awaiter NestedAwaiter >
auto CoPromiseTypeBase::await_transform(NestedAwaiter&& nestedAwaiter)
Overload of await_transform for types that are already Cpp20Awaiter.
- Arguments
nestedAwaiterThe awaiter to wrap.- Returns
- An Awaiter that delegates to the nested awaiter while adding cancellation detection via parentFuture.
Wraps a nested Awaiter with cancellation support. Before resuming, checks if the parent future has been destroyed (via parentFuture weak pointer). If so, throws AThread::Interrupted to abort the coroutine gracefully instead of resuming into a destroyed frame.
template<Cpp20HasPromiseType NestedAwaitable >
auto CoPromiseTypeBase::await_transform(NestedAwaitable nestedAwaitable)
Overload of await_transform for types that have a promise_type (like AFuture
- Arguments
nestedAwaitableThe awaitable to transform.- Returns
- An Awaiter (via the other overload) after converting via operator co_await.
Converts the awaitable into an Awaiter via the free operator co_await function, then delegates to the Cpp20Awaiter overload to wrap it with cancellation support.
final_suspend#
Called by the compiler when the coroutine reaches its final suspension point.
- Returns
- std::suspend_never, meaning the coroutine frame is destroyed immediately after completion.
get_return_object#
AFuture<T> CoPromiseTypeBase::get_return_object()
Called by the compiler to obtain the return value for the caller of the coroutine.
- Returns
- AFuture
that the caller receives. The future is moved out, leaving the internal future member in a moved-from state.
initial_suspend#
Called immediately by the compiler after the coroutine starts.
- Returns
- std::suspend_never, meaning the coroutine starts executing right away (not lazily).
return_#
Handles both return_void and return_value.
This is a unified implementation used by both derived promise types. It locks the
weak pointer to the future and, if the future is still alive, calls AFuture::supplyValue() to propagate the
value to whoever is awaiting the future.
- Arguments
vThe value(s) to supply to the future.
unhandled_exception#
Handles unhandled exceptions thrown within the coroutine body.
Attempts to lock the weak pointer to the future's inner state. If the future is still alive, calls
AFuture::supplyException() to propagate the exception to whoever is awaiting the future. If the future has
already been destroyed, the exception is silently discarded.