2017-08-25 02:39:02 +08:00
|
|
|
#define SOL_CHECK_ARGUMENTS 1
|
2017-03-30 13:31:55 +08:00
|
|
|
#include <sol.hpp>
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
2017-12-26 12:27:22 +08:00
|
|
|
#include "assert.hpp"
|
2017-03-30 13:31:55 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
2017-03-31 02:11:43 +08:00
|
|
|
|
|
|
|
// nested allows serialization of maps with vectors inside, and vice-versa
|
|
|
|
// all from a nested structure of Lua tables
|
|
|
|
// it has less control over which pieces are considered tables in Lua,
|
|
|
|
// and which ones are considered userdata, but it covers a good 90% of cases
|
|
|
|
// where someone wants to handle a nested table
|
|
|
|
void demo(sol::nested<std::map<std::string, std::vector<std::string>>> src) {
|
|
|
|
std::cout << "demo, sol::nested<...>" << std::endl;
|
|
|
|
const auto& listmap = src.source;
|
2017-12-26 12:27:22 +08:00
|
|
|
c_assert(listmap.size() == 2);
|
2017-03-31 02:11:43 +08:00
|
|
|
for (const auto& kvp : listmap) {
|
|
|
|
const std::vector<std::string>& strings = kvp.second;
|
2017-12-26 12:27:22 +08:00
|
|
|
c_assert(strings.size() == 3);
|
2017-03-31 02:11:43 +08:00
|
|
|
std::cout << "\t" << kvp.first << " = ";
|
|
|
|
for (const auto& s : strings) {
|
|
|
|
std::cout << "'" << s << "'" << " ";
|
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This second demo is equivalent to the first
|
2017-03-30 13:31:55 +08:00
|
|
|
// Nota bene the signature here
|
2017-03-31 02:11:43 +08:00
|
|
|
// Every container-type that's meant to be
|
|
|
|
// a table must be wrapped in `sol::as_table_t`
|
|
|
|
// it's verbose, so feel free to use typedefs to make it easy on you
|
|
|
|
// you can mix which parts are considered tables from Lua, and which parts
|
|
|
|
// are considered other kinds of types, such as userdata and the like
|
|
|
|
void demo_explicit (sol::as_table_t<std::map<std::string, sol::as_table_t<std::vector<std::string>>>> src) {
|
|
|
|
std::cout << "demo, explicit sol::as_table_t<...>" << std::endl;
|
|
|
|
// Have to access the "source" member variable for as_table_t
|
2017-03-30 13:31:55 +08:00
|
|
|
const auto& listmap = src.source;
|
2017-12-26 12:27:22 +08:00
|
|
|
c_assert(listmap.size() == 2);
|
2017-03-30 13:31:55 +08:00
|
|
|
for (const auto& kvp : listmap) {
|
2017-03-31 02:11:43 +08:00
|
|
|
// Have to access the internal "source" for the inner as_table_t, as well
|
2017-03-30 13:31:55 +08:00
|
|
|
const std::vector<std::string>& strings = kvp.second.source;
|
2017-12-26 12:27:22 +08:00
|
|
|
c_assert(strings.size() == 3);
|
2017-03-31 02:11:43 +08:00
|
|
|
std::cout << "\t" << kvp.first << " = ";
|
2017-03-30 13:31:55 +08:00
|
|
|
for (const auto& s : strings) {
|
|
|
|
std::cout << "'" << s << "'" << " ";
|
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
2017-03-31 02:11:43 +08:00
|
|
|
std::cout << std::endl;
|
2017-03-30 13:31:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int, char**) {
|
2018-03-16 05:16:28 +08:00
|
|
|
std::cout << "=== containers retrieved from lua tables ===" << std::endl;
|
2017-03-30 13:31:55 +08:00
|
|
|
|
|
|
|
sol::state lua;
|
|
|
|
// bind the function
|
|
|
|
lua.set_function("f", &demo);
|
2017-03-31 02:11:43 +08:00
|
|
|
lua.set_function("g", &demo_explicit);
|
2017-03-30 13:31:55 +08:00
|
|
|
// Call it with a table that has string sequences set to distinct keys
|
|
|
|
lua.script(R"(
|
2017-03-31 02:11:43 +08:00
|
|
|
t = {
|
2017-03-30 13:31:55 +08:00
|
|
|
key1 = {'hello', 'there', 'world'},
|
|
|
|
key2 = {'bark', 'borf', 'woof'}
|
2017-03-31 02:11:43 +08:00
|
|
|
}
|
|
|
|
f(t)
|
|
|
|
g(t)
|
2017-03-30 13:31:55 +08:00
|
|
|
)");
|
|
|
|
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|