3 * @brief Template implementation for State class - Promise/Future state management
18 * @brief Set the promise value and execute continuation if available
19 * @tparam tValue Value type being stored in the promise
20 * @param value Value to store (moved into internal storage)
22 * Sets the promise value using move semantics and immediately executes
23 * any registered continuation callback if both continuation and looper
24 * are available. This provides immediate callback execution for already-
27 * @note Uses perfect forwarding to avoid unnecessary copies
28 * @note Thread-safe when used with SLLooper's message queue
31 * auto state = std::make_shared<State<int>>();
32 * state->setValue(42); // Moves value and executes continuation
35 * @see \ref swt::State::setValue "setValue"
37 template <typename tValue>
38 void State<tValue>::setValue(tValue &&value) {
39 m_value = std::move(value);
40 if (m_continuation && m_looper) {
41 executeContination(std::move(*m_value));
43 // std::cout << "m_continuation or m_looper is empty\n";
48 * @brief Set exception state and execute error handler if available
49 * @tparam tValue Value type being stored in the promise
50 * @param exception Exception pointer to store
52 * Sets the promise to failed state with the provided exception and
53 * immediately executes any registered error handler if both error
54 * handler and looper are available.
56 * @note Exception is stored as std::exception_ptr for type erasure
57 * @note Thread-safe execution via SLLooper message queue
60 * auto state = std::make_shared<State<int>>();
62 * // Some operation that may throw
64 * state->setException(std::current_exception());
68 * @see \ref swt::State::setException "setException"
70 template <typename tValue>
71 void State<tValue>::setException(std::exception_ptr exception) {
72 m_exception = exception;
73 if (m_errorHandler && m_errorLooper) {
74 executeErrorHandler(m_exception);
76 std::cout << "m_errorHandler or m_errorLooper is empty\n";
81 * @brief Register continuation callback for promise resolution
82 * @tparam tValue Value type being stored in the promise
83 * @tparam F Function type for continuation callback
84 * @param looper_ SLLooper instance for callback execution
85 * @param continuation Callback function to execute when value is available
87 * Registers a continuation callback that will be executed when the promise
88 * resolves successfully. If the promise is already resolved, the callback
89 * is executed immediately. If an exception is already set, it's propagated
90 * to the error handler.
92 * @note Uses perfect forwarding to preserve function object properties
93 * @note Handles race conditions by checking existing state
96 * state->setContinuation(looper, [](int result) {
97 * std::cout << "Promise resolved with: " << result << std::endl;
101 * @see \ref swt::State::setContinuation "setContinuation", \ref swt::SLLooper "SLLooper"
103 template <typename tValue>
105 void State<tValue>::setContinuation(std::shared_ptr<SLLooper>& looper_, F&& continuation) {
107 m_continuation = std::forward<F>(continuation);
109 // If value is already set, execute immediately
110 if (m_value.has_value()) {
111 executeContination(*m_value);
112 } else if (m_exception) {
113 // If exception is already set, propagate to error handler
114 if (m_errorHandler && m_errorLooper) {
115 executeErrorHandler(m_exception);
121 * @brief Register error handler for promise rejection
122 * @tparam tValue Value type being stored in the promise
123 * @tparam F Function type for error handler callback
124 * @param looper_ SLLooper instance for error handler execution
125 * @param errorHandler Callback function to execute when exception occurs
127 * Registers an error handler that will be executed when the promise
128 * is rejected with an exception. If an exception is already set,
129 * the error handler is executed immediately.
131 * @note Error handler receives std::exception_ptr for type safety
132 * @note Handles immediate execution for already-failed promises
135 * state->setErrorHandler(looper, [](std::exception_ptr ex) {
137 * std::rethrow_exception(ex);
138 * } catch (const std::exception& e) {
139 * std::cerr << "Promise failed: " << e.what() << std::endl;
144 * @see \ref swt::State::setErrorHandler "setErrorHandler", \ref swt::SLLooper "SLLooper"
146 template <typename tValue>
148 void State<tValue>::setErrorHandler(std::shared_ptr<SLLooper>& looper_, F&& errorHandler) {
149 m_errorLooper = looper_;
150 m_errorHandler = std::forward<F>(errorHandler);
152 // If exception is already set, execute immediately
154 executeErrorHandler(m_exception);
159 * @brief Execute continuation callback asynchronously via SLLooper
160 * @tparam tValue Value type being passed to continuation
161 * @param value Value to pass to continuation callback
163 * Posts the continuation callback to the associated SLLooper's message
164 * queue for asynchronous execution. This ensures the callback runs in
165 * the correct thread context and maintains thread safety.
167 * @note Uses lambda capture with move semantics for efficiency
168 * @note Private method called internally by setValue() and setContinuation()
170 * @see \ref swt::SLLooper "SLLooper"
172 template <typename tValue>
173 void State<tValue>::executeContination(tValue value) {
174 if (m_looper && m_continuation) {
175 m_looper->post([continuation = m_continuation, value = std::move(value)]() mutable {
176 continuation(std::move(value));
182 * @brief Execute error handler callback asynchronously via SLLooper
183 * @tparam tValue Value type (not used in error handling)
184 * @param exception Exception pointer to pass to error handler
186 * Posts the error handler callback to the associated SLLooper's message
187 * queue for asynchronous execution. This ensures error handling occurs
188 * in the correct thread context.
190 * @note Exception pointer is copyable and safe to capture
191 * @note Private method called internally by setException() and error propagation
193 * @see \ref swt::SLLooper "SLLooper"
195 template <typename tValue>
196 void State<tValue>::executeErrorHandler(std::exception_ptr exception) {
197 if (m_errorLooper && m_errorHandler) {
198 m_errorLooper->post([errorHandler = m_errorHandler, exception]() mutable {
199 errorHandler(exception);
204 // ========== Template Specialization for void type (std::monostate) ==========
207 * @brief Specialization: Register continuation for void promises
208 * @tparam F Function type for continuation callback
209 * @param looper_ SLLooper instance for callback execution
210 * @param continuation Callback function to execute (takes no parameters)
212 * Template specialization for handling void-returning promises using
213 * std::monostate as the internal value type. This allows the promise
214 * framework to work uniformly with both value-returning and void functions.
216 * @note Specialization for State<std::monostate> (represents void)
217 * @note Continuation callback takes no parameters for void promises
220 * auto voidState = std::make_shared<State<std::monostate>>();
221 * voidState->setContinuation(looper, []() {
222 * std::cout << "Void promise completed!" << std::endl;
226 * @see \ref swt::State::setContinuation "setContinuation", \ref swt::SLLooper "SLLooper"
229 void State<std::monostate>::setContinuation(std::shared_ptr<SLLooper>& looper_, F&& continuation) {
231 m_continuation = std::forward<F>(continuation);
233 // If value is already set, execute immediately
234 if (m_value.has_value()) {
235 executeContination(*m_value);
236 } else if (m_exception) {
237 // If exception is already set, propagate to error handler
238 if (m_errorHandler && m_errorLooper) {
239 executeErrorHandler(m_exception);
245 * @brief Specialization: Register error handler for void promises
246 * @tparam F Function type for error handler callback
247 * @param looper_ SLLooper instance for error handler execution
248 * @param errorHandler Callback function to execute when exception occurs
250 * Template specialization for error handling in void-returning promises.
251 * Identical behavior to the general template but specialized for
252 * State<std::monostate> type consistency.
254 * @note Specialization maintains API consistency for void promises
255 * @note Error handler signature remains the same (takes std::exception_ptr)
257 * @see \ref swt::State::setErrorHandler "setErrorHandler", \ref swt::SLLooper "SLLooper"
260 void State<std::monostate>::setErrorHandler(std::shared_ptr<SLLooper>& looper_, F&& errorHandler) {
261 m_errorLooper = looper_;
262 m_errorHandler = std::forward<F>(errorHandler);
264 // If exception is already set, execute immediately
266 executeErrorHandler(m_exception);