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
Awaitable.h
Go to the documentation of this file.
1
13 #pragma once
14
15 #include <coroutine>
16 #include <exception>
17 #include <optional>
18 #include <memory>
19 #include <functional>
20
21 namespace swt {
22
23 // Forward declaration
24 class SLLooper;
25
42 template<typename T>
44 private:
45 std::shared_ptr<SLLooper> mLooper;
46 std::function<T()> mFunc;
47 mutable std::optional<T> mResult;
48 mutable std::exception_ptr mException;
49 mutable bool mReady = false;
50
51 public:
58 template<typename Func>
59 WorkAwaitable(std::shared_ptr<SLLooper> looper, Func&& func)
60 : mLooper(looper), mFunc(std::forward<Func>(func)) {}
61
66 bool await_ready() const noexcept { return mReady; }
67
72 void await_suspend(std::coroutine_handle<> handle) const noexcept;
73
79 auto await_resume() const -> T {
80 if (mException) std::rethrow_exception(mException);
81 if (mResult.has_value()) return std::move(mResult.value());
82 throw std::runtime_error("Work not completed");
83 }
84
89 std::shared_ptr<SLLooper> getLooper() const { return mLooper; }
90
95 std::function<T()> getFunc() const { return mFunc; }
96
101 void setResult(T result) const {
102 mResult = result;
103 mReady = true;
104 }
105
110 void setException(std::exception_ptr ex) const {
111 mException = ex;
112 mReady = true;
113 }
114 };
115
130 template<>
131 class WorkAwaitable<void> {
132 private:
133 std::shared_ptr<SLLooper> mLooper;
134 std::function<void()> mFunc;
135 mutable std::exception_ptr mException;
136 mutable bool mReady = false;
137
138 public:
145 template<typename Func>
146 WorkAwaitable(std::shared_ptr<SLLooper> looper, Func&& func)
147 : mLooper(looper), mFunc(std::forward<Func>(func)) {}
148
153 bool await_ready() const noexcept { return mReady; }
154
159 void await_suspend(std::coroutine_handle<> handle) const noexcept;
160
165 void await_resume() const {
166 if (mException) std::rethrow_exception(mException);
167 if (!mReady) throw std::runtime_error("Work not completed");
168 }
169
174 std::shared_ptr<SLLooper> getLooper() const { return mLooper; }
175
180 std::function<void()> getFunc() const { return mFunc; }
181
185 void setResult() const { mReady = true; }
186
191 void setException(std::exception_ptr ex) const {
192 mException = ex;
193 mReady = true;
194 }
195 };
196
209 private:
210 std::shared_ptr<SLLooper> mLooper;
211 int mDelayMs;
212 mutable bool mReady = false;
213
214 public:
220 DelayAwaitable(std::shared_ptr<SLLooper> looper, int delayMs)
221 : mLooper(looper), mDelayMs(delayMs) {}
222
227 bool await_ready() const noexcept { return mReady; }
228
233 void await_suspend(std::coroutine_handle<> handle) const noexcept;
234
238 void await_resume() const noexcept {}
239
244 int getDelayMs() const { return mDelayMs; }
245
250 std::shared_ptr<SLLooper> getLooper() const { return mLooper; }
251
255 void setReady() const { mReady = true; }
256 };
257
274 template<typename T>
276 private:
277 std::shared_ptr<SLLooper> mLooper;
278 std::function<T()> mFunc;
279 mutable std::optional<T> mResult;
280 mutable std::exception_ptr mException;
281 mutable bool mReady = false;
282
283 public:
290 template<typename Func>
291 PostAwaitable(std::shared_ptr<SLLooper> looper, Func&& func)
292 : mLooper(looper), mFunc(std::forward<Func>(func)) {}
293
298 bool await_ready() const noexcept { return mReady; }
299
304 void await_suspend(std::coroutine_handle<> handle) const noexcept;
305
311 auto await_resume() const -> T {
312 if (mException) std::rethrow_exception(mException);
313 if (mResult.has_value()) return std::move(mResult.value());
314 throw std::runtime_error("Post not completed");
315 }
316
321 std::function<T()> getFunc() const { return mFunc; }
322
327 std::shared_ptr<SLLooper> getLooper() const { return mLooper; }
328
333 void setResult(T result) const {
334 mResult = result;
335 mReady = true;
336 }
337
342 void setException(std::exception_ptr ex) const {
343 mException = ex;
344 mReady = true;
345 }
346 };
347
362 template<>
363 class PostAwaitable<void> {
364 private:
365 std::shared_ptr<SLLooper> mLooper;
366 std::function<void()> mFunc;
367 mutable std::exception_ptr mException;
368 mutable bool mReady = false;
369
370 public:
377 template<typename Func>
378 PostAwaitable(std::shared_ptr<SLLooper> looper, Func&& func)
379 : mLooper(looper), mFunc(std::forward<Func>(func)) {}
380
385 bool await_ready() const noexcept { return mReady; }
386
391 void await_suspend(std::coroutine_handle<> handle) const noexcept;
392
397 void await_resume() const {
398 if (mException) std::rethrow_exception(mException);
399 }
400
405 std::function<void()> getFunc() const { return mFunc; }
406
411 std::shared_ptr<SLLooper> getLooper() const { return mLooper; }
412
416 void setResult() const { mReady = true; }
417
422 void setException(std::exception_ptr ex) const {
423 mException = ex;
424 mReady = true;
425 }
426 };
427
428 } // namespace swt
Awaitable for delay operations.
Definition Awaitable.h:208
void setReady() const
Mark delay as completed (internal use)
Definition Awaitable.h:255
bool await_ready() const noexcept
Check if delay is ready (coroutine interface)
Definition Awaitable.h:227
void await_suspend(std::coroutine_handle<> handle) const noexcept
Suspend coroutine and start timer.
Definition SLLooper.cpp:246
int getDelayMs() const
Get delay duration.
Definition Awaitable.h:244
void await_resume() const noexcept
Resume coroutine after delay (no return value)
Definition Awaitable.h:238
DelayAwaitable(std::shared_ptr< SLLooper > looper, int delayMs)
Construct DelayAwaitable with delay duration.
Definition Awaitable.h:220
std::shared_ptr< SLLooper > getLooper() const
Get the SLLooper instance.
Definition Awaitable.h:250
std::shared_ptr< SLLooper > getLooper() const
Get the SLLooper instance.
Definition Awaitable.h:411
PostAwaitable(std::shared_ptr< SLLooper > looper, Func &&func)
Construct PostAwaitable with void function.
Definition Awaitable.h:378
std::function< void()> getFunc() const
Get the main thread function.
Definition Awaitable.h:405
void setResult() const
Mark operation as completed (internal use)
Definition Awaitable.h:416
void await_resume() const
Resume coroutine after void operation completion.
Definition Awaitable.h:397
void setException(std::exception_ptr ex) const
Set exception result (internal use)
Definition Awaitable.h:422
bool await_ready() const noexcept
Check if operation is ready (coroutine interface)
Definition Awaitable.h:385
Awaitable for executing function on main thread.
Definition Awaitable.h:275
void await_suspend(std::coroutine_handle<> handle) const noexcept
Suspend coroutine and post to main thread.
Definition SLLooper.cpp:305
bool await_ready() const noexcept
Check if result is ready (coroutine interface)
Definition Awaitable.h:298
std::function< T()> getFunc() const
Get the main thread function.
Definition Awaitable.h:321
void setException(std::exception_ptr ex) const
Set exception result (internal use)
Definition Awaitable.h:342
void setResult(T result) const
Set successful result (internal use)
Definition Awaitable.h:333
PostAwaitable(std::shared_ptr< SLLooper > looper, Func &&func)
Construct PostAwaitable with function.
Definition Awaitable.h:291
std::shared_ptr< SLLooper > getLooper() const
Get the SLLooper instance.
Definition Awaitable.h:327
auto await_resume() const -> T
Resume coroutine and return result.
Definition Awaitable.h:311
Central event loop coordinator providing asynchronous task execution and timer management.
Definition SLLooper.h:83
std::shared_ptr< SLLooper > getLooper() const
Get the SLLooper instance.
Definition Awaitable.h:174
bool await_ready() const noexcept
Check if work is ready (coroutine interface)
Definition Awaitable.h:153
void await_resume() const
Resume coroutine after void work completion.
Definition Awaitable.h:165
WorkAwaitable(std::shared_ptr< SLLooper > looper, Func &&func)
Construct WorkAwaitable with void function.
Definition Awaitable.h:146
void setException(std::exception_ptr ex) const
Set exception result (internal use)
Definition Awaitable.h:191
std::function< void()> getFunc() const
Get the work function.
Definition Awaitable.h:180
void setResult() const
Mark work as completed (internal use)
Definition Awaitable.h:185
Awaitable for executing work on background thread.
Definition Awaitable.h:43
std::shared_ptr< SLLooper > getLooper() const
Get the SLLooper instance.
Definition Awaitable.h:89
void setException(std::exception_ptr ex) const
Set exception result (internal use)
Definition Awaitable.h:110
void await_suspend(std::coroutine_handle<> handle) const noexcept
Suspend coroutine and execute work on background thread.
Definition SLLooper.cpp:258
void setResult(T result) const
Set successful result (internal use)
Definition Awaitable.h:101
bool await_ready() const noexcept
Check if result is ready (coroutine interface)
Definition Awaitable.h:66
auto await_resume() const -> T
Resume coroutine and return result.
Definition Awaitable.h:79
WorkAwaitable(std::shared_ptr< SLLooper > looper, Func &&func)
Construct WorkAwaitable with function.
Definition Awaitable.h:59
std::function< T()> getFunc() const
Get the work function.
Definition Awaitable.h:95
Software Timer namespace containing all timer-related classes.
Definition Awaitable.h:21