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>
36 task_controller_impl(Func&& func, Args&&... args) : _func(std::forward<Func>(func)), _args(std::forward<Args>(args)...)
48 cancelable_async_task<FuncType, ArgTypes...>,
67template<
typename ResultType>
73 using callback_type = notifier<>::callback_type;
89 _progress_cb = _progress_sink.
subscribe([
this] {
99 template<
typename Func,
typename... Args>
103 _pimpl = std::make_shared<detail::task_controller_impl<result_type, std::decay_t<Func>, std::decay_t<Args>...>>(
104 std::forward<Func>(func), std::forward<Args>(args)...);
113 template<
typename Func,
typename... Args>
117 _pimpl = std::make_shared<detail::task_controller_impl<result_type, std::decay_t<Func>, std::decay_t<Args>...>>(
118 std::forward<Func>(func), std::forward<Args>(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)
241 return _notifier.subscribe(std::forward<Callback>(
callback), flags);
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:20
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.
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Definition callback.hpp:77
Token to pass to a function to report its progress.
Definition progress.hpp:23
progress_token get_token() const noexcept
Get a token to pass to a function.
Definition progress.hpp:59
constexpr float value() const noexcept
Get the current progress.
Definition progress.hpp:81
void reset()
Reset progress.
Definition progress.hpp:66
notifier ::callback_type subscribe(Callback &&callback, callback_flags flags=callback_flags::synchronous)
Subscribe a callback function to be called when progress is modified.
Definition progress.hpp:96
Definition task_controller.hpp:19
Definition task_controller.hpp:31
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