SW Task Event Loop Framework v1.0.0
High-performance C++ asynchronous event loop framework with timer management and promise-based programming
Loading...
Searching...
No Matches
swt::Task< T > Class Template Reference

RAII wrapper for coroutines with proper continuation chaining. More...

#include <Task.h>

Collaboration diagram for swt::Task< T >:
Collaboration graph

Public Types

using promise_type = TaskPromise< T >
 
using Handle = typename promise_type::Handle
 

Public Member Functions

 Task (Handle handle)
 Construct task with coroutine handle.
 
 ~Task ()
 Destructor - automatically destroys coroutine handle.
 
 Task (const Task &)=delete
 
Taskoperator= (const Task &)=delete
 
 Task (Task &&other) noexcept
 Move constructor.
 
Taskoperator= (Task &&other) noexcept
 Move assignment operator.
 
void start ()
 Start the coroutine execution.
 
bool done () const noexcept
 Check if the task has completed.
 
auto getResult () -> T
 Get the result value (non-blocking)
 
void getResult ()
 Get the result for void tasks (non-blocking)
 
bool await_ready () const noexcept
 Check if task is ready (awaitable interface)
 
void await_suspend (std::coroutine_handle<> waiter) noexcept
 Suspend awaiting coroutine until this task completes.
 
auto await_resume () -> T
 Get result when co_await completes (non-void version)
 
void await_resume ()
 Complete co_await for void tasks.
 
bool isReady () const noexcept
 Check if task is ready without blocking.
 

Detailed Description

template<typename T>
class swt::Task< T >

RAII wrapper for coroutines with proper continuation chaining.

Task<T> provides a type-safe, move-only wrapper around C++20 coroutines with support for proper asynchronous execution and continuation chaining.

Template Parameters
TThe return type of the coroutine

Key features:

  • RAII management: Automatic coroutine handle cleanup
  • Move-only semantics: Prevents accidental copying
  • Awaitable interface: Can be co_awaited by other coroutines
  • Thread-safe: Safe to await from multiple threads
  • Exception propagation: Proper exception handling across await boundaries
Task<int> computeAsync() {
co_await std::suspend_always{};
co_return 42;
}
Task<void> example() {
int result = co_await computeAsync();
std::cout << "Result: " << result << std::endl;
}
RAII wrapper for coroutines with proper continuation chaining.
Definition Task.h:303
Note
Tasks must be started manually with start() and are lazy by default
Warning
Task objects must not be copied - move-only for resource safety

Definition at line 303 of file Task.h.

Member Typedef Documentation

◆ Handle

template<typename T >
using swt::Task< T >::Handle = typename promise_type::Handle

Definition at line 306 of file Task.h.

◆ promise_type

template<typename T >
using swt::Task< T >::promise_type = TaskPromise<T>

Definition at line 305 of file Task.h.

Constructor & Destructor Documentation

◆ Task() [1/3]

template<typename T >
swt::Task< T >::Task ( Handle  handle)
inlineexplicit

Construct task with coroutine handle.

Parameters
handleCoroutine handle from promise.get_return_object()

Definition at line 312 of file Task.h.

◆ ~Task()

template<typename T >
swt::Task< T >::~Task ( )
inline

Destructor - automatically destroys coroutine handle.

Definition at line 317 of file Task.h.

◆ Task() [2/3]

template<typename T >
swt::Task< T >::Task ( const Task< T > &  )
delete

◆ Task() [3/3]

template<typename T >
swt::Task< T >::Task ( Task< T > &&  other)
inlinenoexcept

Move constructor.

Parameters
otherTask to move from

Definition at line 331 of file Task.h.

Member Function Documentation

◆ await_ready()

template<typename T >
bool swt::Task< T >::await_ready ( ) const
inlinenoexcept

Check if task is ready (awaitable interface)

Returns
true if task completed, false if still running

Definition at line 409 of file Task.h.

References swt::Task< T >::done().

Here is the call graph for this function:

◆ await_resume() [1/2]

template<typename T >
void swt::Task< T >::await_resume ( )
inline

Complete co_await for void tasks.

Exceptions
std::exception_ptrif coroutine threw an exception

Definition at line 449 of file Task.h.

References swt::Task< T >::getResult().

Here is the call graph for this function:

◆ await_resume() [2/2]

template<typename T >
auto swt::Task< T >::await_resume ( ) -> T
inline

Get result when co_await completes (non-void version)

Returns
The result value
Exceptions
std::exception_ptrif coroutine threw an exception

Definition at line 441 of file Task.h.

References swt::Task< T >::getResult().

Here is the call graph for this function:

◆ await_suspend()

template<typename T >
void swt::Task< T >::await_suspend ( std::coroutine_handle<>  waiter)
inlinenoexcept

Suspend awaiting coroutine until this task completes.

Parameters
waiterHandle to the coroutine that's awaiting this task

This method implements proper continuation chaining. The waiting coroutine will be resumed when this task completes.

Definition at line 420 of file Task.h.

◆ done()

template<typename T >
bool swt::Task< T >::done ( ) const
inlinenoexcept

Check if the task has completed.

Returns
true if coroutine has finished execution

Definition at line 364 of file Task.h.

Referenced by swt::Task< T >::await_ready().

Here is the caller graph for this function:

◆ getResult() [1/2]

template<typename T >
void swt::Task< T >::getResult ( )
inline

Get the result for void tasks (non-blocking)

Exceptions
std::runtime_errorif task not completed or invalid handle
std::exception_ptrif coroutine threw an exception

Definition at line 393 of file Task.h.

◆ getResult() [2/2]

template<typename T >
auto swt::Task< T >::getResult ( ) -> T
inline

Get the result value (non-blocking)

Returns
The result value
Exceptions
std::runtime_errorif task not completed or invalid handle
std::exception_ptrif coroutine threw an exception

This method does not block. Use co_await or check done() first.

Definition at line 376 of file Task.h.

Referenced by swt::Task< T >::await_resume(), and swt::Task< T >::await_resume().

Here is the caller graph for this function:

◆ isReady()

template<typename T >
bool swt::Task< T >::isReady ( ) const
inlinenoexcept

Check if task is ready without blocking.

Returns
true if task completed, false if still running

Definition at line 457 of file Task.h.

◆ operator=() [1/2]

template<typename T >
Task & swt::Task< T >::operator= ( const Task< T > &  )
delete

◆ operator=() [2/2]

template<typename T >
Task & swt::Task< T >::operator= ( Task< T > &&  other)
inlinenoexcept

Move assignment operator.

Parameters
otherTask to move from
Returns
Reference to this task

Definition at line 338 of file Task.h.

◆ start()

template<typename T >
void swt::Task< T >::start ( )
inline

Start the coroutine execution.

Tasks are lazy by default and must be explicitly started. This resumes the coroutine from its initial suspend point.

Definition at line 354 of file Task.h.


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