8#include "awaitable.hpp"
9#include "../utility/utility.hpp"
12#include "../macros.hpp"
19hi_export_module(hikogui.dispatch : task);
21hi_export
namespace hi::inline
v1 {
30template<
typename T =
void,
bool DestroyFrame = false>
35 using callback_type = notifier_type::callback_type;
39 std::optional<value_type> value = {};
42 template<std::convertible_to<value_type> NewValue>
43 void return_value(NewValue&& new_value)
noexcept
45 value = std::forward<NewValue>(new_value);
48 void unhandled_exception()
noexcept
53 std::suspend_always final_suspend()
noexcept
61 hi_assert_not_null(exception);
62 if constexpr (std::is_default_constructible_v<value_type>) {
70 task get_return_object()
noexcept
72 return task{handle_type::from_promise(*
this)};
82 template<convertible_to_awaitable RHS>
83 decltype(
auto) await_transform(RHS&& rhs)
89 using handle_type = std::coroutine_handle<promise_type>;
91 task(handle_type coroutine) noexcept : _coroutine(coroutine) {}
95 if (DestroyFrame and _coroutine) {
104 task(task
const&) =
delete;
105 task& operator=(task
const&) =
delete;
107 task(task&& other)
noexcept
109 _coroutine = std::exchange(
other._coroutine, {});
112 task& operator=(task&& other)
noexcept
114 _coroutine = std::exchange(
other._coroutine, {});
129 return _coroutine and not _coroutine.done();
134 [[nodiscard]]
bool done() const noexcept
136 return _coroutine and _coroutine.done();
144 [[nodiscard]] value_type
const&
value()
const
148 auto const& promise = _coroutine.promise();
150 return *promise.value;
153 hi_axiom(promise.exception);
175 template<forward_of<
void(value_type)> Func>
178 return _coroutine.promise().notifier.subscribe(std::forward<Func>(func), flags);
183 auto operator co_await()
const noexcept
185 return _coroutine.promise().notifier.operator
co_await();
190 handle_type _coroutine;
196template<
bool DestroyFrame>
197class task<void, DestroyFrame> {
199 using value_type = void;
201 using callback_type = notifier_type::callback_type;
203 struct promise_type {
207 void return_void()
noexcept {}
209 void unhandled_exception()
noexcept
214 std::suspend_always final_suspend()
noexcept
220 task get_return_object()
noexcept
222 return task{handle_type::from_promise(*
this)};
225 std::suspend_never initial_suspend()
noexcept
230 template<convertible_to_awaitable RHS>
231 decltype(
auto) await_transform(RHS&& rhs)
237 using handle_type = std::coroutine_handle<promise_type>;
239 task(handle_type coroutine) noexcept : _coroutine(coroutine) {}
243 if (DestroyFrame and _coroutine) {
244 _coroutine.destroy();
249 task(task
const&) =
delete;
250 task& operator=(task
const&) =
delete;
252 task(task&& other)
noexcept
254 _coroutine = std::exchange(
other._coroutine, {});
257 task& operator=(task&& other)
noexcept
259 _coroutine = std::exchange(
other._coroutine, {});
274 return _coroutine and not _coroutine.done();
279 [[nodiscard]]
bool done() const noexcept
281 return _coroutine and _coroutine.done();
293 auto const& promise = _coroutine.promise();
294 if (promise.exception) {
302 template<forward_of<
void()> Func>
305 return _coroutine.promise().notifier.subscribe(std::forward<Func>(func), flags);
310 auto operator co_await()
const noexcept
312 return _coroutine.promise().notifier.operator
co_await();
317 handle_type _coroutine;
320template<
typename T =
void>
321using scoped_task = task<T, true>;
328template<
typename ResultType,
bool DestroyFrame>
339template<
typename ResultType,
bool DestroyFrame>
341 using type = ResultType;
349template<
typename Func,
typename... ArgTypes>
351 constexpr static bool value =
is_task_v<std::invoke_result_t<Func, ArgTypes...>>;
356template<
typename Func,
typename... ArgTypes>
359template<
typename Func,
typename... ArgTypes>
361 using type = task_value_type_t<std::invoke_result_t<Func, ArgTypes...>>;
364template<
typename Func,
typename... ArgTypes>
Functions and types for accessing operating system threads.
Definition of the unfair_mutex.
@ other
The gui_event does not have associated data.
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
constexpr bool is_invocable_task_v
type-trait to determining if the given invocable Func is a task.
Definition task.hpp:357
callback_flags
Definition callback_flags.hpp:15
constexpr bool is_task_v
type-trait to determine if the given type T is a task.
Definition task.hpp:334
Definition awaitable.hpp:41
A task.
Definition task.hpp:31
bool started() const noexcept
Check if the co-routine was started.
Definition task.hpp:120
bool done() const noexcept
Check if the co-routine has completed.
Definition task.hpp:134
callback< void(value_type)> subscribe(Func &&func, callback_flags flags=callback_flags::synchronous) noexcept
Subscribe a callback for when the co-routine is completed.
Definition task.hpp:176
value_type const & operator*() const
Get the return value returned from co_return.
Definition task.hpp:163
value_type const & value() const
Get the return value returned from co_return.
Definition task.hpp:144
bool running() const noexcept
Check if the co-routine is running.
Definition task.hpp:127
std::suspend_never initial_suspend() noexcept
Before we enter the coroutine, allow the caller to set the callback.
Definition task.hpp:77
callback< void()> subscribe(Func &&func, callback_flags flags=callback_flags::synchronous) noexcept
Definition task.hpp:303
bool started() const noexcept
Check if the co-routine was started.
Definition task.hpp:265
bool running() const noexcept
Check if the co-routine is running.
Definition task.hpp:272
bool done() const noexcept
Check if the co-routine has completed.
Definition task.hpp:279
void value() const
Get the return value returned from co_return.
Definition task.hpp:289
type-trait to determine if the given type T is a task.
Definition task.hpp:326
type-trait to determining if the given invocable Func is a task.
Definition task.hpp:350
T current_exception(T... args)
T rethrow_exception(T... args)