10#include "awaitable_timer.hpp"
16namespace hi::inline v1 {
27template<
typename T =
void>
35 std::optional<value_type> value = {};
38 void return_value(std::convertible_to<value_type>
auto&& new_value)
noexcept
43 void unhandled_exception()
noexcept
48 std::suspend_always final_suspend()
noexcept
57 if constexpr (std::is_default_constructible_v<value_type>) {
67 return scoped_task{handle_type::from_promise(*
this)};
78 using handle_type = std::coroutine_handle<promise_type>;
80 scoped_task(handle_type coroutine) noexcept : _coroutine(coroutine) {}
89 scoped_task() =
default;
93 scoped_task(scoped_task
const&) =
delete;
94 scoped_task& operator=(scoped_task
const&) =
delete;
96 scoped_task(scoped_task&& other)
noexcept
98 _coroutine = std::exchange(other._coroutine, {});
101 scoped_task& operator=(scoped_task&& other)
noexcept
103 _coroutine = std::exchange(other._coroutine, {});
109 [[nodiscard]]
bool done() const noexcept
111 hi_axiom(_coroutine);
112 return _coroutine.done();
117 explicit operator bool() const noexcept
127 [[nodiscard]] value_type
const&
value()
const
131 hilet& promise = _coroutine.promise();
133 return *promise.value;
136 hi_axiom(promise.exception);
156 notifier_type::token_type
subscribe(callback_flags flags, std::invocable<value_type>
auto&& callback)
noexcept
158 return _coroutine.promise().notifier.subscribe(flags,
hi_forward(callback));
166 notifier_type::token_type
subscribe(std::invocable<value_type>
auto&& callback)
noexcept
168 return subscribe(callback_flags::synchronous,
hi_forward(callback));
173 handle_type _coroutine;
182 using value_type = void;
185 struct promise_type {
189 void return_void()
noexcept {}
191 void unhandled_exception()
noexcept
196 std::suspend_always final_suspend()
noexcept
204 return scoped_task{handle_type::from_promise(*
this)};
207 std::suspend_never initial_suspend()
noexcept
213 using handle_type = std::coroutine_handle<promise_type>;
215 scoped_task(handle_type coroutine) noexcept : _coroutine(coroutine) {}
220 _coroutine.destroy();
224 scoped_task() =
default;
225 scoped_task(scoped_task
const&) =
delete;
226 scoped_task& operator=(scoped_task
const&) =
delete;
228 scoped_task(scoped_task&& other)
noexcept
230 _coroutine = std::exchange(other._coroutine, {});
233 scoped_task& operator=(scoped_task&& other)
noexcept
235 _coroutine = std::exchange(other._coroutine, {});
242 [[nodiscard]]
bool done() const noexcept
244 hi_axiom(_coroutine);
245 return _coroutine.done();
251 explicit operator bool() const noexcept
266 hilet& promise = _coroutine.promise();
267 if (promise.exception) {
275 notifier_type::token_type
subscribe(callback_flags flags, std::invocable<>
auto&& callback)
noexcept
277 return _coroutine.promise().notifier.subscribe(flags,
hi_forward(callback));
283 notifier_type::token_type
subscribe(std::invocable<>
auto&& callback)
noexcept
285 return subscribe(callback_flags::synchronous,
hi_forward(callback));
290 handle_type _coroutine;
This file includes required definitions.
#define hilet
Invariant should be the default for variables.
Definition required.hpp:23
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition required.hpp:29
A scoped_task.
Definition scoped_task.hpp:28
value_type const & operator*() const
Get the return value returned from co_return.
Definition scoped_task.hpp:146
notifier_type::token_type subscribe(callback_flags flags, std::invocable< value_type > auto &&callback) noexcept
Subscribe a callback for when the co-routine is completed.
Definition scoped_task.hpp:156
value_type const & value() const
Get the return value returned from co_return.
Definition scoped_task.hpp:127
notifier_type::token_type subscribe(std::invocable< value_type > auto &&callback) noexcept
Subscribe a callback for when the co-routine is completed.
Definition scoped_task.hpp:166
bool done() const noexcept
Check if the co-routine has completed.
Definition scoped_task.hpp:109
Definition scoped_task.hpp:33
std::suspend_never initial_suspend() noexcept
Before we enter the coroutine, allow the caller to set the callback.
Definition scoped_task.hpp:72
void value() const
Get the return value returned from co_return.
Definition scoped_task.hpp:262
bool done() const noexcept
Definition scoped_task.hpp:242
notifier_type::token_type subscribe(callback_flags flags, std::invocable<> auto &&callback) noexcept
Definition scoped_task.hpp:275
notifier_type::token_type subscribe(std::invocable<> auto &&callback) noexcept
Definition scoped_task.hpp:283
T current_exception(T... args)
T rethrow_exception(T... args)