diff options
Diffstat (limited to '3rdparty/sol2/examples')
23 files changed, 0 insertions, 1426 deletions
diff --git a/3rdparty/sol2/examples/any_return.cpp b/3rdparty/sol2/examples/any_return.cpp deleted file mode 100644 index f286071b8d8..00000000000 --- a/3rdparty/sol2/examples/any_return.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include <sol.hpp> - -#include <iostream> -#include <cassert> - -// Uses some of the fancier bits of sol2, including the "transparent argument", -// sol::this_state, which gets the current state and does not increment -// function arguments -sol::object fancy_func(sol::object a, sol::object b, sol::this_state s) { - sol::state_view lua(s); - if (a.is<int>() && b.is<int>()) { - return sol::object(lua, sol::in_place, a.as<int>() + b.as<int>()); - } - else if (a.is<bool>()) { - bool do_triple = a.as<bool>(); - return sol::object(lua, sol::in_place<double>, b.as<double>() * (do_triple ? 3 : 1)); - } - // Can also use make_object - return sol::make_object(lua, sol::nil); -} - -int main() { - sol::state lua; - - lua["f"] = fancy_func; - - int result = lua["f"](1, 2); - // result == 3 - assert(result == 3); - double result2 = lua["f"](false, 2.5); - // result2 == 2.5 - assert(result2 == 2.5); - - // call in Lua, get result - // notice we only need 2 arguments here, not 3 (sol::this_state is transparent) - lua.script("result3 = f(true, 5.5)"); - double result3 = lua["result3"]; - // result3 == 16.5 - assert(result3 == 16.5); - - std::cout << "=== any_return example ===" << std::endl; - std::cout << "result : " << result << std::endl; - std::cout << "result2: " << result2 << std::endl; - std::cout << "result3: " << result3 << std::endl; - std::cout << std::endl; -}
\ No newline at end of file diff --git a/3rdparty/sol2/examples/basic.cpp b/3rdparty/sol2/examples/basic.cpp deleted file mode 100644 index cf00ee70ab2..00000000000 --- a/3rdparty/sol2/examples/basic.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include <sol.hpp> - -#include <iostream> - -int main() { - // create an empty lua state - sol::state lua; - - // by default, libraries are not opened - // you can open libraries by using open_libraries - // the libraries reside in the sol::lib enum class - lua.open_libraries(sol::lib::base); - - // call lua code directly - std::cout << "=== basic example ===" << std::endl; - lua.script("print('hello world')"); - - std::cout << std::endl; -}
\ No newline at end of file diff --git a/3rdparty/sol2/examples/config.cpp b/3rdparty/sol2/examples/config.cpp deleted file mode 100644 index d899de385ca..00000000000 --- a/3rdparty/sol2/examples/config.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include <sol.hpp> -#include <string> -#include <iostream> - -// shows how to load basic data to a struct - -struct config { - std::string name; - int width; - int height; - - void print() { - std::cout << "Name: " << name << '\n' - << "Width: " << width << '\n' - << "Height: " << height << '\n'; - } -}; - -int main() { - sol::state lua; - config screen; - // To use the file, uncomment here and make sure it is in local dir - //lua.script_file("config.lua"); - lua.script(R"( -name = "Asus" -width = 1920 -height = 1080 -)"); - screen.name = lua.get<std::string>("name"); - screen.width = lua.get<int>("width"); - screen.height = lua.get<int>("height"); - assert(screen.name == "Asus"); - assert(screen.width == 1920); - assert(screen.height == 1080); - - std::cout << "=== config example ===" << std::endl; - screen.print(); - std::cout << std::endl; -} diff --git a/3rdparty/sol2/examples/config.lua b/3rdparty/sol2/examples/config.lua deleted file mode 100644 index 0144f989a84..00000000000 --- a/3rdparty/sol2/examples/config.lua +++ /dev/null @@ -1,3 +0,0 @@ -name = "Asus" -width = 1920 -height = 1080 diff --git a/3rdparty/sol2/examples/coroutine.cpp b/3rdparty/sol2/examples/coroutine.cpp deleted file mode 100644 index 75b4f817a3b..00000000000 --- a/3rdparty/sol2/examples/coroutine.cpp +++ /dev/null @@ -1,59 +0,0 @@ -#include <string> -#include "sol.hpp" - -#include <iostream> - -int main() { - std::cout << "=== coroutine example ===" << std::endl; - - sol::state lua; - std::vector<sol::coroutine> tasks; - - lua.open_libraries(sol::lib::base, sol::lib::coroutine); - - sol::thread runner_thread = sol::thread::create(lua); - - lua.set_function("start_task", - [&runner_thread, &tasks](sol::function f, sol::variadic_args va) { - // You must ALWAYS get the current state - sol::state_view runner_thread_state = runner_thread.state(); - // Put the task in our task list to keep it alive and track it - std::size_t task_index = tasks.size(); - tasks.emplace_back(runner_thread_state, f); - sol::coroutine& f_on_runner_thread = tasks[task_index]; - // call coroutine with arguments that came - // from main thread / other thread - // pusher for `variadic_args` and other sol types will transfer the - // arguments from the calling thread to - // the runner thread automatically for you - // using `lua_xmove` internally - int wait = f_on_runner_thread(va); - std::cout << "First return: " << wait << std::endl; - // When you call it again, you don't need new arguments - // (they remain the same from the first call) - f_on_runner_thread(); - std::cout << "Second run complete: " << wait << std::endl; - }); - - lua.script( - R"( -function main(x, y, z) - -- do something - coroutine.yield(20) - -- do something else - -- do ... - print(x, y, z) -end - -function main2(x, y) - coroutine.yield(10) - print(x, y) -end - - start_task(main, 10, 12, 8) - start_task(main2, 1, 2) -)" -); - - return 0; -} diff --git a/3rdparty/sol2/examples/customization.cpp b/3rdparty/sol2/examples/customization.cpp deleted file mode 100644 index 45280e5989b..00000000000 --- a/3rdparty/sol2/examples/customization.cpp +++ /dev/null @@ -1,91 +0,0 @@ -#include <sol.hpp> - -#include <iostream> -#include <iomanip> -#include <cassert> - -struct two_things { - int a; - bool b; -}; - -namespace sol { - - // First, the expected size - // Specialization of a struct - template <> - struct lua_size<two_things> : std::integral_constant<int, 2> {}; - - // Now, specialize various stack structures - namespace stack { - - template <> - struct checker<two_things> { - template <typename Handler> - static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { - // indices can be negative to count backwards from the top of the stack, - // rather than the bottom up - // to deal with this, we adjust the index to - // its absolute position using the lua_absindex function - int absolute_index = lua_absindex(L, index); - // Check first and second second index for being the proper types - bool success = stack::check<int>(L, absolute_index + 1, handler) - && stack::check<bool>(L, absolute_index, handler); - tracking.use(2); - return success; - } - }; - - template <> - struct getter<two_things> { - static two_things get(lua_State* L, int index, record& tracking) { - int absolute_index = lua_absindex(L, index); - // Get the first element - int a = stack::get<int>(L, absolute_index); - // Get the second element, - // in the +1 position from the first - bool b = stack::get<bool>(L, absolute_index + 1); - // we use 2 slots, each of the previous takes 1 - tracking.use(2); - return two_things{ a, b }; - } - }; - - template <> - struct pusher<two_things> { - static int push(lua_State* L, const two_things& things) { - int amount = stack::push(L, things.a); - // amount will be 1: int pushes 1 item - amount += stack::push(L, things.b); - // amount 2 now, since bool pushes a single item - // Return 2 things - return amount; - } - }; - - } -} - -int main() { - sol::state lua; - - // Create a pass-through style of function - lua.script("function f ( a, b ) return a, b end"); - - // get the function out of Lua - sol::function f = lua["f"]; - - two_things things = f(two_things{ 24, true }); - assert(things.a == 24); - assert(things.b == true); - // things.a == 24 - // things.b == true - - std::cout << "=== customization example ===" << std::endl; - std::cout << std::boolalpha; - std::cout << "things.a: " << things.a << std::endl; - std::cout << "things.b: " << things.b << std::endl; - std::cout << std::endl; - - return 0; -} diff --git a/3rdparty/sol2/examples/functions.cpp b/3rdparty/sol2/examples/functions.cpp deleted file mode 100644 index b4f9a1da140..00000000000 --- a/3rdparty/sol2/examples/functions.cpp +++ /dev/null @@ -1,110 +0,0 @@ -#include <sol.hpp> -#include <cassert> -#include <iostream> - -inline int my_add(int x, int y) { - return x + y; -} - -struct multiplier { - int operator()(int x) { - return x * 10; - } - - static int by_five(int x) { - return x * 5; - } -}; - -inline std::string make_string( std::string input ) { - return "string: " + input; -} - -int main() { - std::cout << "=== functions example ===" << std::endl; - - sol::state lua; - lua.open_libraries(sol::lib::base); - - // setting a function is simple - lua.set_function("my_add", my_add); - - // you could even use a lambda - lua.set_function("my_mul", [](double x, double y) { return x * y; }); - - // member function pointers and functors as well - lua.set_function("mult_by_ten", multiplier{}); - lua.set_function("mult_by_five", &multiplier::by_five); - - // assert that the functions work - lua.script("assert(my_add(10, 11) == 21)"); - lua.script("assert(my_mul(4.5, 10) == 45)"); - lua.script("assert(mult_by_ten(50) == 500)"); - lua.script("assert(mult_by_five(10) == 50)"); - - // using lambdas, functions can have state. - int x = 0; - lua.set_function("inc", [&x]() { x += 10; }); - - // calling a stateful lambda modifies the value - lua.script("inc()"); - assert(x == 10); - if (x == 10) { - // Do something based on this information - std::cout << "Yahoo! x is " << x << std::endl; - } - - // this can be done as many times as you want - lua.script(R"( -inc() -inc() -inc() -)"); - assert(x == 40); - if (x == 40) { - // Do something based on this information - std::cout << "Yahoo! x is " << x << std::endl; - } - // retrieval of a function is done similarly - // to other variables, using sol::function - sol::function add = lua["my_add"]; - int value = add(10, 11); - // second way to call the function - int value2 = add.call<int>(10, 11); - assert(value == 21); - assert(value2 == 21); - if (value == 21 && value2 == 21) { - std::cout << "Woo, value is 21!" << std::endl; - } - - // multi-return functions are supported using - // std::tuple as the interface. - lua.set_function("multi", [] { return std::make_tuple(10, "goodbye"); }); - lua.script("x, y = multi()"); - lua.script("assert(x == 10 and y == 'goodbye')"); - - auto multi = lua.get<sol::function>("multi"); - int first; - std::string second; - sol::tie(first, second) = multi(); - - // use the values - assert(first == 10); - assert(second == "goodbye"); - - // you can even overload functions - // just pass in the different functions - // you want to pack into a single name: - // make SURE they take different types! - - lua.set_function("func", sol::overload([](int x) { return x; }, make_string, my_add)); - - // All these functions are now overloaded through "func" - lua.script(R"( -print(func(1)) -print(func("bark")) -print(func(1,2)) -)"); - - std::cout << std::endl; -}
\ No newline at end of file diff --git a/3rdparty/sol2/examples/namespacing.cpp b/3rdparty/sol2/examples/namespacing.cpp deleted file mode 100644 index 8d32744eb64..00000000000 --- a/3rdparty/sol2/examples/namespacing.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#define SOL_CHECK_ARGUMENTS -#include <sol.hpp> -#include <cassert> - -int main() { - struct my_class { - int b = 24; - - int f() const { - return 24; - } - - void g() { - ++b; - } - }; - - sol::state lua; - lua.open_libraries(); - - sol::table bark = lua.create_named_table("bark"); - bark.new_usertype<my_class>("my_class", - "f", &my_class::f, - "g", &my_class::g - ); // the usual - - lua.script("obj = bark.my_class.new()"); // this works - lua.script("obj:g()"); - my_class& obj = lua["obj"]; - assert(obj.b == 25); - - return 0; -} diff --git a/3rdparty/sol2/examples/player_script.lua b/3rdparty/sol2/examples/player_script.lua deleted file mode 100644 index 432983e02a7..00000000000 --- a/3rdparty/sol2/examples/player_script.lua +++ /dev/null @@ -1,29 +0,0 @@ --- call single argument integer constructor -p1 = player.new(2) - --- p2 is still here from being --- set with lua["p2"] = player(0); below -local p2shoots = p2:shoot() -assert(not p2shoots) --- had 0 ammo - --- set variable property setter -p1.hp = 545; --- get variable through property getter -print(p1.hp); - -local did_shoot_1 = p1:shoot() -print(did_shoot_1) -print(p1.bullets) -local did_shoot_2 = p1:shoot() -print(did_shoot_2) -print(p1.bullets) -local did_shoot_3 = p1:shoot() -print(did_shoot_3) - --- can read -print(p1.bullets) --- would error: is a readonly variable, cannot write --- p1.bullets = 20 - -p1:boost()
\ No newline at end of file diff --git a/3rdparty/sol2/examples/protected_functions.cpp b/3rdparty/sol2/examples/protected_functions.cpp deleted file mode 100644 index 340f0134d63..00000000000 --- a/3rdparty/sol2/examples/protected_functions.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include <sol.hpp> - -#include <iostream> - -int main() { - std::cout << "=== protected_functions example ===" << std::endl; - - sol::state lua; - - // A complicated function which can error out - // We define both in terms of Lua code - - lua.script(R"( - function handler (message) - return "Handled this message: " .. message - end - - function f (a) - if a < 0 then - error("negative number detected") - end - return a + 5 - end - )"); - - // Get a protected function out of Lua - sol::protected_function f = lua["f"]; - // Set a non-default error handler - f.error_handler = lua["handler"]; - - sol::protected_function_result result = f(-500); - if (result.valid()) { - // Call succeeded - int x = result; - std::cout << "call succeeded, result is " << x << std::endl; - } - else { - // Call failed - sol::error err = result; - std::string what = err.what(); - std::cout << "call failed, sol::error::what() is " << what << std::endl; - // 'what' Should read - // "Handled this message: negative number detected" - } - - std::cout << std::endl; -} diff --git a/3rdparty/sol2/examples/require.cpp b/3rdparty/sol2/examples/require.cpp deleted file mode 100644 index a94765b9453..00000000000 --- a/3rdparty/sol2/examples/require.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#define SOL_CHECK_ARGUMENTS -#include <sol.hpp> -#include <cassert> - -#include <iostream> - -struct some_class { - int bark = 2012; -}; - -sol::table open_mylib(sol::this_state s) { - sol::state_view lua(s); - - sol::table module = lua.create_table(); - module["func"] = []() { /* super cool function here */ }; - // register a class too - module.new_usertype<some_class>("some_class", - "bark", &some_class::bark - ); - - return module; -} - -int main() { - std::cout << "=== require example ===" << std::endl; - - sol::state lua; - lua.open_libraries(sol::lib::package); - // sol::c_call takes functions at the template level - // and turns it into a lua_CFunction - // alternatively, one can use sol::c_call<sol::wrap<callable_struct, callable_struct{}>> to make the call - // if it's a constexpr struct - lua.require("my_lib", sol::c_call<decltype(&open_mylib), &open_mylib>); - - // do ALL THE THINGS YOU LIKE - lua.script(R"( -s = my_lib.some_class.new() -s.bark = 20; -)"); - some_class& s = lua["s"]; - assert(s.bark == 20); - std::cout << "s.bark = " << s.bark << std::endl; - - std::cout << std::endl; - - return 0; -}
\ No newline at end of file diff --git a/3rdparty/sol2/examples/self_call.cpp b/3rdparty/sol2/examples/self_call.cpp deleted file mode 100644 index c64ded34133..00000000000 --- a/3rdparty/sol2/examples/self_call.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#define SOL_CHECK_ARGUMENTS -#include <sol.hpp> -#include <cassert> -#include <iostream> - -int main() { - std::cout << "=== self_call example ===" << std::endl; - - sol::state lua; - - lua.open_libraries(sol::lib::base, sol::lib::package, sol::lib::table); - - // a small script using 'self' syntax - lua.script(R"( - some_table = { some_val = 100 } - - function some_table:add_to_some_val(value) - self.some_val = self.some_val + value - end - - function print_some_val() - print("some_table.some_val = " .. some_table.some_val) - end - )"); - - // do some printing - lua["print_some_val"](); - // 100 - - sol::table self = lua["some_table"]; - self["add_to_some_val"](self, 10); - lua["print_some_val"](); - - std::cout << std::endl; -}
\ No newline at end of file diff --git a/3rdparty/sol2/examples/static_variables.cpp b/3rdparty/sol2/examples/static_variables.cpp deleted file mode 100644 index c101e824484..00000000000 --- a/3rdparty/sol2/examples/static_variables.cpp +++ /dev/null @@ -1,61 +0,0 @@ -#include <sol.hpp> - -#include <iostream> -#include <cassert> - -struct test { - static int muh_variable; -}; -int test::muh_variable = 25; - - -int main() { - std::cout << "=== static_variables example ===" << std::endl; - - sol::state lua; - lua.open_libraries(); - lua.new_usertype<test>("test", - "direct", sol::var(2), - "global", sol::var(test::muh_variable), - "ref_global", sol::var(std::ref(test::muh_variable)) - ); - - int direct_value = lua["test"]["direct"]; - // direct_value == 2 - assert(direct_value == 2); - std::cout << "direct_value: " << direct_value << std::endl; - - int global = lua["test"]["global"]; - int global2 = lua["test"]["ref_global"]; - // global == 25 - // global2 == 25 - assert(global == 25); - assert(global2 == 25); - - std::cout << "First round of values --" << std::endl; - std::cout << global << std::endl; - std::cout << global2 << std::endl; - - test::muh_variable = 542; - - global = lua["test"]["global"]; - // global == 25 - // global is its own memory: was passed by value - - global2 = lua["test"]["ref_global"]; - // global2 == 542 - // global2 was passed through std::ref - // global2 holds a reference to muh_variable - // if muh_variable goes out of scope or is deleted - // problems could arise, so be careful! - - assert(global == 25); - assert(global2 == 542); - - std::cout << "Second round of values --" << std::endl; - std::cout << "global : " << global << std::endl; - std::cout << "global2: " << global2 << std::endl; - std::cout << std::endl; - - return 0; -} diff --git a/3rdparty/sol2/examples/tables.cpp b/3rdparty/sol2/examples/tables.cpp deleted file mode 100644 index 41798d3b0b4..00000000000 --- a/3rdparty/sol2/examples/tables.cpp +++ /dev/null @@ -1,59 +0,0 @@ -#include <sol.hpp> -#include <string> -#include <iostream> - -// this example shows how to read data in from a lua table - -int main() { - std::cout << "=== tables example ===" << std::endl; - - sol::state lua; - // table used as an array - lua.script("table1 = {\"hello\", \"table\"}"); - // table with a nested table and the key value syntax - lua.script("table2 = {" - "[\"nestedTable\"] = {" - "[\"key1\"] = \"value1\"," - "[\"key2\"]= \"value2\"" - "}," - "[\"name\"]= \"table2\"" - "}"); - - - /* Shorter Syntax: */ - // using the values stored in table1 - /*std::cout << (std::string)lua["table1"][1] << " " - << (std::string)lua["table1"][2] << '\n'; - */ - // some retrieval of values from the nested table - // the cleaner way of doing things - // chain off the the get<>() / [] results - auto t2 = lua.get<sol::table>("table2"); - auto nestedTable = t2.get<sol::table>("nestedTable"); - // Alternatively: - //sol::table t2 = lua["table2"]; - //sol::table nestedTable = t2["nestedTable"]; - - std::string x = lua["table2"]["nestedTable"]["key2"]; - std::cout << "nested table: key1 : " << nestedTable.get<std::string>("key1") << ", key2: " - << x - << '\n'; - std::cout << "name of t2: " << t2.get<std::string>("name") << '\n'; - std::string t2name = t2["name"]; - std::cout << "name of t2: " << t2name << '\n'; - - /* Longer Syntax: */ - // using the values stored in table1 - std::cout << lua.get<sol::table>("table1").get<std::string>(1) << " " - << lua.get<sol::table>("table1").get<std::string>(2) << '\n'; - - // some retrieval of values from the nested table - // the cleaner way of doing things - std::cout << "nested table: key1 : " << nestedTable.get<std::string>("key1") << ", key2: " - // yes you can chain the get<>() results - << lua.get<sol::table>("table2").get<sol::table>("nestedTable").get<std::string>("key2") - << '\n'; - std::cout << "name of t2: " << t2.get<std::string>("name") << '\n'; - - std::cout << std::endl; -} diff --git a/3rdparty/sol2/examples/usertype.cpp b/3rdparty/sol2/examples/usertype.cpp deleted file mode 100644 index 496a0b763d3..00000000000 --- a/3rdparty/sol2/examples/usertype.cpp +++ /dev/null @@ -1,109 +0,0 @@ -#include <sol.hpp> -#include <iostream> -#include <cassert> -#include <cmath> - -struct foo { -private: - std::string name; -public: - foo(std::string name): name(std::string(name)) {} - - void print() { - std::cout << name << '\n'; - } - - int test(int x) { - return name.length() + x; - } -}; - -struct vector { -private: - float x = 0; - float y = 0; -public: - vector() = default; - vector(float x): x(x) {} - vector(float x, float y): x(x), y(y) {} - - bool is_unit() const { - return (x * x + y * y) == 1.f; - } -}; - -struct variables { - bool low_gravity = false; - int boost_level = 0; - -}; - -int main() { - std::cout << "=== usertype example ===" << std::endl; - - sol::state lua; - lua.open_libraries(sol::lib::base, sol::lib::math); - - // the simplest way to create a class is through - // sol::state::new_userdata - // the first template is the class type - // the rest are the constructor parameters - // using new_userdata you can only have one constructor - - - // you must make sure that the name of the function - // goes before the member function pointer - lua.new_usertype<foo, std::string>("foo", "print", &foo::print, "test", &foo::test); - - // making the class from lua is simple - // same with calling member functions - lua.script("x = foo.new('test')\n" - "x:print()\n" - "y = x:test(10)"); - - auto y = lua.get<int>("y"); - std::cout << y << std::endl; // show 14 - - // if you want a class to have more than one constructor - // the way to do so is through set_userdata and creating - // a userdata yourself with constructor types - - { - // Notice the brace: this means we're in a new scope - // first, define the different types of constructors - sol::constructors<sol::types<>, sol::types<float>, sol::types<float, float>> ctor; - - // the only template parameter is the class type - // the first argument of construction is the name - // second is the constructor types - // then the rest are function name and member function pointer pairs - sol::usertype<vector> utype(ctor, "is_unit", &vector::is_unit); - - // then you must register it - lua.set_usertype("vector", utype); - // You can throw away the usertype after you set it: you do NOT - // have to keep it around - // cleanup happens automagically - } - // calling it is the same as new_userdata - - lua.script("v = vector.new()\n" - "v = vector.new(12)\n" - "v = vector.new(10, 10)\n" - "assert(not v:is_unit())\n"); - - // You can even have C++-like member-variable-access - // just pass is public member variables in the same style as functions - lua.new_usertype<variables>("variables", "low_gravity", &variables::low_gravity, "boost_level", &variables::boost_level); - - // making the class from lua is simple - // same with calling member functions/variables - lua.script("local vars = variables.new()\n" - "assert(not vars.low_gravity)\n" - "vars.low_gravity = true\n" - "local x = vars.low_gravity\n" - "assert(x)"); - - std::cout << std::endl; - -} diff --git a/3rdparty/sol2/examples/usertype_advanced.cpp b/3rdparty/sol2/examples/usertype_advanced.cpp deleted file mode 100644 index 32679089844..00000000000 --- a/3rdparty/sol2/examples/usertype_advanced.cpp +++ /dev/null @@ -1,121 +0,0 @@ -#include <sol.hpp> - -#include <iostream> - -struct player { -public: - int bullets; - int speed; - - player() - : player(3, 100) { - - } - - player(int ammo) - : player(ammo, 100) { - - } - - player(int ammo, int hitpoints) - : bullets(ammo), hp(hitpoints) { - - } - - void boost() { - speed += 10; - } - - bool shoot() { - if (bullets < 1) - return false; - --bullets; - return true; - } - - void set_hp(int value) { - hp = value; - } - - int get_hp() const { - return hp; - } - -private: - int hp; -}; - -int main() { - std::cout << "=== usertype_advanced example ===" << std::endl; - sol::state lua; - - lua.open_libraries(sol::lib::base); - - // note that you can set a - // userdata before you register a usertype, - // and it will still carry - // the right metatable if you register it later - - // set a variable "p2" of type "player" with 0 ammo - lua["p2"] = player(0); - - // make usertype metatable - lua.new_usertype<player>("player", - - // 3 constructors - sol::constructors<sol::types<>, sol::types<int>, sol::types<int, int>>(), - - // typical member function that returns a variable - "shoot", &player::shoot, - // typical member function - "boost", &player::boost, - - // gets or set the value using member variable syntax - "hp", sol::property(&player::get_hp, &player::set_hp), - - // read and write variable - "speed", &player::speed, - // can only read from, not write to - "bullets", sol::readonly(&player::bullets) - ); - - std::string player_script = R"( --- call single argument integer constructor -p1 = player.new(2) - --- p2 is still here from being --- set with lua["p2"] = player(0); below -local p2shoots = p2:shoot() -assert(not p2shoots) --- had 0 ammo - --- set variable property setter -p1.hp = 545 --- get variable through property getter -print(p1.hp) -assert(p1.hp == 545) - -local did_shoot_1 = p1:shoot() -print(did_shoot_1) -print(p1.bullets) -local did_shoot_2 = p1:shoot() -print(did_shoot_2) -print(p1.bullets) -local did_shoot_3 = p1:shoot() -print(did_shoot_3) - --- can read -print(p1.bullets) --- would error: is a readonly variable, cannot write --- p1.bullets = 20 - -p1:boost() -)"; - - // Uncomment and use the file to try that out, too! - // Make sure it's in the local directory of the executable after you build, or adjust the filename path - // Or whatever else you like! - //lua.script_file("player_script.lua"); - lua.script(player_script); - std::cout << std::endl; -} diff --git a/3rdparty/sol2/examples/usertype_bitfields.cpp b/3rdparty/sol2/examples/usertype_bitfields.cpp deleted file mode 100644 index 271045babcd..00000000000 --- a/3rdparty/sol2/examples/usertype_bitfields.cpp +++ /dev/null @@ -1,164 +0,0 @@ -#define SOL_CHECK_ARGUMENTS -#include <sol.hpp> - -#include <cstddef> -#include <cstdint> -#include <climits> -#include <type_traits> - -namespace itsy_bitsy { - - template <std::size_t sz, typename C = void> - struct bit_type { - typedef uint64_t type; - }; - - template <std::size_t sz> - struct bit_type<sz, std::enable_if_t<(sz <= 1)>> { - typedef bool type; - }; - - template <std::size_t sz> - struct bit_type<sz, std::enable_if_t<(sz > 2 && sz <= 16)>> { - typedef uint16_t type; - }; - - template <std::size_t sz> - struct bit_type<sz, std::enable_if_t<(sz > 16 && sz <= 32)>> { - typedef uint32_t type; - }; - - template <std::size_t sz> - struct bit_type<sz, std::enable_if_t<(sz > 32 && sz <= 64)>> { - typedef uint64_t type; - }; - - template <std::size_t sz> - using bit_type_t = typename bit_type<sz>::type; - - template <typename T, typename V> - bool vcxx_warning_crap(std::true_type, V val) { - return val != 0; - } - - template <typename T, typename V> - T vcxx_warning_crap(std::false_type, V val) { - return static_cast<T>(val); - } - - template <typename T, typename V> - auto vcxx_warning_crap(V val) { - return vcxx_warning_crap<T>(std::is_same<bool, T>(), val); - } - - template <typename Base, std::size_t bit_target = 0x0, std::size_t size = 0x1> - void write(Base& b, bit_type_t<size> bits) { - typedef bit_type_t<sizeof(Base) * CHAR_BIT> aligned_type; - static const std::size_t aligned_type_bit_size = sizeof(aligned_type) * CHAR_BIT; - static_assert(sizeof(Base) * CHAR_BIT >= (bit_target + size), "bit offset and size are too large for the desired structure."); - static_assert((bit_target % aligned_type_bit_size) <= ((bit_target + size) % aligned_type_bit_size), "bit offset and size cross beyond largest integral constant boundary."); - - const std::size_t aligned_target = (bit_target + size) / aligned_type_bit_size; - const aligned_type bits_left = static_cast<aligned_type>(bit_target - aligned_target); - const aligned_type shifted_mask = ((static_cast<aligned_type>(1) << size) - 1) << bits_left; - const aligned_type compl_shifted_mask = ~shifted_mask; - // Jump by native size of a pointer to target - // then OR the bits - aligned_type* jumper = static_cast<aligned_type*>(static_cast<void*>(&b)); - jumper += aligned_target; - aligned_type& aligned = *jumper; - aligned &= compl_shifted_mask; - aligned |= (static_cast<aligned_type>(bits) << bits_left); - } - - template <typename Base, std::size_t bit_target = 0x0, std::size_t size = 0x1> - bit_type_t<size> read(Base& b) { - typedef bit_type_t<sizeof(Base) * CHAR_BIT> aligned_type; - typedef bit_type_t<size> field_type; - static const std::size_t aligned_type_bit_size = sizeof(aligned_type) * CHAR_BIT; - static_assert(sizeof(Base) * CHAR_BIT >= (bit_target + size), "bit offset and size are too large for the desired structure."); - static_assert((bit_target % aligned_type_bit_size) <= ((bit_target + size) % aligned_type_bit_size), "bit offset and size cross beyond largest integral constant boundary."); - - const std::size_t aligned_target = (bit_target + size) / aligned_type_bit_size; - const aligned_type bits_left = static_cast<aligned_type>(bit_target - aligned_target); - const aligned_type mask = (static_cast<aligned_type>(1) << size) - 1; - // Jump by native size of a pointer to target - // then OR the bits - aligned_type* jumper = static_cast<aligned_type*>(static_cast<void*>(&b)); - jumper += aligned_target; - const aligned_type& aligned = *jumper; - aligned_type field_bits = (aligned >> bits_left) & mask; - field_type bits = vcxx_warning_crap<field_type>(field_bits); - return bits; - } - -} - -#include <iostream> -#include <cassert> - -#if defined(_MSC_VER) || defined(__MINGW32__) -#pragma pack(1) -struct flags_t { -#else -struct __attribute__((packed, aligned(1))) flags_t { -#endif - uint8_t C : 1; - uint8_t N : 1; - uint8_t PV : 1; - uint8_t _3 : 1; - uint8_t H : 1; - uint8_t _5 : 1; - uint8_t Z : 1; - uint8_t S : 1; - uint16_t D : 14; -} flags{}; - -int main() { - std::cout << "=== usertype_bitfields example ===" << std::endl; -#ifdef __MINGW32__ - std::cout << "MinGW Detected, packing structs is broken in MinGW and this test may fail" << std::endl; -#endif - sol::state lua; - lua.open_libraries(); - - lua.new_usertype<flags_t>("flags_t", - "C", sol::property(itsy_bitsy::read<flags_t, 0>, itsy_bitsy::write<flags_t, 0>), - "N", sol::property(itsy_bitsy::read<flags_t, 1>, itsy_bitsy::write<flags_t, 1>), - "D", sol::property(itsy_bitsy::read<flags_t, 8, 14>, itsy_bitsy::write<flags_t, 8, 14>) - ); - - lua["f"] = std::ref(flags); - - lua.script(R"( - print(f.C) - f.C = true; - print(f.C) - - print(f.N) - f.N = true; - print(f.N) - - print(f.D) - f.D = 0xDF; - print(f.D) -)"); - - bool C = flags.C; - bool N = flags.N; - uint16_t D = flags.D; - - std::cout << std::hex; - std::cout << "sizeof(flags): " << sizeof(flags) << std::endl; - std::cout << "C: " << C << std::endl; - std::cout << "N: " << N << std::endl; - std::cout << "D: " << D << std::endl; - - assert(C); - assert(N); - assert(D == 0xDF); - - std::cout << std::endl; - - return 0; -} diff --git a/3rdparty/sol2/examples/usertype_initializers.cpp b/3rdparty/sol2/examples/usertype_initializers.cpp deleted file mode 100644 index 10f1f68cb94..00000000000 --- a/3rdparty/sol2/examples/usertype_initializers.cpp +++ /dev/null @@ -1,67 +0,0 @@ -#define SOL_CHECK_ARGUMENTS -#include <sol.hpp> -#include <memory> -#include <iostream> -#include <cassert> - -struct holy { -private: - holy() : data() {} - holy(int value) : data(value) {} - ~holy() {} - -public: - struct deleter { - void operator()(holy* p) const { - destroy(*p); - } - }; - - const int data; - - static std::unique_ptr<holy, deleter> create() { - std::cout << "creating 'holy' unique_ptr directly and letting sol/Lua handle it" << std::endl; - return std::unique_ptr<holy, deleter>(new holy(50)); - } - - static void initialize(holy& uninitialized_memory) { - std::cout << "initializing 'holy' userdata at " << static_cast<void*>(&uninitialized_memory) << std::endl; - // receive uninitialized memory from Lua: - // properly set it by calling a constructor - // on it - // "placement new" - new (&uninitialized_memory) holy(); - } - - static void destroy(holy& memory_from_lua) { - std::cout << "destroying 'holy' userdata at " << static_cast<void*>(&memory_from_lua) << std::endl; - memory_from_lua.~holy(); - } -}; - -int main() { - std::cout << "=== usertype_initializers example ===" << std::endl; - { // additional scope to make usertype destroy earlier - sol::state lua; - lua.open_libraries(); - - lua.new_usertype<holy>("holy", - "new", sol::initializers(&holy::initialize), - "create", sol::factories(&holy::create), - sol::meta_function::garbage_collect, sol::destructor(&holy::destroy), - "data", &holy::data - ); - - lua.script(R"( -h1 = holy.create() -h2 = holy.new() -print('h1.data is ' .. h1.data) -print('h2.data is ' .. h2.data) -)"); - holy& h1 = lua["h1"]; - holy& h2 = lua["h2"]; - assert(h1.data == 50); - assert(h2.data == 0); - } - std::cout << std::endl; -}
\ No newline at end of file diff --git a/3rdparty/sol2/examples/usertype_simple.cpp b/3rdparty/sol2/examples/usertype_simple.cpp deleted file mode 100644 index 1b64e3b403f..00000000000 --- a/3rdparty/sol2/examples/usertype_simple.cpp +++ /dev/null @@ -1,95 +0,0 @@ -#define SOL_CHECK_ARGUMENTS -#include <sol.hpp> -#include <memory> -#include <iostream> -#include <cassert> - -class generator { -private: - int data = 2; - -public: - int get_data() const { return data; } - void set_data(int value) { data = value % 10; } - - std::vector<int> generate_list() { - return { data, data * 2, data * 3, data * 4, data * 5 }; - } -}; - -struct my_data { - int first = 4; - int second = 8; - int third = 12; -}; - -int main() { - std::cout << "=== usertype_simple example ===" << std::endl; - - sol::state lua; - lua.open_libraries(); - - // simple_usertype acts and behaves like - // a regular usertype - lua.new_simple_usertype<my_data>("my_data", - "first", &my_data::first, - "second", &my_data::second, - "third", &my_data::third - ); - - { - // But, simple_usertype also has a `set` function - // where you can append things to the - // method listing after doing `create_simple_usertype`. - auto generator_registration = lua.create_simple_usertype<generator>(); - generator_registration.set("data", sol::property(&generator::get_data, &generator::set_data)); - // you MUST set the usertype after - // creating it - lua.set_usertype("generator", generator_registration); - } - - // Can update a simple_usertype at runtime, after registration - lua["generator"]["generate_list"] = [](generator& self) { return self.generate_list(); }; - // can set 'static methods' (no self) as well - lua["generator"]["get_num"] = []() { return 100; }; - - // Mix it all together! - lua.script(R"( -mdata = my_data.new() - -local g = generator.new() -g.data = mdata.first -list1 = g:generate_list() -g.data = mdata.second -list2 = g:generate_list() -g.data = mdata.third -list3 = g:generate_list() - -print("From lua: ") -for i = 1, #list1 do - print("\tlist1[" .. i .. "] = " .. list1[i]) -end -for i = 1, #list2 do - print("\tlist2[" .. i .. "] = " .. list2[i]) -end -for i = 1, #list3 do - print("\tlist3[" .. i .. "] = " .. list3[i]) -end - -)"); - my_data& mdata = lua["mdata"]; - std::vector<int>& list1 = lua["list1"]; - std::vector<int>& list2 = lua["list2"]; - std::vector<int>& list3 = lua["list3"]; - assert(list1.size() == 5); - assert(list2.size() == 5); - assert(list3.size() == 5); - for (int i = 1; i <= 5; ++i) { - assert(list1[i - 1] == (mdata.first % 10) * i); - assert(list2[i - 1] == (mdata.second % 10) * i); - assert(list3[i - 1] == (mdata.third % 10) * i); - } - - std::cout << std::endl; - return 0; -}
\ No newline at end of file diff --git a/3rdparty/sol2/examples/usertype_special_functions.cpp b/3rdparty/sol2/examples/usertype_special_functions.cpp deleted file mode 100644 index 6528b684047..00000000000 --- a/3rdparty/sol2/examples/usertype_special_functions.cpp +++ /dev/null @@ -1,70 +0,0 @@ -#define SOL_CHECK_ARGUMENTS -#include <sol.hpp> -#include <iostream> -#include <cassert> -#include <cmath> - -struct vec { - double x; - double y; - - vec() : x(0), y(0) {} - vec(double x, double y) : x(x), y(y) {} - - vec operator-(const vec& right) const { - return vec(x - right.x, y - right.y); - } -}; - -double dot(const vec& left, const vec& right) { - return left.x * right.x + left.x * right.x; -} - -vec operator+(const vec& left, const vec& right) { - return vec(left.x + right.x, left.y + right.y); -} - -int main() { - sol::state lua; - lua.open_libraries(); - - lua.new_usertype<vec>("vec", - sol::constructors<sol::types<>, sol::types<double, double>>(), - "dot", &dot, - "norm", [](const vec& self) { double len = std::sqrt(dot(self, self)); return vec(self.x / len, self.y / len); }, - // we use `sol::resolve` because other operator+ can exist - // in the (global) namespace - sol::meta_function::addition, sol::resolve<vec(const vec&, const vec&)>(::operator+), - sol::meta_function::subtraction, &vec::operator- - ); - - lua.script(R"( - v1 = vec.new(1, 0) - v2 = vec.new(0, 1) - -- as "member function" - d1 = v1:dot(v2) - -- as "static" / "free function" - d2 = vec.dot(v1, v2) - assert(d1 == d2) - - -- doesn't matter if - -- bound as free function - -- or member function: - a1 = v1 + v2 - s1 = v1 - v2 -)"); - - vec& a1 = lua["a1"]; - vec& s1 = lua["s1"]; - - assert(a1.x == 1 && a1.y == 1); - assert(s1.x == 1 && s1.y == -1); - - lua["a2"] = lua["a1"]; - - lua.script(R"( - assert(a1 == a2) - )"); - - std::cout << std::endl; -}
\ No newline at end of file diff --git a/3rdparty/sol2/examples/usertype_var.cpp b/3rdparty/sol2/examples/usertype_var.cpp deleted file mode 100644 index 9f56490aa29..00000000000 --- a/3rdparty/sol2/examples/usertype_var.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#define SOL_CHECK_ARGUMENTS -#include <sol.hpp> -#include <iostream> - -struct test { - static int number; -}; -int test::number = 25; - - -int main() { - sol::state lua; - lua.open_libraries(); - lua.new_usertype<test>("test", - "direct", sol::var(2), - "number", sol::var(test::number), - "ref_number", sol::var(std::ref(test::number)) - ); - - int direct_value = lua["test"]["direct"]; - assert(direct_value == 2); - // direct_value == 2 - - int number = lua["test"]["number"]; - assert(number == 25); - int ref_number = lua["test"]["ref_number"]; - assert(ref_number == 25); - - test::number = 542; - - assert(lua["test"]["number"] == 25); - // number is its own memory: was passed by value - // So does not change - - assert(lua["test"]["ref_number"] == 542); - // ref_number is just test::number - // passed through std::ref - // so, it holds a reference - // which can be updated - // be careful about referencing local variables, - // if they go out of scope but are still reference - // you'll suffer dangling reference bugs! - - return 0; -}
\ No newline at end of file diff --git a/3rdparty/sol2/examples/variables.cpp b/3rdparty/sol2/examples/variables.cpp deleted file mode 100644 index ece88915703..00000000000 --- a/3rdparty/sol2/examples/variables.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include <sol.hpp> -#include <iostream> - -int main() { - std::cout << "=== variables example ===" << std::endl; - - sol::state lua; - - // need the base library for assertions - lua.open_libraries(sol::lib::base); - - // basic setting of a variable - // through multiple ways - lua["x"] = 10; - lua.set("y", "hello"); - - // assert values are as given - lua.script("assert(x == 10)"); - lua.script("assert(y == 'hello')"); - - - // basic retrieval of a variable - // through multiple ways - int x = lua["x"]; - auto y = lua.get<std::string>("y"); - - int x2; - std::string y2; - std::tie(x2, y2) = lua.get<int, std::string>("x", "y"); - - // show the values - std::cout << x << std::endl; - std::cout << y << std::endl; - std::cout << x2 << std::endl; - std::cout << y2 << std::endl; - std::cout << std::endl; -}
\ No newline at end of file diff --git a/3rdparty/sol2/examples/variadic_args.cpp b/3rdparty/sol2/examples/variadic_args.cpp deleted file mode 100644 index 918e33ec790..00000000000 --- a/3rdparty/sol2/examples/variadic_args.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#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 |