-
Notifications
You must be signed in to change notification settings - Fork 96
/
Property.h
320 lines (245 loc) · 10.5 KB
/
Property.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#pragma once
#include <string>
#include <bdn/property/GetterSetterBacking.h>
#include <bdn/property/SetterBacking.h>
#include <bdn/property/StreamBacking.h>
#include <bdn/property/TransformBacking.h>
#include <bdn/property/ValueBacking.h>
using namespace std::string_literals;
namespace bdn
{
enum class BindMode
{
unidirectional,
bidirectional
};
template <class ValType> class Property
{
private:
template <typename T> class overloadsArrowOperator
{
template <typename C = T>
static uint8_t _test(int dummy, decltype(&(*((C *)nullptr)->operator->())) pDummy = nullptr);
template <typename C = T> static uint16_t _test(...);
static_assert(sizeof(_test<T>(0)) != 0, "This should never trigger");
public:
enum
{
value = sizeof(_test<T>(0)) == sizeof(uint8_t) ? 1 : 0
};
};
using value_backing_t = ValueBacking<ValType>;
using gs_backing_t = GetterSetterBacking<ValType>;
using setter_backing_t = SetterBacking<ValType>;
using notifier_t = Notifier<Property &>;
public:
using backing_t = Backing<ValType>;
Property() : _backing(std::make_shared<value_backing_t>()) { init(); }
Property(Property &other) : _backing(other.backing()) { init(); }
Property(const Property &) = delete;
~Property()
{
if (_backing) {
_backing->onChange().unsubscribe(_forwardSub);
}
}
Property(ValType value) : _backing(std::make_shared<value_backing_t>())
{
init();
set(value, false /* do not notify on initial set */);
}
Property(const GetterSetterBacking<ValType> &getterSetter)
{
_backing = std::make_shared<gs_backing_t>(getterSetter);
init();
}
Property(const SetterBacking<ValType> &setter)
{
_backing = std::make_shared<setter_backing_t>(setter);
init();
}
Property(const StreamBacking &stream)
{
_backing = std::make_shared<StreamBacking>(stream);
init();
}
template <class U> Property(const TransformBacking<ValType, U> &transform)
{
_backing = std::make_shared<TransformBacking<ValType, U>>(transform);
init();
}
Property(std::shared_ptr<Backing<ValType>> backing)
{
_backing = backing;
init();
}
template <class _Rep, class _Period>
Property(const std::chrono::duration<_Rep, _Period> &duration) : _backing(std::make_shared<value_backing_t>())
{
init();
set(std::chrono::duration_cast<ValType>(duration), false);
}
public:
ValType get() const { return _backing->get(); }
void set(ValType value, bool notify = true) { _backing->set(value, notify); }
const auto backing() const { return _backing; }
public:
template <class OtherType>
void bind(const Property<OtherType> &sourceProperty, BindMode bindMode = BindMode::bidirectional)
{
if ((!ValueBacking<ValType>::template Compare<ValType>::isComparable ||
!ValueBacking<ValType>::template Compare<OtherType>::isComparable) &&
bindMode == BindMode::bidirectional) {
throw std::logic_error(
"You cannot bind this type of Property bidirectionally, its == operator is faked "
"and therefore would end up in an endless loop.");
}
_backing->bind(sourceProperty.backing());
if (bindMode == BindMode::bidirectional) {
sourceProperty.backing()->bind(_backing);
}
}
public:
auto &onChange() const { return _onChange; }
public:
template <typename U = ValType, typename std::enable_if<overloadsArrowOperator<U>::value, int>::type = 0>
const ValType operator->() const
{
return get();
}
template <typename U = ValType, typename std::enable_if<!overloadsArrowOperator<U>::value, int>::type = 0>
const typename backing_t::Proxy operator->() const
{
return _backing->proxy();
}
Property &operator=(const ValType &value)
{
set(value);
return *this;
}
template <class _Rep, class _Period> Property &operator=(const std::chrono::duration<_Rep, _Period> &duration)
{
set(std::chrono::duration_cast<ValType>(duration));
return *this;
}
bool operator==(const ValType &value) const { return get() == value; }
bool operator==(const char *cString) const { return Property<ValType>::operator==(std::string(cString)); }
bool operator!=(const ValType &value) const { return get() != value; }
bool operator!=(const char *cString) const { return Property<ValType>::operator!=(std::string(cString)); }
ValType operator*() const { return get(); }
operator ValType() const { return get(); }
Property &operator=(const Property &otherProperty)
{
if (&otherProperty == this) {
return *this;
}
_backing->set(otherProperty.backing()->get());
return *this;
}
Property operator+() const { return +this->get(); }
Property operator-() const { return -this->get(); }
Property operator+(const Property &otherProperty) const { return this->get() + ValType(otherProperty); }
Property operator-(const Property &otherProperty) const { return this->get() - ValType(otherProperty); }
Property operator*(const Property &otherProperty) const { return this->get() * ValType(otherProperty); }
Property operator/(const Property &otherProperty) const { return this->get() / ValType(otherProperty); }
Property operator%(const Property &otherProperty) const { return this->get() % ValType(otherProperty); }
Property operator~() const { return ~(this->get()); }
Property operator&(const Property &otherProperty) const { return this->get() & ValType(otherProperty); }
Property operator|(const Property &otherProperty) const { return this->get() | ValType(otherProperty); }
Property operator^(const Property &otherProperty) const { return this->get() ^ ValType(otherProperty); }
Property operator<<(const Property &otherProperty) const { return this->get() << ValType(otherProperty); }
Property operator>>(const Property &otherProperty) const { return this->get() >> ValType(otherProperty); }
ValType operator+(const ValType &other) const { return this->get() + other; }
ValType operator-(const ValType &other) const { return this->get() - other; }
ValType operator*(const ValType &other) const { return this->get() * other; }
ValType operator/(const ValType &other) const { return this->get() / other; }
ValType operator%(const ValType &other) const { return this->get() % other; }
ValType operator&(const ValType &other) const { return this->get() & other; }
ValType operator|(const ValType &other) const { return this->get() | other; }
ValType operator^(const ValType &other) const { return this->get() ^ other; }
ValType operator<<(const ValType &other) const { return this->get() << other; }
ValType operator>>(const ValType &other) const { return this->get() >> other; }
Property<ValType> &operator++()
{
set((ValType)this->get() + 1);
return *this;
}
Property<ValType> &operator--()
{
set((ValType)this->get() - 1);
return *this;
}
Property &operator+=(const Property &otherProperty)
{
set(this->get() + ValType(otherProperty));
return *this;
}
Property &operator-=(const Property &otherProperty)
{
set(this->get() - ValType(otherProperty));
return *this;
}
Property &operator*=(const Property &otherProperty)
{
set(this->get() * ValType(otherProperty));
return *this;
}
Property &operator/=(const Property &otherProperty)
{
set(this->get() / ValType(otherProperty));
return *this;
}
Property &operator%=(const Property &otherProperty)
{
set(this->get() % ValType(otherProperty));
return *this;
}
Property &operator&=(const Property &otherProperty)
{
set(this->get() & ValType(otherProperty));
return *this;
}
Property &operator|=(const Property &otherProperty)
{
set(this->get() | ValType(otherProperty));
return *this;
}
Property &operator^=(const Property &otherProperty)
{
set(this->get() ^ ValType(otherProperty));
return *this;
}
Property &operator<<=(const Property &otherProperty)
{
set(this->get() << ValType(otherProperty));
return *this;
}
Property &operator>>=(const Property &otherProperty)
{
set(this->get() >> ValType(otherProperty));
return *this;
}
void forwardNotification() { _onChange.notify(*this); }
private:
void init()
{
_forwardSub = _backing->onChange().subscribe(std::bind(&Property<ValType>::forwardNotification, this));
}
private:
typename backing_t::notifier_t::Subscription _forwardSub;
mutable std::shared_ptr<backing_t> _backing;
mutable bool _isConnected = false;
mutable notifier_t _onChange;
};
template <typename CHAR_TYPE, class CHAR_TRAITS, typename PROP_VALUE>
std::basic_ostream<CHAR_TYPE, CHAR_TRAITS> &operator<<(std::basic_ostream<CHAR_TYPE, CHAR_TRAITS> &stream,
const Property<PROP_VALUE> &s)
{
// note that if there is no << operator for PROP_VALUE then that is not
// a problem. Since this << operator is a template all that does is
// remove the operator from the list of possible overloads. So it would
// be as if there was no << operator for Property<PROP_VALUE>, which is
// exactly what we would want in this case.
return stream << s.get();
}
}