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

Thread-safe promise state management with continuation support. More...

#include <State.h>

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

Public Member Functions

 State ()
 Default constructor - initializes empty state.
 
 ~State ()
 Destructor - cleanup state.
 
void setValue (tValue &&value)
 Set the promise value and trigger continuation.
 
void setException (std::exception_ptr exception)
 Set exception state and trigger error handler.
 
template<typename F >
void setContinuation (std::shared_ptr< SLLooper > &looper_, F &&continuation)
 Register continuation callback for successful resolution.
 
template<typename F >
void setErrorHandler (std::shared_ptr< SLLooper > &looper_, F &&errorHandler)
 Register error handler callback for promise rejection.
 

Detailed Description

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

Thread-safe promise state management with continuation support.

Template Parameters
tValueType of value stored in the promise state

State provides the core functionality for promise/future pattern implementation with asynchronous callback execution via SLLooper integration. Key features:

  • Value storage: Optional-based value storage with move semantics
  • Exception handling: std::exception_ptr for type-safe error propagation
  • Continuation support: Callback registration and execution
  • Thread safety: Asynchronous execution via SLLooper message queue
  • Template specialization: Special handling for void-returning promises

The State class acts as the shared state between Promise and Future objects, allowing thread-safe communication of results and errors across thread boundaries.

// Create state for integer promise
auto state = std::make_shared<State<int>>();
// Register continuation
state->setContinuation(looper, [](int value) {
std::cout << "Received: " << value << std::endl;
});
// Register error handler
state->setErrorHandler(looper, [](std::exception_ptr ex) {
try {
std::rethrow_exception(ex);
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
});
// Set value (triggers continuation)
state->setValue(42);
Note
Thread-safe when used with SLLooper for callback execution
Warning
Direct member access is not thread-safe - use provided methods
See also
Promise, SLLooper

Definition at line 69 of file State.h.

Constructor & Destructor Documentation

◆ State()

template<typename tValue >
swt::State< tValue >::State ( )
inline

Default constructor - initializes empty state.

Creates a new State object with no value, no exception, and no registered callbacks.

Definition at line 78 of file State.h.

◆ ~State()

template<typename tValue >
swt::State< tValue >::~State ( )
inline

Destructor - cleanup state.

Cleans up any stored value, exception, and callback functions. Does not automatically execute pending callbacks.

Definition at line 86 of file State.h.

Member Function Documentation

◆ setContinuation()

template<typename tValue >
template<typename F >
void swt::State< tValue >::setContinuation ( std::shared_ptr< SLLooper > &  looper_,
F &&  continuation 
)

Register continuation callback for successful resolution.

Template Parameters
FFunction type for continuation callback
Parameters
looper_SLLooper instance for callback execution
continuationCallback function to execute when value is available

Registers a continuation callback that will be executed when the promise resolves successfully. If the promise is already resolved, the callback is executed immediately.

Note
Implementation in State.tpp
Uses perfect forwarding to preserve function properties
See also
SLLooper

◆ setErrorHandler()

template<typename tValue >
template<typename F >
void swt::State< tValue >::setErrorHandler ( std::shared_ptr< SLLooper > &  looper_,
F &&  errorHandler 
)

Register error handler callback for promise rejection.

Template Parameters
FFunction type for error handler callback
Parameters
looper_SLLooper instance for error handler execution
errorHandlerCallback function to execute when exception occurs

Registers an error handler that will be executed when the promise is rejected with an exception. If an exception is already set, the error handler is executed immediately.

Note
Implementation in State.tpp
Error handler receives std::exception_ptr parameter
See also
SLLooper

◆ setException()

template<typename tValue >
void swt::State< tValue >::setException ( std::exception_ptr  exception)

Set exception state and trigger error handler.

Parameters
exceptionException pointer to store

Sets the promise to failed state and immediately executes any registered error handler if available. This method can only be called once per State instance.

Note
Implementation in State.tpp
Thread-safe execution via SLLooper
See also
setException

◆ setValue()

template<typename tValue >
void swt::State< tValue >::setValue ( tValue &&  value)

Set the promise value and trigger continuation.

Parameters
valueValue to store (moved into internal storage)

Sets the promise value using move semantics and immediately executes any registered continuation callback if available. This method can only be called once per State instance.

Note
Implementation in State.tpp
Thread-safe execution via SLLooper
See also
setValue

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