8#include "generator.hpp"
15namespace hi::inline v1 {
22template<
typename T =
void()>
28template<
typename Result,
typename... Args>
31 static_assert(std::is_same_v<Result, void>,
"Result of a notifier must be void.");
33 using result_type = Result;
47 constexpr awaiter_type()
noexcept =
default;
48 constexpr awaiter_type(awaiter_type
const&)
noexcept =
default;
49 constexpr awaiter_type(awaiter_type&&)
noexcept =
default;
50 constexpr awaiter_type& operator=(awaiter_type
const&)
noexcept =
default;
51 constexpr awaiter_type& operator=(awaiter_type&&)
noexcept =
default;
55 [[nodiscard]]
constexpr bool await_ready()
noexcept
60 void await_suspend(std::coroutine_handle<> handle)
noexcept
62 hi_axiom(_notifier !=
nullptr);
66 _cbt = _notifier->subscribe([
this, handle](Args
const&...args) {
72 constexpr void await_resume()
const noexcept requires(
sizeof...(Args) == 0) {}
74 constexpr auto await_resume()
const noexcept requires(
sizeof...(Args) == 1)
76 return std::get<0>(_args);
79 constexpr auto await_resume()
const noexcept requires(
sizeof...(Args) > 1)
84 [[nodiscard]]
bool operator==(awaiter_type
const& rhs)
const noexcept
86 return _notifier == rhs._notifier;
105 awaiter_type operator co_await() const noexcept
107 return awaiter_type{
const_cast<notifier&
>(*this)};
120 auto token = std::make_shared<callback_type>(
hi_forward(callback));
121 _callbacks.emplace_back(token);
130 void post(Args
const&...args)
const noexcept requires(std::is_same_v<result_type, void>)
132 for (
auto& weak_callback : _callbacks) {
133 loop::local().post_function([=] {
134 if (
auto callback = weak_callback.lock()) {
135 (*callback)(args...);
147 void post_on_main(Args
const&...args)
const noexcept requires(std::is_same_v<result_type, void>)
149 for (
auto& weak_callback : _callbacks) {
150 loop::main().post_function([=] {
151 if (
auto callback = weak_callback.lock()) {
152 (*callback)(args...);
166 return post(args...);
174 void clean_up() const noexcept
176 std::erase_if(_callbacks, [](
hilet& item) {
177 return item.expired();
181#if HI_BUILD_TYPE == HI_BT_DEBUG
184 mutable bool _notifying =
false;
This file includes required definitions.
#define hilet
Invariant should be the default for variables.
Definition required.hpp:23
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition required.hpp:29
A notifier which can be used to call a set of registered callbacks.
Definition notifier.hpp:23
auto operator()(Args const &...args) const noexcept
Call the subscribed callbacks with the given arguments.
Definition notifier.hpp:164
constexpr notifier() noexcept=default
Create a notifier.
token_type subscribe(std::invocable< Args... > auto &&callback) noexcept
Add a callback to the notifier.
Definition notifier.hpp:118
void post(Args const &...args) const noexcept
Post the subscribed callbacks on the current thread's event loop with the given arguments.
Definition notifier.hpp:130
void post_on_main(Args const &...args) const noexcept
Post the subscribed callbacks on the main thread's event loop with the given arguments.
Definition notifier.hpp:147