8#include "async_task.hpp"
10#include "../macros.hpp"
12hi_export_module(hikogui.dispatch.async_controller);
14hi_export
namespace hi {
18template<
typename ResultType>
21 using result_type = ResultType;
30template<
typename ResultType,
typename FuncType,
typename... ArgTypes>
33 using result_type = ResultType;
35 template<
typename Func,
typename... Args>
67template<
typename ResultType>
73 using callback_type = notifier<>::callback_type;
77 task_controller& operator=(task_controller
const&) =
delete;
78 task_controller& operator=(task_controller&&) =
delete;
89 _progress_cb = _progress_sink.subscribe([
this] {
99 template<
typename Func,
typename... Args>
113 template<
typename Func,
typename... Args>
127 using namespace std::literals;
140 return _pimpl->features();
150 return static_cast<bool>(_pimpl);
160 return _task.started();
170 return _task.running();
175 [[nodiscard]]
bool done() const noexcept
195 _progress_sink.reset();
203 _task = _pimpl->run(_stop_source.get_token(), _progress_sink.get_token());
213 return _stop_source.request_stop();
223 return _progress_sink.value();
233 return _task.value();
238 template<
typename Callback>
239 hi::notifier<>::callback_type
subscribe(Callback&&
callback, callback_flags flags = callback_flags::synchronous)
246 std::stop_source _stop_source = {};
247 progress_sink _progress_sink = {};
248 progress_sink::callback_type _progress_cb = {};
249 task<result_type> _task = {};
250 hi::notifier<> _notifier = {};
The HikoGUI namespace.
Definition array_generic.hpp:21
The HikoGUI API version 1.
Definition array_generic.hpp:22
constexpr auto cancel_features_v
The value of the hi::cancel_features<> type trait.
Definition async_task.hpp:108
cancel_features_type
Features of an invocable.
Definition async_task.hpp:71
@ none
This invocable does not have extra arguments.
Definition async_task.hpp:74
auto cancelable_async_task(Func func, std::stop_token stop_token, ::hi::progress_token progress_token, Args... args)
Run a function asynchronously as a co-routine task.
Definition async_task.hpp:197
Definition callback.hpp:77
Token to pass to a function to report its progress.
Definition progress.hpp:23
Definition task_controller.hpp:19
Definition task_controller.hpp:59
A task controller.
Definition task_controller.hpp:68
void set_function(Func &&func, Args &&... args)
Set the coroutine or function and its arguments.
Definition task_controller.hpp:114
void run()
Run the assigned coroutine or function with the previous given arguments.
Definition task_controller.hpp:200
hi::notifier ::callback_type subscribe(Callback &&callback, callback_flags flags=callback_flags::synchronous)
Register a callback to be called when a coroutine or function reports progress.
Definition task_controller.hpp:239
bool request_stop() noexcept
Request stop.
Definition task_controller.hpp:211
float_t progress() const noexcept
Get progress of a function.
Definition task_controller.hpp:221
void reset()
Reset the state of the function to not-started.
Definition task_controller.hpp:187
result_type value() const
Get the return value from the coroutine or function.
Definition task_controller.hpp:231
bool runnable() const noexcept
Check if a function is assigned.
Definition task_controller.hpp:148
ResultType result_type
The result type returned by a hi::task or function.
Definition task_controller.hpp:72
bool running() const noexcept
Check if the function is currently running.
Definition task_controller.hpp:165
void unset_function()
Remove the task, so that it can no longer be run.
Definition task_controller.hpp:125
bool started() const noexcept
Check if the function was started.
Definition task_controller.hpp:155
task_controller(Func &&func, Args &&... args)
Create a new task_controller with a assigned coroutine or function and its arguments.
Definition task_controller.hpp:100
bool done() const noexcept
Check if the function has completed.
Definition task_controller.hpp:175
cancel_features_type features() const noexcept
The features of the coroutine or function that was assigned.
Definition task_controller.hpp:137
task_controller() noexcept
Create a new task_controller.
Definition task_controller.hpp:87
A concept for a callable that may be use in cancelable_async_task().
Definition async_task.hpp:173