ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
Bindable.hpp
Go to the documentation of this file.
1#pragma once
2
27
28#include "Common.hpp"
29
30namespace ZenitUI
31{
35
42 template <typename T>
43 class Bindable
44 {
45 public:
55 class Subscription
56 {
57 public:
58 Subscription() = default;
59
60 ~Subscription()
61 {
63 }
64
65 Subscription(Subscription &&o) noexcept
66 : ctrl_(std::move(o.ctrl_)), token_(o.token_)
67 {
68 o.ctrl_.reset();
69 o.token_ = 0;
70 }
71
72 Subscription &operator=(Subscription &&o) noexcept
73 {
74 if (this != &o)
75 {
77 ctrl_ = std::move(o.ctrl_);
78 token_ = o.token_;
79 o.ctrl_.reset();
80 o.token_ = 0;
81 }
82 return *this;
83 }
84
85 Subscription(const Subscription &) = delete;
86 Subscription &operator=(const Subscription &) = delete;
87
90 {
91 if (ctrl_ && ctrl_->alive_ && ctrl_->owner_ && token_ != 0)
92 ctrl_->owner_->unsubscribe_(token_);
93 ctrl_.reset();
94 token_ = 0;
95 }
96
98 bool active() const
99 {
100 return ctrl_ && ctrl_->alive_ && ctrl_->owner_ && token_ != 0;
101 }
102
103 private:
104 friend class Bindable;
105
115 struct ControlBlock
116 {
117 Bindable *owner_{nullptr};
118 bool alive_{true};
119 };
120
121 Subscription(std::shared_ptr<ControlBlock> ctrl, uint64_t token)
122 : ctrl_(std::move(ctrl)), token_(token) {}
123
124 std::shared_ptr<ControlBlock> ctrl_;
125 uint64_t token_{0};
126 };
127
128 Bindable() = default;
129
131 explicit Bindable(T initial)
132 : value_(std::move(initial)) {}
133
134 ~Bindable()
135 {
136 if (ctrl_)
137 ctrl_->alive_ = false;
138 }
139
140 Bindable(const Bindable &) = delete;
141 Bindable &operator=(const Bindable &) = delete;
142 Bindable(Bindable &&) = delete;
143 Bindable &operator=(Bindable &&) = delete;
144
146 const T &get() const { return value_; }
147
155 void set(T v)
156 {
157 if (v == value_)
158 return;
159 value_ = std::move(v);
160
161 auto snapshot = subscribers_;
162 for (const auto &entry : snapshot)
163 {
164 if (isStillSubscribed(entry.token))
165 entry.cb(value_);
166 }
167 }
168
176 Subscription onChanged(std::function<void(const T &)> cb,
177 bool fireImmediately = false)
178 {
179 if (!ctrl_)
180 ctrl_ = std::make_shared<typename Subscription::ControlBlock>();
181 ctrl_->owner_ = this;
182
183 uint64_t token = nextToken_++;
184 subscribers_.push_back({token, std::move(cb)});
185
186 Subscription sub(ctrl_, token);
187
188 if (fireImmediately)
189 {
190 if (auto it = findEntry(token))
191 it->cb(value_);
192 }
193
194 return sub;
195 }
196
210 template <typename W, typename Arg>
211 Subscription bindTo(std::weak_ptr<W> widget,
212 void (W::*method)(Arg),
213 bool fireImmediately = false)
214 {
215 return onChanged(
216 [widget, method](const T &val)
217 {
218 if (auto w = widget.lock())
219 (w.get()->*method)(val);
220 },
221 fireImmediately);
222 }
223
225 template <typename W, typename Arg>
226 Subscription bindTo(std::shared_ptr<W> widget,
227 void (W::*method)(Arg),
228 bool fireImmediately = false)
229 {
230 return bindTo(std::weak_ptr<W>(widget), method, fireImmediately);
231 }
232
233 private:
234 struct Entry
235 {
236 uint64_t token;
237 std::function<void(const T &)> cb;
238 };
239
240 std::vector<Entry> subscribers_;
241 std::shared_ptr<typename Subscription::ControlBlock> ctrl_;
242 uint64_t nextToken_{1};
243 T value_{};
244
246 void unsubscribe_(uint64_t token)
247 {
248 subscribers_.erase(
249 std::remove_if(
250 subscribers_.begin(), subscribers_.end(),
251 [token](const Entry &e)
252 { return e.token == token; }),
253 subscribers_.end());
254 }
255
257 bool isStillSubscribed(uint64_t token) const
258 {
259 for (const auto &e : subscribers_)
260 if (e.token == token)
261 return true;
262 return false;
263 }
264
266 Entry *findEntry(uint64_t token)
267 {
268 for (auto &e : subscribers_)
269 if (e.token == token)
270 return &e;
271 return nullptr;
272 }
273 };
274
275} // namespace ZenitUI
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