8#include "generator.hpp"
10#include "callback_flags.hpp"
16namespace hi::inline
v1 {
23template<
typename T =
void()>
29template<
typename Result,
typename... Args>
32 static_assert(std::is_same_v<Result, void>,
"Result of a notifier must be void.");
34 using result_type = Result;
35 using callback_proto = Result(Args...);
49 constexpr awaiter_type()
noexcept =
default;
50 constexpr awaiter_type(awaiter_type
const&)
noexcept =
default;
51 constexpr awaiter_type(awaiter_type&&)
noexcept =
default;
52 constexpr awaiter_type& operator=(awaiter_type
const&)
noexcept =
default;
53 constexpr awaiter_type& operator=(awaiter_type&&)
noexcept =
default;
57 [[nodiscard]]
constexpr bool await_ready()
noexcept
62 void await_suspend(std::coroutine_handle<> handle)
noexcept
64 hi_axiom(_notifier !=
nullptr);
68 _cbt = _notifier->subscribe(
69 [
this, handle](Args
const&...args) {
77 callback_flags::main | callback_flags::once);
80 constexpr void await_resume()
const noexcept requires(
sizeof...(Args) == 0) {}
82 constexpr auto await_resume()
const noexcept requires(
sizeof...(Args) == 1)
84 return std::get<0>(_args);
87 constexpr auto await_resume()
const noexcept requires(
sizeof...(Args) > 1)
108 awaiter_type operator co_await() const noexcept
110 return awaiter_type{
const_cast<notifier&
>(*this)};
122 [[nodiscard]] callback_token
125 auto token = std::make_shared<function_type>(
hi_forward(callback));
126 _callbacks.emplace_back(token, flags);
137 for (
auto& callback : _callbacks) {
138 if (is_synchronous(callback.flags)) {
139 if (
auto func = callback.lock()) {
143 }
else if (is_local(callback.flags)) {
144 loop::local().post_function([=] {
145 if (
auto func = callback.lock()) {
150 }
else if (is_main(callback.flags)) {
151 loop::main().post_function([=] {
152 if (
auto func = callback.lock()) {
157 }
else if (is_timer(callback.flags)) {
158 loop::timer().post_function([=] {
159 if (
auto func = callback.lock()) {
172 if (is_once(callback.flags)) {
180 struct callback_type {
181 weak_callback_token token;
184 [[nodiscard]]
bool expired() const noexcept
186 return token.expired();
189 void reset() noexcept
194 [[nodiscard]] callback_token
lock() const noexcept
204 void clean_up() const noexcept
207 std::erase_if(_callbacks, [](
hilet& item) {
208 return item.expired();
215 mutable bool _notifying =
false;
Utilities used by the HikoGUI library itself.
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition utility.hpp:29
DOXYGEN BUG.
Definition algorithm.hpp:15
callback_flags
Definition callback_flags.hpp:12
A notifier which can be used to call a set of registered callbacks.
Definition notifier.hpp:24
callback_token subscribe(forward_of< callback_proto > auto &&callback, callback_flags flags=callback_flags::synchronous) noexcept
Add a callback to the notifier.
Definition notifier.hpp:123
constexpr notifier() noexcept=default
Create a notifier.
void operator()(Args const &...args) const noexcept
Call the subscribed callbacks with the given arguments.
Definition notifier.hpp:135
True if T is a forwarded type of Forward.
Definition concepts.hpp:130