forked from ThePhD/sol2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusertype_automatic_operators.cpp
More file actions
135 lines (112 loc) · 2.71 KB
/
usertype_automatic_operators.cpp
File metadata and controls
135 lines (112 loc) · 2.71 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
#define SOL_ALL_SAFETIES_ON 1
#include <sol/sol.hpp>
#include <list>
#include <iosfwd>
#include <iostream>
class automatic {
private:
std::list<double> data;
public:
using value_type = decltype(data)::value_type;
using iterator = decltype(data)::const_iterator;
using size_type = decltype(data)::size_type;
// automatically bound to obj( args... ) [ __call ]
void operator()() {
data.push_back(
static_cast<value_type>(data.size() + 1) / 3.0);
}
// automatically used for pairs(obj) [ __pairs ], 5.2+
iterator begin() const {
return data.begin();
}
iterator end() const {
return data.end();
}
// automatically bound to #obj [ __len ]
size_type size() const {
return data.size();
}
// automatically bound for obj == obj [ __eq ]
bool operator==(const automatic& right) const {
return data == right.data;
}
// automatically bound for obj < obj [ __lt ]
bool operator<(const automatic& right) const {
return data < right.data;
}
// automatically bound for obj <= obj [ __le ]
bool operator<=(const automatic& right) const {
return data <= right.data;
}
// other comparison operators are based off the above in Lua
// and cannot be overridden directly
};
// automatically bound to tostring(obj) [ __tostring ]
std::ostream& operator<<(
std::ostream& os, const automatic& right) {
if (right.size() == 0) {
os << "{ empty }";
return os;
}
auto b = right.begin();
auto e = right.end();
os << "{ " << right.size() << " | ";
os << *b;
++b;
while (b != e) {
os << ", " << *b;
++b;
}
os << " }";
return os;
}
int main(int, char*[]) {
std::cout << "=== usertype automatic operators ==="
<< std::endl;
sol::state lua;
lua.open_libraries(sol::lib::base);
lua.new_usertype<automatic>("automatic");
lua.script(R"(
obj1 = automatic.new()
obj2 = automatic.new()
obj3 = automatic.new()
print("obj1:", obj1)
print("obj2:", obj2)
print("obj3:", obj2)
print("#obj1:", #obj1)
print("#obj2:", #obj2)
print("#obj3:", #obj3)
obj1() obj1() obj1() obj1() obj1() obj1()
obj2() obj2() obj2()
obj3() obj3() obj3()
print("after modifications using obj() operator")
print("obj1:", obj1)
print("obj2:", obj2)
print("obj3:", obj2)
print("#obj1:", #obj1)
print("#obj2:", #obj2)
print("#obj3:", #obj3)
)");
#if SOL_LUA_VERSION_I_ > 501
lua.script(R"(
for k, v in pairs(obj1) do
assert( (k / 3) == v )
end
)");
#endif
lua.script(R"(
print("obj1 == obj2:", obj1 == obj2)
print("obj1 < obj2:", obj1 < obj2)
print("obj1 >= obj2:", obj1 >= obj2)
assert(obj1 ~= obj2)
assert(obj1 > obj2)
assert(obj1 >= obj2)
print("obj2 == obj3:", obj2 == obj3)
print("obj2 > obj3:", obj2 > obj3)
print("obj2 <= obj3:", obj2 <= obj3)
assert(obj2 == obj3)
assert(obj2 <= obj3)
)");
std::cout << std::endl;
return 0;
}