8#include "unfair_recursive_mutex.hpp"
9#include "coroutine.hpp"
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;
46 auto lock = std::scoped_lock(_mutex);
48 ttlet i =
std::find_if(_callbacks.cbegin(), _callbacks.cend(), [&callback_ptr](ttlet &item) {
49 return item.lock() == callback_ptr;
52 if (i == _callbacks.cend()) {
53 _callbacks.emplace_back(callback_ptr);
66 template<
typename Callback>
requires (std::is_invocable_v<Callback>)
69 auto callback_ptr = std::make_shared<callback_type>(
std::forward<
decltype(callback)>(callback));
71 auto lock = std::scoped_lock(_mutex);
72 _callbacks.emplace_back(callback_ptr);
81 auto lock = std::scoped_lock(_mutex);
83 ttlet new_end =
std::remove_if(_callbacks.begin(), _callbacks.end(), [&callback_ptr](ttlet &item) {
84 return item.expired() || item.lock() == callback_ptr;
86 _callbacks.erase(new_end, _callbacks.cend());
91 auto lock = std::scoped_lock(_mutex);
94 std::erase_if(_callbacks, [](
auto &x){
return x.expired(); });
104 void operator()(Args
const &...args)
const noexcept requires(std::is_same_v<result_type, void>)
106 auto callbacks_ = callbacks();
107 for (
auto &callback : callbacks_) {
108 if (
auto callback_ = callback.lock()) {
109 (*callback_)(args...);
122 auto callbacks_ = callbacks();
123 for (
auto &callback : callbacks_) {
124 if (
auto callback_ = callback.lock()) {
125 co_yield (*callback_)(args...);
A return value for a generator-function.
Definition coroutine.hpp:25
Definition notifier.hpp:18
callback_ptr_type subscribe(callback_ptr_type const &callback_ptr) noexcept
Add a callback to the notifier.
Definition notifier.hpp:44
void unsubscribe(callback_ptr_type const &callback_ptr) noexcept
Remove a callback from the notifier.
Definition notifier.hpp:79
void operator()(Args const &...args) const noexcept
Call the subscribed callbacks with the given arguments.
Definition notifier.hpp:104
generator< result_type > operator()(Args const &...args) const noexcept
Call the subscribed callbacks with the given arguments.
Definition notifier.hpp:120
callback_ptr_type subscribe(Callback &&callback) noexcept
Add a callback to the notifier.
Definition notifier.hpp:67
An unfair recursive-mutex This is a fast implementation of a recursive-mutex which does not fairly ar...
Definition unfair_recursive_mutex.hpp:32