25#include <unordered_map>
41 : cancelFunction(std::move(
cancel))
46 : cancelFunction(std::move(other.cancelFunction))
48 other.cancelFunction =
nullptr;
56 this->cancelFunction = std::move(other.cancelFunction);
57 other.cancelFunction =
nullptr;
72 if (this->cancelFunction)
74 auto function = std::move(this->cancelFunction);
75 this->cancelFunction =
nullptr;
82 return this->cancelFunction !=
nullptr;
86 std::function<void()> cancelFunction;
105 : state(std::make_shared<State>())
110 : state(std::make_shared<State>())
112 this->state->value = std::move(value);
122 return this->state->value;
125 operator const T&()
const
127 return this->state->value;
132 if constexpr (std::is_invocable_r_v<bool, std::equal_to<>,
const T&,
const T&>)
134 if (this->state->value == value)
137 this->state->value = std::move(value);
143 this->
set(std::move(value));
152 std::shared_ptr<State> keep = this->state;
153 std::vector<std::pair<size_t, Subscriber>> subscribers = keep->subscribers;
154 for (
auto& [
id, subscriber] : subscribers)
156 if (keep->contains(
id))
157 subscriber(keep->value);
165 size_t id = ++this->state->lastId;
166 this->state->subscribers.emplace_back(
id, std::move(subscriber));
167 std::weak_ptr<State> weak = this->state;
170 if (
auto state = weak.lock())
171 state->remove(
id); });
176 return this->state->subscribers.size();
184 std::vector<std::pair<size_t, Subscriber>> subscribers;
186 bool contains(
size_t id)
const
188 for (
auto& entry : this->subscribers)
189 if (entry.first == id)
194 void remove(
size_t id)
196 for (
auto it = this->subscribers.begin(); it != this->subscribers.end(); ++it)
200 this->subscribers.erase(it);
207 std::shared_ptr<State> state;
224 template <
typename T>
227 this->entries.insert_or_assign(key, Entry { std::type_index(
typeid(T)), &observable });
232 this->entries.erase(key);
236 template <
typename T>
239 auto it = this->entries.find(key);
240 if (it == this->entries.end() || it->second.type != std::type_index(
typeid(T)))
245 bool has(
const std::string& key)
const
247 return this->entries.count(key) > 0;
253 std::type_index type;
257 std::unordered_map<std::string, Entry> entries;
Definition observable.hpp:222
void add(const std::string &key, Observable< T > &observable)
Definition observable.hpp:225
Observable< T > * get(const std::string &key) const
Definition observable.hpp:237
void remove(const std::string &key)
Definition observable.hpp:230
bool has(const std::string &key) const
Definition observable.hpp:245
Definition observable.hpp:100
Observable(const Observable &)=delete
Observable & operator=(const Observable &)=delete
Observable(T value)
Definition observable.hpp:109
void notify()
Definition observable.hpp:149
Observable()
Definition observable.hpp:104
Subscription subscribe(Subscriber subscriber)
Definition observable.hpp:163
std::function< void(const T &)> Subscriber
Definition observable.hpp:102
size_t getSubscriberCount() const
Definition observable.hpp:174
const T & get() const
Definition observable.hpp:120
void set(T value)
Definition observable.hpp:130
Observable & operator=(T value)
Definition observable.hpp:141
Definition observable.hpp:37
Subscription(const Subscription &)=delete
void cancel()
Definition observable.hpp:70
Subscription(std::function< void()> cancel)
Definition observable.hpp:40
Subscription & operator=(Subscription &&other) noexcept
Definition observable.hpp:51
~Subscription()
Definition observable.hpp:65
bool active() const
Definition observable.hpp:80
Subscription & operator=(const Subscription &)=delete
Subscription(Subscription &&other) noexcept
Definition observable.hpp:45
Definition actions.hpp:25