aboutsummaryrefslogtreecommitdiffstats
path: root/deps/sol2/examples/source/multi_results.cpp
diff options
context:
space:
mode:
authorAndy Belle-Isle <drumsetmonkey@gmail.com>2019-08-30 00:19:31 -0400
committerAndy Belle-Isle <drumsetmonkey@gmail.com>2019-08-30 00:19:31 -0400
commitbd3fe0cac583739bc0d7c4b5c8f301bb350abca0 (patch)
tree7eeb1aabcebd6999de1c3457d0882246ec0ff4d4 /deps/sol2/examples/source/multi_results.cpp
parent2662ac356ce14dacfbc91689fd37244facff4989 (diff)
Renamed lib to deps so github will ignore it for language stats
Diffstat (limited to 'deps/sol2/examples/source/multi_results.cpp')
-rw-r--r--deps/sol2/examples/source/multi_results.cpp81
1 files changed, 81 insertions, 0 deletions
diff --git a/deps/sol2/examples/source/multi_results.cpp b/deps/sol2/examples/source/multi_results.cpp
new file mode 100644
index 0000000..6487f72
--- /dev/null
+++ b/deps/sol2/examples/source/multi_results.cpp
@@ -0,0 +1,81 @@
+#define SOL_ALL_SAFETIES_ON 1
+#include <sol/sol.hpp>
+
+#include <tuple>
+#include "assert.hpp"
+#include <iostream>
+
+int main() {
+ std::cout << "=== multi results ===" << 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_tuple");
+ int first;
+ std::string second;
+ // tie the values
+ sol::tie(first, second) = multi();
+
+ // use the values
+ c_assert(first == 10);
+ c_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"];
+
+ c_assert(a == 55);
+ c_assert(b == 66);
+ c_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, "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"];
+
+ c_assert(t == 42);
+ c_assert(u);
+ c_assert(v == "awoo");
+
+ return 0;
+} \ No newline at end of file