SW Task Event Loop Framework v1.0.0
High-performance C++ asynchronous event loop framework with timer management and promise-based programming
|
Type-safe promise for asynchronous result handling with continuation chaining. More...
#include <Promise.h>
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. | |
Type-safe promise for asynchronous result handling with continuation chaining.
tValue | Type of value that the promise will resolve to |
Promise provides a modern C++ implementation of the promise/future pattern with:
Key features:
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.
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.
auto swt::Promise< tValue >::catchError | ( | std::shared_ptr< SLLooper > & | looper_, |
F | func | ||
) | -> Promise< tValue > |
Chain error handler for exception recovery.
F | Function type for error handler callback (auto-deduced) |
looper_ | SLLooper instance for callback execution thread context |
func | Error handler function that receives std::exception_ptr |
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:
|
inline |
Function call operator for convenient promise resolution.
value | Value to resolve the promise with |
Convenience operator that allows using the promise as a callable object for resolution. Equivalent to calling set_value(value).
Definition at line 203 of file Promise.h.
References swt::Promise< tValue >::set_value().
void swt::Promise< tValue >::set_exception | ( | std::exception_ptr | exception | ) |
Reject promise with an exception.
exception | Exception 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.
void swt::Promise< tValue >::set_value | ( | tValue | value | ) |
Resolve promise with a value.
value | Value 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.
Referenced by swt::Promise< void >::operator()(), and swt::Promise< tValue >::operator()().
auto swt::Promise< tValue >::then | ( | std::shared_ptr< SLLooper > & | looper_, |
F | func | ||
) | -> Promise< std::invoke_result_t< F, tValue > > |
Chain continuation callback for promise resolution.
F | Function type for continuation callback (auto-deduced) |
looper_ | SLLooper instance for callback execution thread context |
func | Continuation function to execute when promise resolves |
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: