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-07-10 00:00:57 +08:00
|
|
|
|
|
|
|
#include <tuple>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
int main() {
|
2018-03-16 05:16:28 +08:00
|
|
|
std::cout << "=== multi results ===" << std::endl;
|
2017-07-10 00:00:57 +08:00
|
|
|
|
|
|
|
sol::state lua;
|
|
|
|
lua.open_libraries(sol::lib::base);
|
|
|
|
|
|
|
|
// multi-return functions are supported using
|
|
|
|
// std::tuple as the transfer type,
|
|
|
|
// sol::as_returns for containers,
|
|
|
|
// and sol::variadic_results for more special things
|
2021-03-06 14:03:23 +08:00
|
|
|
lua.set_function("multi_tuple", [] { return std::make_tuple(10, "goodbye"); });
|
2017-07-10 00:00:57 +08:00
|
|
|
lua.script("print('calling multi_tuple')");
|
|
|
|
lua.script("print(multi_tuple())");
|
|
|
|
lua.script("x, y = multi_tuple()");
|
2017-12-26 21:04:54 +08:00
|
|
|
lua.script("assert(x == 10 and y == 'goodbye')");
|
2017-07-10 00:00:57 +08:00
|
|
|
|
2017-07-10 01:52:02 +08:00
|
|
|
auto multi = lua.get<sol::function>("multi_tuple");
|
2017-07-10 00:00:57 +08:00
|
|
|
int first;
|
|
|
|
std::string second;
|
|
|
|
// tie the values
|
|
|
|
sol::tie(first, second) = multi();
|
|
|
|
|
|
|
|
// use the values
|
2021-03-06 14:03:23 +08:00
|
|
|
sol_c_assert(first == 10);
|
|
|
|
sol_c_assert(second == "goodbye");
|
2017-07-10 00:00:57 +08:00
|
|
|
|
|
|
|
// sol::as_returns
|
2021-03-06 14:03:23 +08:00
|
|
|
// works with any iterable,
|
2017-07-10 00:00:57 +08:00
|
|
|
// but we show off std::vector here
|
2021-03-06 14:03:23 +08:00
|
|
|
lua.set_function("multi_containers", [](bool add_extra) {
|
|
|
|
std::vector<int> values { 55, 66 };
|
2017-07-10 00:00:57 +08:00
|
|
|
if (add_extra) {
|
|
|
|
values.push_back(77);
|
|
|
|
}
|
2021-03-06 14:03:23 +08:00
|
|
|
return sol::as_returns(std::move(values));
|
2017-07-10 00:00:57 +08:00
|
|
|
});
|
|
|
|
lua.script("print('calling multi_containers')");
|
|
|
|
lua.script("print(multi_containers(false))");
|
|
|
|
lua.script("a, b, c = multi_containers(true)");
|
|
|
|
int a = lua["a"];
|
|
|
|
int b = lua["b"];
|
|
|
|
int c = lua["c"];
|
|
|
|
|
2021-03-06 14:03:23 +08:00
|
|
|
sol_c_assert(a == 55);
|
|
|
|
sol_c_assert(b == 66);
|
|
|
|
sol_c_assert(c == 77);
|
2017-07-10 00:00:57 +08:00
|
|
|
|
|
|
|
// sol::variadic_results
|
|
|
|
// you can push objects of different types
|
|
|
|
// note that sol::this_state is a transparent
|
|
|
|
// argument: you don't need to pass
|
|
|
|
// that state through Lua
|
|
|
|
lua.set_function("multi_vars", [](int a, bool b, sol::this_state L) {
|
|
|
|
sol::variadic_results values;
|
|
|
|
values.push_back({ L, sol::in_place_type<int>, a });
|
|
|
|
values.push_back({ L, sol::in_place_type<bool>, b });
|
2017-07-10 01:52:02 +08:00
|
|
|
values.push_back({ L, sol::in_place, "awoo" });
|
2017-07-10 00:00:57 +08:00
|
|
|
return values;
|
|
|
|
});
|
|
|
|
lua.script("print('calling multi_vars')");
|
|
|
|
lua.script("print(multi_vars(2, false))");
|
|
|
|
lua.script("t, u, v = multi_vars(42, true)");
|
|
|
|
int t = lua["t"];
|
|
|
|
bool u = lua["u"];
|
|
|
|
std::string v = lua["v"];
|
|
|
|
|
2021-03-06 14:03:23 +08:00
|
|
|
sol_c_assert(t == 42);
|
|
|
|
sol_c_assert(u);
|
|
|
|
sol_c_assert(v == "awoo");
|
2017-07-10 00:00:57 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|