-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProperty.hpp
122 lines (97 loc) · 2.1 KB
/
Property.hpp
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
# pragma once
template <typename T>
class propertyBase
{
public:
T dummy = T();
T& obj = dummy;
propertyBase(T& value):
obj(value)
{}
};
// Read/Write specialization
template <typename T, bool canWrite = true, bool isPod = true>
class Property : private propertyBase<T>
{
public:
Property() = default;
Property(const Property<T, true, true>& value):
propertyBase<T>(value.obj)
{}
Property(Property<T, true, true>& value):
propertyBase<T>(value.obj)
{}
Property(T& value):
propertyBase<T>(value)
{}
T& operator=(const Property<T, true, true>& value)
{
this->obj = value.obj;
return this->obj;
}
T& operator=(const T& value)
{
this->obj = value;
return this->obj;
}
T& operator=(T& value)
{
this->obj = value;
return this->obj;
}
operator T&() const
{
return this->obj;
}
};
// Read only specialization
template <typename T>
class Property<T, false, true> : private propertyBase<T>
{
public:
Property() = default;
Property(const Property<T, false, true>& value):
propertyBase<T>(value.obj)
{}
Property(T& value):
propertyBase<T>(value)
{}
T& operator=(const Property& value)
{
this->obj = value.obj;
return this->obj;
}
operator T() const
{
return this->obj;
}
};
// Read/Write non-pod specialization
template <typename T>
class Property<T, true, false> : public T
{
public:
using T::T;
Property(const T& value):
T(value)
{}
};
// Read only non-pod specialization
template <typename T>
class Property<T, false, false> : public T
{
const T* const _obj = this;
public:
using T::T;
Property(const T& value):
T(value)
{}
const T* const operator->() const
{
return this->_obj;
}
};
template <typename T>
using roProperty = Property<T, false, std::is_pod<T>::value>;
template <typename T>
using rwProperty = Property<T, true, std::is_pod<T>::value>;