diff options
author | 2016-10-07 14:43:09 +0200 | |
---|---|---|
committer | 2016-10-07 14:43:09 +0200 | |
commit | 377472a6dd2c9bdff4d5667f5789c49aee2abb4c (patch) | |
tree | a0ce4d6fc3db1524609efc61307d48448e703f38 /3rdparty/sol2/examples/variadic_args.cpp | |
parent | 1335933ce00cf6095e403a24d0b1dd9e0b565650 (diff) |
Added sol2 header only library as future replacement for luabridge (nw)
Diffstat (limited to '3rdparty/sol2/examples/variadic_args.cpp')
-rw-r--r-- | 3rdparty/sol2/examples/variadic_args.cpp | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/3rdparty/sol2/examples/variadic_args.cpp b/3rdparty/sol2/examples/variadic_args.cpp new file mode 100644 index 00000000000..918e33ec790 --- /dev/null +++ b/3rdparty/sol2/examples/variadic_args.cpp @@ -0,0 +1,40 @@ +#include <sol.hpp> + +#include <iostream> + +int main() { + std::cout << "=== variadic_args example ===" << std::endl; + + sol::state lua; + lua.open_libraries(sol::lib::base); + + // Function requires 2 arguments + // rest can be variadic, but: + // va will include everything after "a" argument, + // which means "b" will be part of the varaidic_args list too + // at position 0 + lua.set_function("v", [](int a, sol::variadic_args va, int /*b*/) { + int r = 0; + for (auto v : va) { + int value = v; // get argument out (implicit conversion) + // can also do int v = va.get<int>(i); with index i + r += value; + } + // Only have to add a, b was included from variadic_args and beyond + return r + a; + }); + + lua.script("x = v(25, 25)"); + lua.script("x2 = v(25, 25, 100, 50, 250, 150)"); + lua.script("x3 = v(1, 2, 3, 4, 5, 6)"); + // will error: not enough arguments + //lua.script("x4 = v(1)"); + + lua.script("assert(x == 50)"); + lua.script("assert(x2 == 600)"); + lua.script("assert(x3 == 21)"); + lua.script("print(x)"); // 50 + lua.script("print(x2)"); // 600 + lua.script("print(x3)"); // 21 + std::cout << std::endl; +}
\ No newline at end of file |