7#include "../utility/utility.hpp"
8#include "../concurrency/concurrency.hpp"
9#include "../macros.hpp"
17namespace hi::inline
v1 {
28template<
typename T =
void>
32 using notifier_type =
notifier<void(value_type)>;
33 using callback_token = notifier_type::callback_token;
34 using callback_proto = notifier_type::callback_proto;
37 notifier_type notifier;
38 std::optional<value_type> value = {};
41 void return_value(std::convertible_to<value_type>
auto&& new_value)
noexcept
43 value = hi_forward(new_value);
46 void unhandled_exception()
noexcept
51 std::suspend_always final_suspend()
noexcept
59 hi_assert_not_null(exception);
60 if constexpr (std::is_default_constructible_v<value_type>) {
61 notifier(value_type{});
68 scoped_task get_return_object()
noexcept
70 return scoped_task{handle_type::from_promise(*
this)};
81 using handle_type = std::coroutine_handle<promise_type>;
83 scoped_task(handle_type coroutine) noexcept : _coroutine(coroutine) {}
92 scoped_task() =
default;
96 scoped_task(scoped_task
const&) =
delete;
97 scoped_task& operator=(scoped_task
const&) =
delete;
99 scoped_task(scoped_task&& other)
noexcept
101 _coroutine = std::exchange(
other._coroutine, {});
104 scoped_task& operator=(scoped_task&& other)
noexcept
106 _coroutine = std::exchange(
other._coroutine, {});
112 [[nodiscard]]
bool done() const noexcept
114 hi_axiom(_coroutine);
115 return _coroutine.done();
120 explicit operator bool() const noexcept
130 [[nodiscard]] value_type
const&
value()
const
134 hilet& promise = _coroutine.promise();
136 return *promise.value;
139 hi_axiom(promise.exception);
163 return _coroutine.promise().notifier.subscribe(hi_forward(callback), flags);
168 handle_type _coroutine;
175class scoped_task<void> {
177 using value_type = void;
179 using callback_token = notifier_type::callback_token;
180 using callback_proto = notifier_type::callback_proto;
183 notifier_type notifier;
186 void return_void()
noexcept {}
188 void unhandled_exception()
noexcept
193 std::suspend_always final_suspend()
noexcept
199 scoped_task get_return_object()
noexcept
201 return scoped_task{handle_type::from_promise(*
this)};
204 std::suspend_never initial_suspend()
noexcept
210 using handle_type = std::coroutine_handle<promise_type>;
212 scoped_task(handle_type coroutine) noexcept : _coroutine(coroutine) {}
217 _coroutine.destroy();
221 scoped_task() =
default;
222 scoped_task(scoped_task
const&) =
delete;
223 scoped_task& operator=(scoped_task
const&) =
delete;
225 scoped_task(scoped_task&& other)
noexcept
227 _coroutine = std::exchange(
other._coroutine, {});
230 scoped_task& operator=(scoped_task&& other)
noexcept
232 _coroutine = std::exchange(
other._coroutine, {});
239 [[nodiscard]]
bool done() const noexcept
241 hi_axiom(_coroutine);
242 return _coroutine.done();
248 explicit operator bool() const noexcept
262 hilet& promise = _coroutine.promise();
263 if (promise.exception) {
273 return _coroutine.promise().notifier.subscribe(hi_forward(callback), flags);
278 handle_type _coroutine;
@ other
The gui_event does not have associated data.
Definition gui_event_variant.hpp:22
DOXYGEN BUG.
Definition algorithm.hpp:16
callback_flags
Definition callback_flags.hpp:14
@ synchronous
Call the function synchronously.
Definition callback_flags.hpp:17
A notifier which can be used to call a set of registered callbacks.
Definition notifier.hpp:27
A scoped_task.
Definition scoped_task.hpp:29
value_type const & operator*() const
Get the return value returned from co_return.
Definition scoped_task.hpp:149
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:161
value_type const & value() const
Get the return value returned from co_return.
Definition scoped_task.hpp:130
bool done() const noexcept
Check if the co-routine has completed.
Definition scoped_task.hpp:112
Definition scoped_task.hpp:36
std::suspend_never initial_suspend() noexcept
Before we enter the coroutine, allow the caller to set the callback.
Definition scoped_task.hpp:75
callback_token subscribe(forward_of< callback_proto > auto &&callback, callback_flags flags=callback_flags::synchronous) noexcept
Definition scoped_task.hpp:271
void value() const
Get the return value returned from co_return.
Definition scoped_task.hpp:258
bool done() const noexcept
Definition scoped_task.hpp:239
Definition scoped_task.hpp:182
T current_exception(T... args)
T rethrow_exception(T... args)