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::TaskPromise< T > Class Template Reference

Promise type for coroutine-based Task implementation. More...

#include <Task.h>

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

Public Types

using Handle = std::coroutine_handle< TaskPromise< T > >
 

Public Member Functions

Task< T > get_return_object ()
 Create the Task object for this promise.
 
std::suspend_always initial_suspend () noexcept
 Initial suspend behavior - always suspend to allow manual start.
 
auto final_suspend () noexcept
 Final suspend behavior with continuation chaining.
 
void return_value (T value)
 Store the coroutine return value.
 
void unhandled_exception ()
 Handle unhandled exceptions in the coroutine.
 
getResult ()
 Get the stored result value.
 
bool isReady () const noexcept
 Check if the task has completed.
 
void addContinuation (std::coroutine_handle<> continuation)
 Add a continuation to be resumed when task completes.
 

Detailed Description

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

Promise type for coroutine-based Task implementation.

TaskPromise manages the coroutine state and provides continuation chaining for proper asynchronous execution. It handles result storage, exception propagation, and thread-safe continuation management.

Template Parameters
TThe return type of the coroutine

Key features:

  • Thread-safe completion: Atomic flags and proper memory ordering
  • Continuation chaining: Multiple coroutines can await the same task
  • Exception safety: Proper exception propagation and resource cleanup
  • Memory efficient: Minimal overhead with optimized storage
Note
This class is not intended for direct use - it's managed by Task<T>

Definition at line 45 of file Task.h.

Member Typedef Documentation

◆ Handle

template<typename T >
using swt::TaskPromise< T >::Handle = std::coroutine_handle<TaskPromise<T> >

Definition at line 47 of file Task.h.

Member Function Documentation

◆ addContinuation()

template<typename T >
void swt::TaskPromise< T >::addContinuation ( std::coroutine_handle<>  continuation)
inline

Add a continuation to be resumed when task completes.

Parameters
continuationCoroutine handle to resume

If the task is already completed, the continuation is resumed immediately. Otherwise, it's added to the continuation list for later resumption.

Definition at line 144 of file Task.h.

◆ final_suspend()

template<typename T >
auto swt::TaskPromise< T >::final_suspend ( )
inlinenoexcept

Final suspend behavior with continuation chaining.

Returns
Custom awaiter that resumes all waiting continuations

Resume all waiting continuations when task completes

Parameters
hHandle to the completing coroutine
Returns
noop_coroutine to prevent current coroutine resumption

Definition at line 67 of file Task.h.

◆ get_return_object()

template<typename T >
Task< T > swt::TaskPromise< T >::get_return_object ( )
inline

Create the Task object for this promise.

Returns
Task<T> object associated with this promise

Definition at line 467 of file Task.h.

◆ getResult()

template<typename T >
T swt::TaskPromise< T >::getResult ( )
inline

Get the stored result value.

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

Definition at line 119 of file Task.h.

◆ initial_suspend()

template<typename T >
std::suspend_always swt::TaskPromise< T >::initial_suspend ( )
inlinenoexcept

Initial suspend behavior - always suspend to allow manual start.

Returns
std::suspend_always to require manual task.start() call

Definition at line 59 of file Task.h.

◆ isReady()

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

Check if the task has completed.

Returns
true if task completed (successfully or with exception)

Definition at line 133 of file Task.h.

◆ return_value()

template<typename T >
void swt::TaskPromise< T >::return_value ( value)
inline

Store the coroutine return value.

Parameters
valueThe value returned by co_return

Definition at line 100 of file Task.h.

◆ unhandled_exception()

template<typename T >
void swt::TaskPromise< T >::unhandled_exception ( )
inline

Handle unhandled exceptions in the coroutine.

Definition at line 108 of file Task.h.


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