Photon microGUI widgets library 0.6.0
property.hpp
1#ifndef CPP_PROPERTY_HPP
2#define CPP_PROPERTY_HPP
3
4namespace cppproperties
5{
6 namespace detail
7 {
8 struct property_flag
9 {
10 enum e_property_flag
11 {
12 ro,
13 wo,
14 rw
15 };
16 };
17
18 template<typename T>
19 struct remove_reference
20 {
21 typedef T type;
22 };
23
24 template<typename T>
25 struct remove_reference<T&>
26 {
27 typedef T type;
28 };
29
30 template<typename T>
31 struct remove_const
32 {
33 typedef T type;
34 };
35
36 template<typename T>
37 struct remove_const<const T>
38 {
39 typedef T type;
40 };
41
42
43 template <typename ValueT, typename CValueT>
44 struct flag_chooser_helper
45 {
46 static const property_flag::e_property_flag flag = property_flag::rw;
47 };
48
49 template <typename ValueT>
50 struct flag_chooser_helper<ValueT, ValueT>
51 {
52 static const property_flag::e_property_flag flag = property_flag::ro;
53 };
54
55 template <typename ValueT>
56 struct flag_chooser_helper<ValueT, void>
57 {
58 static const property_flag::e_property_flag flag = property_flag::wo;
59 };
60
61 template <>
62 struct flag_chooser_helper<void, void>
63 {
64 };
65
66 template<class T1>
67 struct flag_chooser :
68 public flag_chooser_helper<T1, const T1>
69 {
70 };
71
72 template<class T1>
73 struct flag_chooser<T1&> :
74 public flag_chooser_helper<T1, const T1>
75 {
76 };
77
78 struct Void {};
79
80 template<typename ValueT, typename ParentT>
81 struct get_parent_func
82 {
83 typedef ValueT value_t;
84 typedef ParentT parent_t;
85 typedef value_t(parent_t::* getter_t)()const;
86 typedef void(parent_t::* setter_t)(value_t);
87
88 static const getter_t &default_getter() { static getter_t getter = getter_t(0); return getter;}
89 static const setter_t &default_setter() { static setter_t setter = setter_t(0); return setter;}
90 };
91
92 template<>
93 struct get_parent_func<void, void>
94 {
95 typedef void value_t;
96 typedef Void parent_t;
97 typedef const int getter_t;
98 typedef const int setter_t;
99
100 static const getter_t &default_getter() { static getter_t getter = 0; return getter;}
101 static const setter_t &default_setter() { static setter_t setter = 0; return setter;}
102 };
103
104 template<typename ValueT>
105 struct get_parent_func<ValueT, void>:
106 public get_parent_func<void, void>
107 {
108 };
109
110 template<typename ParentT>
111 struct get_parent_func<void, ParentT> :
112 public get_parent_func<void, void>
113 {
114 };
115 }
116
117
118 template<class ValueT = void,
119 const detail::property_flag::e_property_flag Flag = (const detail::property_flag::e_property_flag)(detail::flag_chooser<ValueT>::flag)>
120 class property{};
121
122 //property<void>:
123 template<>
124 class property<void, detail::property_flag::ro>:
125 public detail::property_flag
126 {
127 public:
128 typedef detail::property_flag flags;
129 };
130
131 template<>
132 class property<void, detail::property_flag::wo>:
133 public detail::property_flag
134 {
135 public:
136 typedef detail::property_flag flags;
137 };
138
139 template<>
140 class property<void, detail::property_flag::rw>:
141 public detail::property_flag
142 {
143 public:
144 typedef detail::property_flag flags;
145 };
146
147 //property<Value>:
148 template<typename ValueT>
149 class property<ValueT, detail::property_flag::ro>//ValueT == const...
150 {
151 typedef typename detail::remove_const<typename detail::remove_reference<ValueT>::type>::type value_t;
152
153 public:
154 template<class ParentT, typename detail::get_parent_func<ValueT, ParentT>::getter_t Getter>
155 class bind
156 {
157 typedef typename detail::get_parent_func<ValueT, ParentT>::value_t value_t;
158
159 public:
160 bind(ParentT *parent) :
161 _obj(parent)
162 {}
163
164 inline value_t get() const
165 {
166 return (_obj->*Getter)();
167 }
168
169 inline operator value_t() const { return get(); }
170
171 inline value_t operator()(void) const { return get(); }
172
173 private:
174 ParentT *_obj;
175
176 bind(const bind &rhs);
177
178 inline bind &operator=(typename detail::remove_const<typename detail::remove_reference<ValueT>::type>::type const &);
179 inline bind &operator=(bind const &);
180 };
181
182 property(value_t const &value) :
183 _val(value)
184 {}
185
186 inline const value_t &get() const
187 {
188 return _val;
189 }
190
191 inline operator const value_t&() const { return get(); }
192
193 inline const value_t& operator()(void) const { return get(); }
194
195 private:
196
197 //inline operator value_t&();
198
199 const ValueT _val;
200
201 inline property &operator=(value_t const &);
202 inline property &operator=(property const &);
203 };
204
205
206 template<typename ValueT>
207 class property<ValueT, detail::property_flag::rw>//ValueT != const...
208 {
209 typedef typename detail::remove_reference<ValueT>::type value_t;
210
211 public:
212
213 template<class ParentT, typename detail::get_parent_func<ValueT, ParentT>::getter_t Getter, typename detail::get_parent_func<ValueT, ParentT>::setter_t Setter>
214 class bind
215 {
216 typedef typename detail::get_parent_func<ValueT, ParentT>::value_t value_t;
217
218 public:
219 bind(ParentT *parent) :
220 _obj(parent)
221 {}
222
223 inline void set(value_t value)
224 {
225 (_obj->*Setter)(value);
226 }
227
228 inline value_t get() const
229 {
230 return (_obj->*Getter)();
231 }
232
233 inline operator value_t() const { return get(); }
234
235 inline bind &operator=(bind const &) {return *this;}
236 inline bind &operator=(value_t value) { set(value); return *this; }
237
238 inline value_t operator()(void) const { return get(); }
239
240 inline void operator()(value_t value) { set(value); return *this; }
241
242
243 private:
244 ParentT *_obj;
245
246 bind(const bind &rhs);
247 };
248
249 property()
250 {}
251
252 property(ValueT value) :
253 _val(value)
254 {}
255
256 inline void set(value_t const &value)
257 {
258 _val = value;
259 }
260
261 inline value_t &get() const
262 {
263 return _val;
264 }
265
266 inline property &operator=(value_t const &value) { set(value); return *this; }
267
268 inline operator value_t&() const { return get(); }
269
270 inline value_t operator()(void) const { return get(); }
271
272 inline void operator()(value_t const &value) { set(value); }
273
274 private:
275
276 ValueT _val;
277
278 };
279
280 template<typename ValueT>
281 class property<ValueT, detail::property_flag::wo>//ValueT != const...
282 {
283 typedef typename detail::remove_reference<ValueT>::type value_t;
284
285 public:
286
287 template<class ParentT, typename detail::get_parent_func<ValueT, ParentT>::setter_t Setter>
288 class bind
289 {
290 typedef typename detail::get_parent_func<ValueT, ParentT>::value_t value_t;
291
292 public:
293 bind(ParentT *parent) :
294 _obj(parent)
295 {}
296
297 inline void set(value_t value)
298 {
299 (_obj->*Setter)(value);
300 }
301
302 inline bind &operator=(bind const &) {return *this;}
303 inline bind &operator=(value_t value) { set(value); return *this; }
304
305 inline void operator()(value_t value) { set(value); return *this; }
306 private:
307 ParentT *_obj;
308
309 bind(const bind &rhs);
310 };
311
312 property()
313 {}
314
315 property(ValueT value) :
316 _val(value)
317 {}
318
319 inline void set(value_t const &value)
320 {
321 _val = value;
322 }
323
324 inline property &operator=(value_t const &value) { set(value); return *this; }
325
326 inline void operator()(value_t const &value) { set(value); }
327 private:
328
329 ValueT _val;
330 };
331
332
333 /*template<typename ValueT, typename ParentT = void, typename detail::get_parent_func<ValueT, ParentT>::getter_t Getter = 0, typename detail::get_parent_func<ValueT, ParentT>::setter_t Setter = 0,
334 const detail::property_flag::e_property_flag Flag = (const detail::property_flag::e_property_flag)(detail::flag_chooser<ValueT>::flag)
335 >
336
337 class property:
338 public detail::property_base<ValueT, ParentT, detail::property_flag::e_property_flag(Flag)>
339 {
340 public:
341
342 property(ParentT *parent) :
343 detail::property_base<ValueT, ParentT, Flag>(parent, Getter, Setter)
344 {}
345
346 private:
347
348 property(const property &rhs);
349 };
350
351
352 //property<void>:
353 template<detail::property_flag::e_property_flag Flag>
354 class property<void, void, 0, 0, Flag>
355 {
356 public:
357 typedef detail::property_flag flags;
358 };
359
360
361 //property<Value>:
362 template<typename ValueT>
363 class property<ValueT, void, 0, 0, detail::property_flag::r>//ValueT == const...
364 {
365 public:
366
367 property(ValueT const &value) :
368 _val(value)
369 {}
370
371 inline const ValueT &get() const
372 {
373 return _val;
374 }
375
376 operator const ValueT&() const { return get(); }
377
378 private:
379
380 const ValueT _val;
381
382 property &operator=(ValueT const &);
383 };
384
385
386 template<typename ValueT>
387 class property<ValueT, void, 0, 0, detail::property_flag::rw>//ValueT != const...
388 {
389 public:
390
391 property()
392 {}
393
394 property(const ValueT &value) :
395 _val(value)
396 {}
397
398 inline void set(ValueT const &value)
399 {
400 _val = value;
401 }
402
403 inline const ValueT &get() const
404 {
405 return _val;
406 }
407
408 property &operator=(ValueT const &value) { set(value); return *this; }
409
410 operator const ValueT&() const { return get(); }
411
412 private:
413
414 ValueT _val;
415
416 };
417
418 template<typename ValueT>
419 class property<ValueT, void, 0, 0, detail::property_flag::w>//ValueT != const...
420 {
421 public:
422
423 property()
424 {}
425
426 property(const ValueT &value) :
427 _val(value)
428 {}
429
430 inline void set(ValueT const &value)
431 {
432 _val = value;
433 }
434
435 property &operator=(ValueT const &value) { set(value); return *this; }
436
437 private:
438
439 ValueT _val;
440 };
441
442
443 //property<Value, ParentT>:
444 template<typename ValueT, typename ParentT>
445 class property<ValueT, ParentT, detail::get_parent_func<void, void>::default_getter(), detail::get_parent_func<void, void>::default_setter(), detail::property_flag::w>//ValueT == const...
446 {
447 public:
448
449 property(ParentT *parent, ValueT(ParentT::*getter)()const) :
450 _obj(parent),
451 _getter(getter)
452 {}
453
454 inline ValueT get() const
455 {
456 return (_obj->*_getter)();
457 }
458
459 operator ValueT() const { return get(); }
460
461 private:
462 ParentT *_obj;
463 ValueT(ParentT::*_getter)()const;
464
465 property &operator=(ValueT const &);
466 };
467
468 template<typename ValueT, typename ParentT, typename ValueT(ParentT::*Getter)()const, typename void(ParentT::*Setter)(ValueT const &), detail::e_get_set_state Active>
469 class property<ValueT, ParentT, Getter, Setter, detail::property_flag::rw, Active>//ValueT != const...
470 {
471 public:
472
473 property(ParentT *parent, const ValueT(ParentT::*getter)()const, void(ParentT::*setter)(ValueT const &), ValueT const & value) :
474 _obj(parent),
475 _getter(getter),
476 _setter(setter)
477 {
478 set(value);
479 }
480
481 property(ParentT *parent, const ValueT(ParentT::*getter)()const, void(ParentT::*setter)(ValueT const &)) :
482 _obj(parent),
483 _getter(getter),
484 _setter(setter)
485 {
486 }
487
488 inline void set(ValueT const & value)
489 {
490 (_obj->*_setter)(value);
491 }
492
493 inline const ValueT &get() const
494 {
495 return (_obj->*_getter)();
496 }
497
498 property &operator=(ValueT const & value) { set(value); return *this; }
499 private:
500
501 ParentT *_obj;
502 const ValueT(ParentT::*_getter)()const;
503 void(ParentT::*_setter)(ValueT const &);
504
505 };
506
507
508 //property<Value, ParentT, ValueT(ParentT::*Getter)()>:
509 template<typename ValueT, typename ParentT, typename detail::get_parent_func<ValueT, ParentT>::getter_t Getter, detail::property_flag::e_property_flag Flag>
510 class property<ValueT, ParentT, Getter, 0, Flag> //typename detail::get_parent_func<typename ValueT, typename ParentT>::default_setter()
511 {
512 public:
513
514 property(ParentT *parent) :
515 _obj(parent)
516 {}
517
518 inline ValueT get() const
519 {
520 return (_obj->*Getter)();
521 }
522
523 operator ValueT() const { return get(); }
524
525 private:
526
527 property &operator=(ValueT const &);
528 };
529
530 //property<Value, ParentT, 0, ValueT(ParentT::*Setter)(ValueT const &)>:
531 template<typename ValueT, typename ParentT, typename void(ParentT::*Setter)(ValueT const &), detail::e_get_set_state Active>
532 class property<ValueT, ParentT, 0, Setter, detail::property_flag::r, Active>//ValueT == const...
533 {
534 property();//Invalid class: value is const but no Getter is provided!
535
536 property(const property &rhs);
537 };
538
539 template<typename ValueT, typename ParentT, typename void(ParentT::*Setter)(ValueT const &), detail::property_flag::e_property_flag Flag, detail::e_get_set_state Active>
540 class property<ValueT, ParentT, 0, Setter, Flag, Active>//ValueT != const...
541 {
542 public:
543
544 property()
545 {}
546
547 property(ValueT const & value)
548 {
549 set(value);
550 }
551
552 ValueT set(ValueT const & value)
553 {
554 return (_obj->*Setter)(value);
555 }
556
557 property &operator=(ValueT const & value) { set(value); return *this; }
558
559 private:
560 ParentT *_obj;
561
562 property(const property &rhs);
563
564 };
565 */
566
567}
568
569#endif