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
Promise.h
Go to the documentation of this file.
1
9 #ifndef PROMISE_HPP
10 #define PROMISE_HPP
11
12 #include <type_traits>
13 #include <memory>
14 #include <functional>
15 #include <variant>
16 #include <exception>
17 #include "Log.h"
18
19 // Forward declarations
20 namespace swt {
21 class SLLooper;
22 template <typename tValue> class State;
23
66 template <typename tValue>
67 class Promise
68 {
69 public:
79
96 void set_value(tValue value);
97
117 void set_exception(std::exception_ptr exception);
118
151 template <typename F>
152 auto then(std::shared_ptr<SLLooper>& looper_, F func) -> Promise<std::invoke_result_t<F, tValue>>;
153
188 template <typename F>
189 auto catchError(std::shared_ptr<SLLooper>& looper_, F func) -> Promise<tValue>;
190
203 void operator()(tValue value) {
204 set_value(std::move(value));
205 }
206
207 private:
208 std::shared_ptr<State<tValue>> m_state;
209 };
210
211 // ========== Template Specialization for void type ==========
212
246 template <>
247 class Promise<void>
248 {
249 public:
258 Promise();
259
275 void set_value();
276
287 void set_exception(std::exception_ptr exception);
288
313 template <typename F>
314 auto then(std::shared_ptr<SLLooper>& looper_, F func) -> Promise<std::invoke_result_t<F>>;
315
341 template <typename F>
342 auto catchError(std::shared_ptr<SLLooper>& looper_, F func) -> Promise<void>;
343
355 void operator()() {
356 set_value();
357 }
358
359 private:
360 std::shared_ptr<State<std::monostate>> m_state;
361 };
362
363 } // namespace swt
364
365 // Include template implementations
366 #include "Promise.tpp"
367
368 #endif // PROMISE_HPP
auto catchError(std::shared_ptr< SLLooper > &looper_, F func) -> Promise< void >
Chain error handler for void promise rejection.
auto then(std::shared_ptr< SLLooper > &looper_, F func) -> Promise< std::invoke_result_t< F > >
Chain continuation callback for void promise completion.
void operator()()
Function call operator for convenient void promise resolution.
Definition Promise.h:355
Type-safe promise for asynchronous result handling with continuation chaining.
Definition Promise.h:68
void set_exception(std::exception_ptr exception)
Reject promise with an exception.
Promise()
Default constructor - creates promise with shared state.
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 operator()(tValue value)
Function call operator for convenient promise resolution.
Definition Promise.h:203
void set_value(tValue value)
Resolve promise with a value.
Software Timer namespace containing all timer-related classes.
Definition Awaitable.h:21