7#include "../utility/utility.hpp"
9#include "../macros.hpp"
17hi_export_module(hikogui.concurrency.callback);
19hi_export
namespace hi {
23template<
typename ResultType,
typename... ArgTypes>
28 virtual ResultType operator()(ArgTypes... args) = 0;
31template<
typename FunctionType,
typename ResultType,
typename... ArgTypes>
37 auto const _ = std::scoped_lock(_mutex);
38 hi_assert(_thread_ids.
empty());
42 template<
typename Func>
47 ResultType operator()(ArgTypes... args)
override
50 auto const _ = std::scoped_lock(_mutex);
52 hi_assert(not std::ranges::contains(_thread_ids, thread_id));
54 auto const d =
defer([&] {
55 std::erase(_thread_ids, thread_id);
59 return _func(args...);
73template<
typename T =
void()>
76template<
typename T =
void()>
79template<
typename ResultType,
typename... ArgTypes>
80class callback<ResultType(ArgTypes...)>;
82template<
typename ResultType,
typename... ArgTypes>
85 using result_type = ResultType;
101 [[nodiscard]]
long use_count()
const noexcept
103 return _impl.use_count();
114 return _impl.expired();
117 [[nodiscard]] callback_type lock() const noexcept;
120 using base_impl_type = detail::callback_base<ResultType, ArgTypes...>;
122 std::weak_ptr<base_impl_type> _impl;
152template<typename ResultType, typename... ArgTypes>
155 using result_type = ResultType;
156 using weak_callback_type =
weak_callback<ResultType(ArgTypes...)>;
172 template<
typename Func>
177 void reset()
noexcept
179 return _impl.reset();
182 [[nodiscard]]
long use_count()
const noexcept
184 return _impl.use_count();
187 [[nodiscard]]
operator bool()
const noexcept
189 return static_cast<bool>(_impl);
201 template<
typename... Args>
202 decltype(
auto)
operator()(Args... args)
210 return (*_impl)(std::forward<Args>(args)...);
216 template<
typename FunctionType>
223 friend weak_callback<ResultType(ArgTypes...)>;
226template<
typename ResultType,
typename... ArgTypes>
227inline weak_callback<ResultType(ArgTypes...)>::weak_callback(callback<ResultType(ArgTypes...)>
const&
other) noexcept :
232template<
typename ResultType,
typename... ArgTypes>
233[[nodiscard]]
inline callback<ResultType(ArgTypes...)> weak_callback<ResultType(ArgTypes...)>::lock() const noexcept
235 return {_impl.lock()};
Functions and types for accessing operating system threads.
thread_id current_thread_id() noexcept
Get the current thread id.
@ other
The gui_event does not have associated data.
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Definition callback.hpp:24
Definition callback.hpp:32
Definition callback.hpp:74
Definition callback.hpp:77
bool expired() const noexcept
Check if the callback object is expired.
Definition callback.hpp:112
A callback function.
Definition callback.hpp:153
Defer execution of a lambda to the end of the scope.
Definition defer.hpp:21