namespace ctw enum access read_only write_only read_write typedef Valu

 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
namespace ctw
{
enum access
{
read_only,
write_only,
read_write
};
//typedef ValueType(*getter_type)();
//typedef void(*setter_type)(ValueType);
template<typename Container, typename ValueType, access AccessLevel>
class property
{
private:
property(){}
};
template <typename Container, typename ValueType>
class property<Container, ValueType, read_only>
{
public:
typedef ValueType(Container::*getter_type)() const;
property(Container* obj, getter_type getter)
: obj_(obj), getter_(getter)
{}
operator ValueType() const{ return (obj_->*getter_)(); }
private:
Container* obj_;
getter_type getter_;
};
template <typename Container, typename ValueType>
class property<Container, ValueType, write_only>
{
public:
typedef void(Container::*setter_type)(const ValueType&);
property(Container* obj, setter_type setter)
: obj_(obj), setter_(setter)
{}
const ValueType& operator=(const ValueType& value)
{
(obj_->*setter_)(value);
return value;
}
private:
Container* obj_;
setter_type setter_;
};
template <typename Container, typename ValueType>
class property<Container, ValueType, read_write>
{
public:
typedef ValueType(Container::*getter_type)() const;
typedef void(Container::*setter_type)(const ValueType&);
property(Container* obj, getter_type getter, setter_type setter)
: obj_(obj), getter_(getter), setter_(setter)
{}
operator ValueType() const{ return (obj_->*getter_)(); }
const ValueType& operator=(const ValueType& value)
{
(obj_->*setter_)(value);
return value;
}
private:
Container* obj_;
getter_type getter_;
setter_type setter_;
};
}