mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
e69e7c79fa
added variadic_results, to return a variable number of arguments to Lua added variadic_results and as_results added improved function examples (for multiple results and split overloading out) added tests for variadics added tests for C++17 utilities added a forwarding header added a specific `unsafe_function` header added and improved documetation pages
81 lines
2.1 KiB
C++
81 lines
2.1 KiB
C++
#define SOL_CHECK_ARGUMENTS
|
|
#include <sol.hpp>
|
|
|
|
#include <tuple>
|
|
#include <cassert>
|
|
#include <iostream>
|
|
|
|
int main() {
|
|
std::cout << "=== multi results example ===" << std::endl;
|
|
|
|
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
|
|
lua.set_function("multi_tuple", [] {
|
|
return std::make_tuple(10, "goodbye");
|
|
});
|
|
lua.script("print('calling multi_tuple')");
|
|
lua.script("print(multi_tuple())");
|
|
lua.script("x, y = multi_tuple()");
|
|
lua.script("assert(x == 10 and y == 'goodbye')");
|
|
|
|
auto multi = lua.get<sol::function>("multi");
|
|
int first;
|
|
std::string second;
|
|
// tie the values
|
|
sol::tie(first, second) = multi();
|
|
|
|
// use the values
|
|
assert(first == 10);
|
|
assert(second == "goodbye");
|
|
|
|
// sol::as_returns
|
|
// works with any iterable,
|
|
// but we show off std::vector here
|
|
lua.set_function("multi_containers", [] (bool add_extra) {
|
|
std::vector<int> values{55, 66};
|
|
if (add_extra) {
|
|
values.push_back(77);
|
|
}
|
|
return sol::as_returns(std::move(values));
|
|
});
|
|
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"];
|
|
|
|
assert(a == 55);
|
|
assert(b == 66);
|
|
assert(c == 77);
|
|
|
|
// 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 });
|
|
values.push_back({ L, sol::in_place_type<const char*>, "awoo" });
|
|
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"];
|
|
|
|
assert(t == 42);
|
|
assert(u);
|
|
assert(v == "awoo");
|
|
|
|
return 0;
|
|
} |