-
Notifications
You must be signed in to change notification settings - Fork 1
/
property_main.cc
110 lines (75 loc) · 3.14 KB
/
property_main.cc
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
//
// Created by oyoung on 19-2-15.
//
#include <oyoung/property.hpp>
#include <iostream>
class Person {
public:
oyoung::property<int> age{1};
oyoung::property<std::string> gender{"Female"};
oyoung::property<std::string> name {};
oyoung::property<std::string> full_name {};
oyoung::property<int> nextAge{};
Person() {
age.willSet([=] (int newVal) {
return newVal > age;
});
age.didSet([=](int val, int old) {
std::cout << "Age changed: ( current: " << val << ", old: " << old << ")" << std::endl;
});
name.bind(_name);
nextAge.computed([=] {
return age + 1;
});
full_name.computed([=] {
return _name + ".D.Luffy";
});
}
std::string& internal_name() { return _name; }
private:
std::string _name {"Noname"};
};
int main(int, char**)
{
Person person;
std::cout << std::boolalpha;
std::cout << person.internal_name() << ", gender: " << person.gender << ", age: " << person.age << std::endl;
/// assign property stored value
person.age = 26;
person.gender = "Male";
/// assign bind member _name;
person.name = "oyoung";
std::cout << person.internal_name() << ", gender: " << person.gender << ", age: " << person.age << std::endl;
/// assign age will be reject, because 22 less than the old age 26
person.age = 22;
std::cout << person.internal_name() << ", gender: " << person.gender << ", age: " << person.age << std::endl;
person.age += 3;
std::cout << person.nextAge << std::endl;
int next = person.nextAge;
std::cout << next << std::endl;
try {
person.nextAge = 100;
} catch (const std::exception& e) {
std::cout << "computed property nextAge= except: " << e.what() << std::endl;
}
try {
person.full_name->c_str();
} catch (const std::exception & e) {
std::cout << "computed property fullName-> except: " << e.what() << std::endl;
}
person.name->append("123");
std::cout << "after append, person's name is " << person.name << std::endl;
std::cout << "person.age is between 0 and 100 ? " << (0 < person.age and person.age < 100) << std::endl;
std::cout << "person.age is greater than 100 or equal to 0/100 or less than 0 ? " << (100 <= person.age or person.age <= 0) << std::endl;
std::cout << "person.age == 29 ? " << (person.age == 29) << std::endl;
std::cout << "person.age != 29 ? " << (person.age != 29) << std::endl;
std::cout << "30 == person.age ? " << (30 == person.age) << std::endl;
std::cout << "30 != person.age ? " << (30 != person.age) << std::endl;
std::cout << "before 5 years, " << person.name << "'s age was " << (person.age - 5) << std::endl;
std::cout << "after 5 years, " << person.name << "'s age will be " << (person.age + 5) << std::endl;
std::cout << "person.age * 5: " << (person.age * 5) << std::endl;
std::cout << "person.age / 5: " << (person.age / 5) << std::endl;
std::cout << "person.age << 1: " << (person.age << 1) << std::endl;
std::cout << "person.age >> 1: " << (person.age >> 1) << std::endl;
return 0;
}