2019-05-22 07:17:31 +08:00
|
|
|
#define SOL_ALL_SAFETIES_ON 1
|
2018-09-28 13:27:38 +08:00
|
|
|
#include <sol/sol.hpp>
|
2017-04-03 04:10:00 +08:00
|
|
|
|
2017-03-20 06:06:16 +08:00
|
|
|
#include <string>
|
|
|
|
#include <memory>
|
|
|
|
#include <iostream>
|
|
|
|
|
2019-08-15 13:26:52 +08:00
|
|
|
struct thing {
|
|
|
|
int a = 20;
|
2017-03-20 06:06:16 +08:00
|
|
|
|
2019-08-15 13:26:52 +08:00
|
|
|
thing() = default;
|
|
|
|
thing(int a) : a(a) {
|
|
|
|
}
|
|
|
|
};
|
2017-03-20 06:06:16 +08:00
|
|
|
|
2019-08-15 13:26:52 +08:00
|
|
|
struct super_thing : thing {
|
|
|
|
int b = 40;
|
|
|
|
};
|
2017-03-20 06:06:16 +08:00
|
|
|
|
2021-03-06 14:03:23 +08:00
|
|
|
struct unrelated { };
|
2017-03-20 06:06:16 +08:00
|
|
|
|
2021-03-06 14:03:23 +08:00
|
|
|
int main(int, char*[]) {
|
2019-08-15 13:26:52 +08:00
|
|
|
std::cout << "=== optional with iteration ===" << std::endl;
|
2017-03-20 06:06:16 +08:00
|
|
|
|
|
|
|
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
|
2021-03-06 23:14:48 +08:00
|
|
|
lua.new_usertype<super_thing>(
|
|
|
|
"super_thing", sol::base_classes, sol::bases<thing>());
|
2017-03-20 06:06:16 +08:00
|
|
|
|
|
|
|
// Make a few things
|
2021-03-06 14:03:23 +08:00
|
|
|
lua["t1"] = thing {};
|
|
|
|
lua["t2"] = super_thing {};
|
|
|
|
lua["t3"] = unrelated {};
|
2017-03-20 06:06:16 +08:00
|
|
|
// And a table
|
2021-03-06 23:14:48 +08:00
|
|
|
lua["container"] = lua.create_table_with(
|
|
|
|
0, thing { 50 }, 1, unrelated {}, 4, super_thing {});
|
2017-03-20 06:06:16 +08:00
|
|
|
|
|
|
|
|
|
|
|
std::vector<std::reference_wrapper<thing>> things;
|
|
|
|
// Our recursive function
|
2021-03-06 23:14:48 +08:00
|
|
|
// We use some lambda techniques and pass the function
|
|
|
|
// itself itself so we can recurse, but a regular function
|
|
|
|
// would work too!
|
2017-12-27 09:42:40 +08:00
|
|
|
auto fx = [&things](auto& f, auto& tbl) -> void {
|
2019-08-15 13:26:52 +08:00
|
|
|
// You can iterate through a table: it has
|
2017-03-20 06:06:16 +08:00
|
|
|
// 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) {
|
2017-03-20 07:16:20 +08:00
|
|
|
case sol::type::table: {
|
|
|
|
sol::table inner = value.as<sol::table>();
|
|
|
|
f(f, inner);
|
2019-08-15 13:26:52 +08:00
|
|
|
} break;
|
2017-03-20 06:06:16 +08:00
|
|
|
case sol::type::userdata: {
|
2019-08-15 13:26:52 +08:00
|
|
|
// This allows us to check if a userdata is
|
2017-03-20 06:06:16 +08:00
|
|
|
// a specific class type
|
2021-03-06 23:14:48 +08:00
|
|
|
sol::optional<thing&> maybe_thing
|
|
|
|
= value.as<sol::optional<thing&>>();
|
2017-03-20 06:06:16 +08:00
|
|
|
if (maybe_thing) {
|
|
|
|
thing& the_thing = maybe_thing.value();
|
|
|
|
if (key.is<std::string>()) {
|
2021-03-06 23:14:48 +08:00
|
|
|
std::cout << "key "
|
|
|
|
<< key.as<std::string>()
|
|
|
|
<< " is a thing -- ";
|
2017-03-20 06:06:16 +08:00
|
|
|
}
|
|
|
|
else if (key.is<int>()) {
|
2021-03-06 23:14:48 +08:00
|
|
|
std::cout << "key "
|
|
|
|
<< key.as<int>()
|
|
|
|
<< " is a thing -- ";
|
2017-03-20 06:06:16 +08:00
|
|
|
}
|
2021-03-06 23:14:48 +08:00
|
|
|
std::cout << "thing.a ==" << the_thing.a
|
|
|
|
<< std::endl;
|
2017-03-20 06:06:16 +08:00
|
|
|
things.push_back(the_thing);
|
|
|
|
}
|
2019-08-15 13:26:52 +08:00
|
|
|
} break;
|
2017-03-20 06:06:16 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
fx(fx, lua);
|
|
|
|
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|