10#include "awaitable_timer.hpp"
16namespace hi::inline
v1 {
27template<
typename T =
void>
32 using callback_token = notifier_type::callback_token;
33 using callback_proto = notifier_type::callback_proto;
37 std::optional<value_type> value = {};
40 void return_value(std::convertible_to<value_type>
auto&& new_value)
noexcept
45 void unhandled_exception()
noexcept
50 std::suspend_always final_suspend()
noexcept
59 if constexpr (std::is_default_constructible_v<value_type>) {
69 return scoped_task{handle_type::from_promise(*
this)};
80 using handle_type = std::coroutine_handle<promise_type>;
82 scoped_task(handle_type coroutine) noexcept : _coroutine(coroutine) {}
91 scoped_task() =
default;
95 scoped_task(scoped_task
const&) =
delete;
96 scoped_task& operator=(scoped_task
const&) =
delete;
98 scoped_task(scoped_task&& other)
noexcept
100 _coroutine = std::exchange(other._coroutine, {});
103 scoped_task& operator=(scoped_task&& other)
noexcept
105 _coroutine = std::exchange(other._coroutine, {});
111 [[nodiscard]]
bool done() const noexcept
114 return _coroutine.done();
119 explicit operator bool() const noexcept
129 [[nodiscard]] value_type
const&
value()
const
133 hilet& promise = _coroutine.promise();
135 return *promise.value;
162 return _coroutine.promise().notifier.subscribe(
hi_forward(callback), flags);
167 handle_type _coroutine;
176 using value_type = void;
178 using callback_token = notifier_type::callback_token;
179 using callback_proto = notifier_type::callback_proto;
181 struct promise_type {
185 void return_void()
noexcept {}
187 void unhandled_exception()
noexcept
192 std::suspend_always final_suspend()
noexcept
200 return scoped_task{handle_type::from_promise(*
this)};
203 std::suspend_never initial_suspend()
noexcept
209 using handle_type = std::coroutine_handle<promise_type>;
211 scoped_task(handle_type coroutine) noexcept : _coroutine(coroutine) {}
216 _coroutine.destroy();
220 scoped_task() =
default;
221 scoped_task(scoped_task
const&) =
delete;
222 scoped_task& operator=(scoped_task
const&) =
delete;
224 scoped_task(scoped_task&& other)
noexcept
226 _coroutine = std::exchange(other._coroutine, {});
229 scoped_task& operator=(scoped_task&& other)
noexcept
231 _coroutine = std::exchange(other._coroutine, {});
238 [[nodiscard]]
bool done() const noexcept
241 return _coroutine.done();
247 explicit operator bool() const noexcept
261 hilet& promise = _coroutine.promise();
262 if (promise.exception) {
272 return _coroutine.promise().notifier.subscribe(
hi_forward(callback), flags);
277 handle_type _coroutine;
#define hi_axiom(expression)
Specify an axiom; an expression that is true.
Definition assert.hpp:133
#define hi_assert_not_null(x)
Assert if an expression is not nullptr.
Definition assert.hpp:118
Utilities used by the HikoGUI library itself.
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition utility.hpp:29
DOXYGEN BUG.
Definition algorithm.hpp:15
callback_flags
Definition callback_flags.hpp:12
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:148
callback_token subscribe(forward_of< callback_proto > auto &&callback, callback_flags flags=callback_flags::synchronous) noexcept
Subscribe a callback for when the co-routine is completed.
Definition scoped_task.hpp:160
value_type const & value() const
Get the return value returned from co_return.
Definition scoped_task.hpp:129
bool done() const noexcept
Check if the co-routine has completed.
Definition scoped_task.hpp:111
Definition scoped_task.hpp:35
std::suspend_never initial_suspend() noexcept
Before we enter the coroutine, allow the caller to set the callback.
Definition scoped_task.hpp:74
callback_token subscribe(forward_of< callback_proto > auto &&callback, callback_flags flags=callback_flags::synchronous) noexcept
Definition scoped_task.hpp:270
void value() const
Get the return value returned from co_return.
Definition scoped_task.hpp:257
bool done() const noexcept
Definition scoped_task.hpp:238
True if T is a forwarded type of Forward.
Definition concepts.hpp:130
T current_exception(T... args)
T rethrow_exception(T... args)