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::Promise< tValue > Class Template Reference

Type-safe promise for asynchronous result handling with continuation chaining. More...

#include <Promise.h>

Collaboration diagram for swt::Promise< tValue >:
Collaboration graph

Public Member Functions

 Promise ()
 Default constructor - creates promise with shared state.
 
void set_value (tValue value)
 Resolve promise with a value.
 
void set_exception (std::exception_ptr exception)
 Reject promise with an exception.
 
template<typename F >
auto then (std::shared_ptr< SLLooper > &looper_, F func) -> Promise< std::invoke_result_t< F, tValue > >
 Chain continuation callback for promise resolution.
 
template<typename F >
auto catchError (std::shared_ptr< SLLooper > &looper_, F func) -> Promise< tValue >
 Chain error handler for exception recovery.
 
void operator() (tValue value)
 Function call operator for convenient promise resolution.
 

Detailed Description

template<typename tValue>
class swt::Promise< tValue >

Type-safe promise for asynchronous result handling with continuation chaining.

Template Parameters
tValueType of value that the promise will resolve to

Promise provides a modern C++ implementation of the promise/future pattern with:

  • Type-safe continuation chaining: Automatic type deduction for chained operations
  • Exception propagation: Automatic error handling through the promise chain
  • Asynchronous execution: Integration with SLLooper for thread-safe callback execution
  • Move semantics: Efficient value transfer without unnecessary copies
  • Template specialization: Special handling for void-returning promises

Key features:

  • Continuation chaining: then() method for sequential async operations
  • Error handling: catchError() method for exception recovery
  • Thread safety: Callbacks execute in specified SLLooper thread context
  • Type transformation: Support for changing value types through the chain

The Promise class works in conjunction with State objects to manage the asynchronous state and provide thread-safe communication between producers and consumers of asynchronous results.

// Create and resolve promise
Promise<int> promise;
promise.set_value(42);
// Chain operations with type transformation
promise.then(looper, [](int x) {
return std::to_string(x * 2); // int -> string
}).then(looper, [](const std::string& s) {
std::cout << "Result: " << s << std::endl;
}).catchError(looper, [](std::exception_ptr ex) {
std::cerr << "Error in promise chain!" << std::endl;
});
Type-safe promise for asynchronous result handling with continuation chaining.
Definition Promise.h:68
auto catchError(std::shared_ptr< SLLooper > &looper_, F func) -> Promise< tValue >
Chain error handler for exception recovery.
auto then(std::shared_ptr< SLLooper > &looper_, F func) -> Promise< std::invoke_result_t< F, tValue > >
Chain continuation callback for promise resolution.
void set_value(tValue value)
Resolve promise with a value.
Note
Thread-safe when used with SLLooper for callback execution
Warning
Promise can only be resolved once with either value or exception
See also
swt::State "State", SLLooper, Promise

Definition at line 67 of file Promise.h.

Constructor & Destructor Documentation

◆ Promise()

template<typename tValue >
swt::Promise< tValue >::Promise ( )

Default constructor - creates promise with shared state.

Initializes a new promise with a shared State object for communication with continuation handlers and error handlers.

Note
Implementation in Promise.tpp

Member Function Documentation

◆ catchError()

template<typename tValue >
template<typename F >
auto swt::Promise< tValue >::catchError ( std::shared_ptr< SLLooper > &  looper_,
func 
) -> Promise< tValue >

Chain error handler for exception recovery.

Template Parameters
FFunction type for error handler callback (auto-deduced)
Parameters
looper_SLLooper instance for callback execution thread context
funcError handler function that receives std::exception_ptr
Returns
Promise<tValue> New promise continuing the chain

Registers an error handler that executes when the promise is rejected. The error handler can recover from errors by returning a value, or let errors propagate by throwing or returning nothing.

Error recovery patterns:

  • Recovery: Return a valid tValue to continue with success
  • Propagation: Throw or re-throw to propagate error down chain
  • Logging: Log error and return default value for graceful degradation
Note
Implementation in Promise.tpp
Thread-safe registration and execution
promise.catchError(looper, [](std::exception_ptr ex) -> int {
try {
std::rethrow_exception(ex);
} catch (const NetworkException& e) {
return -1; // Recovery value for network errors
} catch (const TimeoutException& e) {
return -2; // Recovery value for timeout errors
}
// Other exceptions will be re-thrown
});
See also
Promise, SLLooper

◆ operator()()

template<typename tValue >
void swt::Promise< tValue >::operator() ( tValue  value)
inline

Function call operator for convenient promise resolution.

Parameters
valueValue to resolve the promise with

Convenience operator that allows using the promise as a callable object for resolution. Equivalent to calling set_value(value).

Promise<int> promise;
promise(42); // Same as promise.set_value(42)

Definition at line 203 of file Promise.h.

References swt::Promise< tValue >::set_value().

Here is the call graph for this function:

◆ set_exception()

template<typename tValue >
void swt::Promise< tValue >::set_exception ( std::exception_ptr  exception)

Reject promise with an exception.

Parameters
exceptionException pointer to reject the promise with

Sets the promise to failed state and triggers any registered error handlers. This method can only be called once per promise.

Note
Implementation in Promise.tpp
Thread-safe operation
Promise<int> promise;
try {
// Some operation that may fail
} catch (...) {
promise.set_exception(std::current_exception());
}
void set_exception(std::exception_ptr exception)
Reject promise with an exception.

◆ set_value()

template<typename tValue >
void swt::Promise< tValue >::set_value ( tValue  value)

Resolve promise with a value.

Parameters
valueValue to resolve the promise with (moved)

Sets the promise value and triggers any registered continuation callbacks. This method can only be called once per promise instance. Subsequent calls are ignored.

Note
Implementation in Promise.tpp
Thread-safe operation
Promise<int> promise;
promise.set_value(42); // Resolves and triggers continuations

Referenced by swt::Promise< void >::operator()(), and swt::Promise< tValue >::operator()().

Here is the caller graph for this function:

◆ then()

template<typename tValue >
template<typename F >
auto swt::Promise< tValue >::then ( std::shared_ptr< SLLooper > &  looper_,
func 
) -> Promise< std::invoke_result_t< F, tValue > >

Chain continuation callback for promise resolution.

Template Parameters
FFunction type for continuation callback (auto-deduced)
Parameters
looper_SLLooper instance for callback execution thread context
funcContinuation function to execute when promise resolves
Returns
Promise<ReturnType> New promise for further chaining

Registers a continuation callback that executes when the promise resolves successfully. The continuation function receives the resolved value and can return a new value of any type, enabling type transformation chains.

Key capabilities:

  • Type transformation: Return type can differ from input type
  • Exception safety: Automatic exception propagation through chain
  • Asynchronous execution: Callbacks run in SLLooper thread context
  • Chaining: Returns new promise for further then()/catchError() calls
Note
Implementation in Promise.tpp
Thread-safe registration and execution
promise.then(looper, [](int x) {
return x * 2;
}).then(looper, [](int doubled) -> std::string {
return "Result: " + std::to_string(doubled);
}).then(looper, [](const std::string& msg) {
std::cout << msg << std::endl;
});
See also
Promise, SLLooper

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