forked from ThePhD/sol2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptional_with_iteration.cpp
More file actions
98 lines (85 loc) · 2.36 KB
/
optional_with_iteration.cpp
File metadata and controls
98 lines (85 loc) · 2.36 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
#define SOL_ALL_SAFETIES_ON 1
#include <sol/sol.hpp>
#include <string>
#include <memory>
#include <iostream>
struct thing {
int a = 20;
thing() = default;
thing(int a) : a(a) {
}
};
struct super_thing : thing {
int b = 40;
};
struct unrelated { };
int main(int, char*[]) {
std::cout << "=== optional with iteration ===" << std::endl;
sol::state lua;
// Comment out the new_usertype call
// to prevent derived class "super_thing"
// from being picked up and cast to its base
// class
lua.new_usertype<super_thing>(
"super_thing", sol::base_classes, sol::bases<thing>());
// Make a few things
lua["t1"] = thing {};
lua["t2"] = super_thing {};
lua["t3"] = unrelated {};
// And a table
lua["container"] = lua.create_table_with(
0, thing { 50 }, 1, unrelated {}, 4, super_thing {});
std::vector<std::reference_wrapper<thing>> things;
// Our recursive function
// We use some lambda techniques and pass the function
// itself itself so we can recurse, but a regular function
// would work too!
auto fx = [&things](auto& f, auto& tbl) -> void {
// You can iterate through a table: it has
// begin() and end()
// like standard containers
for (auto key_value_pair : tbl) {
// Note that iterators are extremely frail
// and should not be used outside of
// well-constructed for loops
// that use pre-increment ++,
// or C++ ranged-for loops
const sol::object& key = key_value_pair.first;
const sol::object& value = key_value_pair.second;
sol::type t = value.get_type();
switch (t) {
case sol::type::table: {
sol::table inner = value.as<sol::table>();
f(f, inner);
} break;
case sol::type::userdata: {
// This allows us to check if a userdata is
// a specific class type
sol::optional<thing&> maybe_thing
= value.as<sol::optional<thing&>>();
if (maybe_thing) {
thing& the_thing = maybe_thing.value();
if (key.is<std::string>()) {
std::cout << "key "
<< key.as<std::string>()
<< " is a thing -- ";
}
else if (key.is<int>()) {
std::cout << "key "
<< key.as<int>()
<< " is a thing -- ";
}
std::cout << "thing.a ==" << the_thing.a
<< std::endl;
things.push_back(the_thing);
}
} break;
default:
break;
}
}
};
fx(fx, lua);
std::cout << std::endl;
return 0;
}