58 Subscription() =
default;
65 Subscription(Subscription &&o) noexcept
66 : ctrl_(std::move(o.ctrl_)), token_(o.token_)
72 Subscription &operator=(Subscription &&o)
noexcept
77 ctrl_ = std::move(o.ctrl_);
85 Subscription(
const Subscription &) =
delete;
86 Subscription &operator=(
const Subscription &) =
delete;
91 if (ctrl_ && ctrl_->alive_ && ctrl_->owner_ && token_ != 0)
92 ctrl_->owner_->unsubscribe_(token_);
100 return ctrl_ && ctrl_->alive_ && ctrl_->owner_ && token_ != 0;
104 friend class Bindable;
117 Bindable *owner_{
nullptr};
121 Subscription(std::shared_ptr<ControlBlock> ctrl, uint64_t token)
122 : ctrl_(std::move(ctrl)), token_(token) {}
124 std::shared_ptr<ControlBlock> ctrl_;
128 Bindable() =
default;
132 : value_(std::move(initial)) {}
137 ctrl_->alive_ =
false;
140 Bindable(
const Bindable &) =
delete;
141 Bindable &operator=(
const Bindable &) =
delete;
142 Bindable(Bindable &&) =
delete;
143 Bindable &operator=(Bindable &&) =
delete;
146 const T &
get()
const {
return value_; }
159 value_ = std::move(v);
161 auto snapshot = subscribers_;
162 for (
const auto &entry : snapshot)
164 if (isStillSubscribed(entry.token))
177 bool fireImmediately =
false)
180 ctrl_ = std::make_shared<typename Subscription::ControlBlock>();
181 ctrl_->owner_ =
this;
183 uint64_t token = nextToken_++;
184 subscribers_.push_back({token, std::move(cb)});
190 if (
auto it = findEntry(token))
210 template <
typename W,
typename Arg>
212 void (W::*method)(Arg),
213 bool fireImmediately =
false)
216 [widget, method](
const T &val)
218 if (
auto w = widget.lock())
219 (w.get()->*method)(val);
225 template <
typename W,
typename Arg>
227 void (W::*method)(Arg),
228 bool fireImmediately =
false)
230 return bindTo(std::weak_ptr<W>(widget), method, fireImmediately);
237 std::function<void(
const T &)> cb;
240 std::vector<Entry> subscribers_;
241 std::shared_ptr<typename Subscription::ControlBlock> ctrl_;
242 uint64_t nextToken_{1};
246 void unsubscribe_(uint64_t token)
250 subscribers_.begin(), subscribers_.end(),
251 [token](
const Entry &e)
252 { return e.token == token; }),
257 bool isStillSubscribed(uint64_t token)
const
259 for (
const auto &e : subscribers_)
260 if (e.token == token)
266 Entry *findEntry(uint64_t token)
268 for (
auto &e : subscribers_)
269 if (e.token == token)
Centralized standard library includes used throughout ZenitUI.
RAII handle to an onChanged registration.
Definition Bindable.hpp:56
void unsubscribe()
Remove the callback from the owning Bindable.
Definition Bindable.hpp:89
bool active() const
true while the underlying callback is still registered.
Definition Bindable.hpp:98
Observable single-value container.
Definition Bindable.hpp:44
const T & get() const
Read the current value.
Definition Bindable.hpp:146
Subscription bindTo(std::shared_ptr< W > widget, void(W::*method)(Arg), bool fireImmediately=false)
Overload for shared_ptr widgets; forwards to the weak-pointer version.
Definition Bindable.hpp:226
Bindable(T initial)
Construct with an initial value.
Definition Bindable.hpp:131
Subscription onChanged(std::function< void(const T &)> cb, bool fireImmediately=false)
Register a callback fired on value changes.
Definition Bindable.hpp:176
void set(T v)
Replace the value and notify subscribers if it changed.
Definition Bindable.hpp:155
Subscription bindTo(std::weak_ptr< W > widget, void(W::*method)(Arg), bool fireImmediately=false)
Bind this value to a widget method.
Definition Bindable.hpp:211