forked from knizhnik/ptoc
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproperty.h
More file actions
221 lines (177 loc) · 5.72 KB
/
Copy pathproperty.h
File metadata and controls
221 lines (177 loc) · 5.72 KB
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
#ifndef __PROPERTY_H__
#define __PROPERTY_H__
#include <functional>
#include <stdexcept>
class NoGetterSetterError : public std::invalid_argument
{
public:
NoGetterSetterError(const char* message) : invalid_argument(message) {}
};
// This class is used for C++ implementation of Delphi properties of such type
// property PropRec : TRec read FRec write SetRec;
template<typename V>
class Property {
private:
using getter_type = std::function<V()>;
using setter_type = std::function<void(V)>;
getter_type getter_;
setter_type setter_;
public:
Property(getter_type getter, setter_type setter) : getter_(getter), setter_(setter) {}
Property& operator=(const V& value)
{
if (setter_)
setter_(value);
else
throw NoGetterSetterError("Property is read only.");
return *this;
}
operator V () const
{
if(!getter_)
throw NoGetterSetterError("Property cannot be read.");
return getter_();
}
};
// This PropertyArray class is used for C++ implementation of Delphi properties of such type
// property Values[Key: K]: V read GetValue write SetValue;
template<typename K, typename V>
class PropertyArray
{
private:
using getter_type = std::function<V(K)>;
using setter_type = std::function<void(K, V)>;
getter_type getter_;
setter_type setter_;
// proxy class for setter
class WriteProxy
{
private:
PropertyArray& parent;
K key;
public:
WriteProxy(PropertyArray& p, const K& k) : parent(p), key(k) {}
WriteProxy& operator=(const V& new_value)
{
if (!parent.setter_)
throw NoGetterSetterError("Property is read only.");
parent.setter_(key, new_value);
return *this;
}
operator V() const
{
if (!parent.getter_)
throw NoGetterSetterError("Property cannot be read.");
return parent.getter_(key);
}
};
public:
PropertyArray(getter_type getter, setter_type setter) : getter_(getter), setter_(setter) {}
WriteProxy operator[](const K key)
{
return WriteProxy(*this, key);
}
const V operator[](const K key) const
{
if (!getter_)
throw NoGetterSetterError("Property cannot be read.");
return getter_(key);
}
};
// This PropertyArray class is used for C++ implementation of Delphi properties of such type
// property Values[Key1: K1; Key2: K2]: V read GetValue write SetValue;
// OR
// property Values[Key1, Key2: K1]: V read GetValue write SetValue;
template<typename K1, typename K2, typename V>
class PropertyArray2
{
private:
using getter_type = std::function<V(K1,K2)>;
using setter_type = std::function<void(K1,K2,V)>;
using index_type = std::pair<K1, K2>;
getter_type getter_;
setter_type setter_;
// proxy class for setter
class WriteProxy
{
private:
PropertyArray2& parent;
index_type keys;
public:
WriteProxy(PropertyArray2& p, const index_type& ikeys) : parent(p), keys(ikeys) {}
WriteProxy& operator=(const V& new_value)
{
if (!parent.setter_)
throw NoGetterSetterError("Property is read only.");
parent.setter_(keys.first, keys.second, new_value);
return *this;
}
operator V() const
{
if (!parent.getter_)
throw NoGetterSetterError("Property cannot be read.");
return parent.getter_(keys.first, keys.second);
}
};
public:
PropertyArray2(getter_type getter, setter_type setter) : getter_(getter), setter_(setter) {}
WriteProxy operator[](const index_type& ikeys)
{
return WriteProxy(*this, ikeys);
}
const V operator[](const index_type& ikeys) const
{
if (!getter_)
throw NoGetterSetterError("Property cannot be read.");
return getter_(ikeys.first, ikeys.second);
}
};
// This PropertyArray class is used for C++ implementation of Delphi properties of such type
// property Values[Key1:K1; Key2: K2; Key3: K3]: V read GetValue write SetValue;
// OR
// property Values[Key1, Key2, Key3: K1]: V read GetValue write SetValue;
template<typename K1, typename K2, typename K3, typename V>
class PropertyArray3
{
private:
using getter_type = std::function<V(K1, K2, K3)>;
using setter_type = std::function<void(K1, K2, K3, V)>;
using index_type = std::tuple<K1, K2, K3>;
getter_type getter_;
setter_type setter_;
// proxy class for setter
class WriteProxy
{
private:
PropertyArray3& parent;
index_type keys;
public:
WriteProxy(PropertyArray3& p, const index_type& ikeys) : parent(p), keys(ikeys) {}
WriteProxy& operator=(const V& new_value)
{
if (!parent.setter_)
throw NoGetterSetterError("Property is read only.");
parent.setter_(std::get<0>(keys), std::get<1>(keys), std::get<2>(keys), new_value);
return *this;
}
operator V() const
{
if (!parent.getter_)
throw NoGetterSetterError("Property cannot be read.");
return parent.getter_(std::get<0>(keys), std::get<1>(keys), std::get<2>(keys));
}
};
public:
PropertyArray3(getter_type getter, setter_type setter) : getter_(getter), setter_(setter) {}
WriteProxy operator[](const index_type& ikeys)
{
return WriteProxy(*this, ikeys);
}
const V operator[](const index_type& ikeys) const
{
if (!getter_)
throw NoGetterSetterError("Property cannot be read.");
return getter_(std::get<0>(ikeys), std::get<1>(ikeys), std::get<2>(ikeys));
}
};
#endif