/
Backing.h
59 lines (45 loc) · 1.77 KB
/
Backing.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
#pragma once
#include <bdn/SimpleNotifier.h>
#include <bdn/property/IValueAccessor.h>
#include <memory>
namespace bdn
{
template <class ValType> class Property;
template <class ValType> class Backing : virtual public IValueAccessor<ValType>
{
public:
class Proxy
{
public:
Proxy(ValType value) : _value(value) {}
const ValType *operator->() const { return &_value; }
private:
ValType _value;
};
using value_accessor_t = IValueAccessor<ValType>;
using value_accessor_t_ptr = std::shared_ptr<value_accessor_t>;
using notifier_t = SimpleNotifier<std::shared_ptr<value_accessor_t>>;
using notifier_t_ptr = std::shared_ptr<notifier_t>;
using property_t = Property<ValType>;
using property_t_ptr = std::shared_ptr<property_t>;
public:
Backing() : _pOnChange(std::make_shared<notifier_t>()) {}
std::shared_ptr<Backing<ValType>> shared_from_this()
{
return std::dynamic_pointer_cast<Backing<ValType>>(IValueAccessor<ValType>::shared_from_this());
}
virtual ValType get() const = 0;
virtual void set(const ValType &value, bool notify = true) = 0;
virtual Proxy proxy() const { return Proxy(get()); }
virtual notifier_t &onChange() const { return *(_pOnChange.get()); }
virtual void bind(std::shared_ptr<Backing<ValType>> sourceBacking)
{
sourceBacking->onChange() += weakMethod(shared_from_this(), &Backing::bindSourceChanged);
bindSourceChanged(sourceBacking);
}
public:
void bindSourceChanged(value_accessor_t_ptr pValue) { set(pValue->get()); }
protected:
mutable notifier_t_ptr _pOnChange;
};
}