81 template<
typename Promise>
83 std::coroutine_handle<Promise> handle)
noexcept
85 auto& promise = handle.promise();
89 auto continuation = promise.continuation_;
92 promise.completed_.store(
true, std::memory_order_release);
97 return std::noop_coroutine();
130 requires std::convertible_to<U, T>
240 handle_ = std::exchange(other.handle_,
nullptr);
263 [[nodiscard]]
bool valid() const noexcept
271 [[nodiscard]]
explicit operator bool() const noexcept
283 [[nodiscard]]
bool done() const noexcept
285 return handle_ &&
handle_.promise().completed_.load(std::memory_order_acquire);
293 [[nodiscard]]
decltype(
auto)
get() &
295 return handle_.promise().result();
301 [[nodiscard]]
decltype(
auto)
get() &&
303 return std::move(
handle_.promise()).result();
325 std::coroutine_handle<> awaiting)
noexcept
327 handle_.promise().continuation_ = awaiting;
336 return handle_.promise().result();
343 [[nodiscard]]
awaiter operator co_await()
noexcept
367 return task<T>{std::coroutine_handle<detail::promise_type<T>>::from_promise(*
this)};
373 return task<void>{std::coroutine_handle<detail::promise_type<void>>::from_promise(*
this)};
386 co_return std::forward<T>(
value);
409 std::rethrow_exception(ex);
420 std::rethrow_exception(ex);
Forward declaration of task.
task() noexcept
Default constructor - creates empty task.
task & operator=(task &&other) noexcept
Move assignment operator.
decltype(auto) get() &
Get the result (blocking, for testing)
decltype(auto) get() &&
Get the result (rvalue, blocking, for testing)
task(task &&other) noexcept
Move constructor.
task & operator=(const task &)=delete
bool valid() const noexcept
Check if task is valid (has a coroutine)
std::coroutine_handle< promise_type > handle_type
bool done() const noexcept
Check if the coroutine is done.
void resume() const
Resume the coroutine (for manual execution)
task(handle_type handle) noexcept
Construct from coroutine handle.
task(const task &)=delete
~task()
Destructor - destroys the coroutine if owned.
Enhanced type-safe value with perfect legacy compatibility.
task< T > make_exceptional_task(std::exception_ptr ex)
Create a task that throws an exception.
task< void > make_ready_task()
Create a task that completes immediately with no value.
task< void > make_exceptional_task< void >(std::exception_ptr ex)
Specialization for void.
Final awaiter that resumes the continuation.
std::coroutine_handle await_suspend(std::coroutine_handle< Promise > handle) noexcept
void await_resume() noexcept
bool await_ready() const noexcept
Base promise type with common functionality.
std::suspend_never initial_suspend() noexcept
Never suspend at start - start executing immediately.
std::atomic< bool > completed_
std::exception_ptr exception_
void unhandled_exception() noexcept
Handle unhandled exceptions.
final_awaiter final_suspend() noexcept
Suspend at final point to allow result retrieval.
std::coroutine_handle continuation_
void result()
Check for exceptions.
void return_void() noexcept
Handle void return.
Promise type for value-returning tasks.
task< T > get_return_object() noexcept
Get return object (the task)
std::optional< T > result_
void return_value(U &&value)
Store the return value.
T & result() &
Get the stored result, rethrowing any exception.
T && result() &&
Get the stored result (rvalue), rethrowing any exception.
bool await_ready() const noexcept
Check if the coroutine is already done.
decltype(auto) await_resume()
Get the result when resumed.
std::coroutine_handle await_suspend(std::coroutine_handle<> awaiting) noexcept
Suspend and set continuation.