wwa-coro 0.0.1
Yet Another C++20 Coroutine Library
wwa::coro::task< Result > Class Template Reference

A coroutine task. More...

#include <task.h>

+ Collaboration diagram for wwa::coro::task< Result >:

Public Types

using const_reference = std::add_lvalue_reference_t<std::add_const_t<value_type>>
 Constant version of reference.
 
using promise_type = detail::promise_type<Result>
 The promise type associated with the task.
 
using reference
 Reference type; Result&& if Result is an rvalue reference, Result& otherwise.
 
using rvalue_reference = std::add_rvalue_reference_t<std::remove_cv_t<value_type>>
 Rvalue reference type.
 
using value_type = std::remove_reference_t<Result>
 Value type; a Result with all references stripped.
 

Public Member Functions

 task () noexcept=default
 Default constructor.
 
 task (task &&other) noexcept
 Move constructor.
 
 ~task ()
 Destructor.
 
bool destroy ()
 Destroys the task.
 
constexpr bool is_ready () const noexcept
 Checks if the task is ready.
 
auto operator co_await () &&noexcept
 Await the result of the task.
 
auto operator co_await () const &noexcept
 Await the result of the task.
 
taskoperator= (task &&other) noexcept
 Move assignment operator.
 
reference result_value () &
 Returns the result produced by the task.
 
rvalue_reference result_value () &&
 Returns the result produced by the task.
 
const_reference result_value () const &
 Returns the result produced by the task.
 
bool resume () const
 Resumes the task.
 

Detailed Description

template<typename Result = void>
class wwa::coro::task< Result >

A coroutine task.

The task class represents a coroutine task that can be used to perform asynchronous operations. A task is a lightweight object that wraps a coroutine and provides a mechanism for resuming the coroutine and obtaining the result of the operation.

Example:

{
co_return 123;
}
{
co_return 456;
}
{
const auto a = co_await task1();
const auto b = co_await task2();
co_return a + b;
}
{
std::cout << "The result is " << co_await sum() << "\n";
}
Template Parameters
ResultThe type of the result produced by the task.
Examples
advance_with_begin.cpp, async_generator.cpp, eager_task.cpp, run_awaitable.cpp, and task.cpp.

Definition at line 196 of file task.h.

Member Typedef Documentation

◆ const_reference

template<typename Result = void>
using wwa::coro::task< Result >::const_reference = std::add_lvalue_reference_t<std::add_const_t<value_type>>

Constant version of reference.

Definition at line 324 of file task.h.

◆ promise_type

template<typename Result = void>
using wwa::coro::task< Result >::promise_type = detail::promise_type<Result>

The promise type associated with the task.

The promise_type alias is a type alias for the promise type associated with the task.

Definition at line 203 of file task.h.

◆ reference

template<typename Result = void>
using wwa::coro::task< Result >::reference
Initial value:
std::conditional_t<std::is_rvalue_reference_v<Result>, Result, std::add_lvalue_reference_t<value_type>>

Reference type; Result&& if Result is an rvalue reference, Result& otherwise.

Definition at line 321 of file task.h.

◆ rvalue_reference

template<typename Result = void>
using wwa::coro::task< Result >::rvalue_reference = std::add_rvalue_reference_t<std::remove_cv_t<value_type>>

Rvalue reference type.

Definition at line 326 of file task.h.

◆ value_type

template<typename Result = void>
using wwa::coro::task< Result >::value_type = std::remove_reference_t<Result>

Value type; a Result with all references stripped.

Definition at line 319 of file task.h.

Constructor & Destructor Documentation

◆ task() [1/2]

template<typename Result = void>
wwa::coro::task< Result >::task ( )
defaultnoexcept

Default constructor.

◆ task() [2/2]

template<typename Result = void>
wwa::coro::task< Result >::task ( task< Result > && other)
inlinenoexcept

Move constructor.

Definition at line 224 of file task.h.

◆ ~task()

template<typename Result = void>
wwa::coro::task< Result >::~task ( )
inline

Destructor.

Definition at line 250 of file task.h.

Member Function Documentation

◆ destroy()

template<typename Result = void>
bool wwa::coro::task< Result >::destroy ( )
inline

Destroys the task.

Destroys the task by destroying the associated coroutine handle.

Returns
Operation status.
Return values
trueThe task was destroyed.
falseThe task was already destroyed or was default-constructed.

Definition at line 307 of file task.h.

◆ is_ready()

template<typename Result = void>
bool wwa::coro::task< Result >::is_ready ( ) const
inlinenodiscardconstexprnoexcept

Checks if the task is ready.

A task is considered ready if it has finished executing or has an empty coroutine handle associated with (for example, a default-constructed task).

A task that is ready cannot be resumed.

Returns
Task state.
Return values
trueThe task has finished executing; the result is available.
Returns
false The task has not yet finished executing.

Definition at line 269 of file task.h.

◆ operator co_await() [1/2]

template<typename Result = void>
auto wwa::coro::task< Result >::operator co_await ( ) &&
inlinenoexcept

Await the result of the task.

This operator allows the task to be co_await'ed, returning the result produced by the task. It is used when the task is an rvalue, ensuring that the result is moved.

Definition at line 445 of file task.h.

◆ operator co_await() [2/2]

template<typename Result = void>
auto wwa::coro::task< Result >::operator co_await ( ) const &
inlinenoexcept

Await the result of the task.

This operator allows the task to be co_await'ed, returning the result produced by the task. If the task's result type is an rvalue reference, it returns an rvalue reference to the result. Otherwise, it returns an lvalue reference to the result.

Returns
An awaiter that retrieves the result of the task.

Definition at line 405 of file task.h.

◆ operator=()

template<typename Result = void>
task & wwa::coro::task< Result >::operator= ( task< Result > && other)
inlinenoexcept

Move assignment operator.

Definition at line 234 of file task.h.

◆ result_value() [1/3]

template<typename Result = void>
reference wwa::coro::task< Result >::result_value ( ) &
inline

Returns the result produced by the task.

Returns an lvalue or rvalue (if Result template parameter of the task is an rvalue reference) reference to the result produced by the task.

Returns
The result produced by the task.
Exceptions
bad_taskThe task is empty or has been destroyed.
bad_result_accessThe result is not yet available.

Definition at line 343 of file task.h.

◆ result_value() [2/3]

template<typename Result = void>
rvalue_reference wwa::coro::task< Result >::result_value ( ) &&
inline

Returns the result produced by the task.

Returns an rvalue reference to the result produced by the task.

Returns
The result produced by the task.
Exceptions
bad_taskThe task is empty or has been destroyed.
bad_result_accessThe result is not yet available.

Definition at line 386 of file task.h.

◆ result_value() [3/3]

template<typename Result = void>
const_reference wwa::coro::task< Result >::result_value ( ) const &
inline

Returns the result produced by the task.

Returns a constant lvalue reference to the result produced by the task.

Returns
The result produced by the task.
Exceptions
bad_taskThe task is empty or has been destroyed.
bad_result_accessThe result is not yet available.

Definition at line 368 of file task.h.

◆ resume()

template<typename Result = void>
bool wwa::coro::task< Result >::resume ( ) const
inline

Resumes the task.

Resuming a task causes the associated coroutine to be resumed. A task can only be resumed if it is not ready.

Returns
Task state.
Return values
trueThe task has not yet finished executing.
falseThe task has finished executing; the result is available.
See also
is_ready()

Definition at line 286 of file task.h.


The documentation for this class was generated from the following file: