Fenix @develop
 
Loading...
Searching...
No Matches
awaiter.hpp
1#ifndef FENIX_TASKS_AWAITER_HPP
2#define FENIX_TASKS_AWAITER_HPP
3
4#include <coroutine>
5#include <exception>
6
7#include "subtask.hpp"
8
9namespace fenix::tasks {
10
11enum class AwaitMode { NonBlocking, Blocking };
12
13template <Subtaskable T>
14class Awaiter {
15 public:
16 using TaskT = Subtask<T>;
17
18 Awaiter(Subtask<T> t) : task(t) {}
19 Awaiter(SubtaskBase* t) : Awaiter(*static_cast<Subtask<T>*>(t)) {}
20
21 bool await_ready() const { return task.done(); }
22 auto await_resume() const noexcept { return task.result(); }
23
24 template <typename PromiseT>
25 bool await_suspend(std::coroutine_handle<PromiseT> h) const {
26 if (h.promise().await_mode == AwaitMode::Blocking) task.wait();
27 return !await_ready();
28 }
29
30 mutable Subtask<T> task;
31};
32
33} // namespace fenix::tasks
34
35#endif //FENIX_TASKS_AWAITER_HPP
Definition awaiter.hpp:14
Definition subtask.hpp:18
Definition subtask.hpp:44