aboutsummaryrefslogtreecommitdiffstats
path: root/lib/sol2/examples/source/customization_global_transparent_argument.cpp
diff options
context:
space:
mode:
authorAndy Belle-Isle <drumsetmonkey@gmail.com>2019-08-30 00:45:36 -0400
committerAndy Belle-Isle <drumsetmonkey@gmail.com>2019-08-30 00:45:36 -0400
commitdc2493e7525bb7633f697ef10f72b72b46222249 (patch)
tree9816755219e65d3f47fdce81c78f3736a7ddb8ab /lib/sol2/examples/source/customization_global_transparent_argument.cpp
parent9d2b31797d0cfd130802b69261df2cd402e39b49 (diff)
Forget what I said, I just need to change git attributes to mark for vendor
Diffstat (limited to 'lib/sol2/examples/source/customization_global_transparent_argument.cpp')
-rw-r--r--lib/sol2/examples/source/customization_global_transparent_argument.cpp63
1 files changed, 63 insertions, 0 deletions
diff --git a/lib/sol2/examples/source/customization_global_transparent_argument.cpp b/lib/sol2/examples/source/customization_global_transparent_argument.cpp
new file mode 100644
index 0000000..a7e93c3
--- /dev/null
+++ b/lib/sol2/examples/source/customization_global_transparent_argument.cpp
@@ -0,0 +1,63 @@
+#define SOL_ALL_SAFETIES_ON 1
+#include <sol/sol.hpp>
+
+// Something that can't be collided with
+static const auto& script_key = "GlobalResource.MySpecialIdentifier123";
+
+struct GlobalResource {
+ int value = 2;
+};
+
+template <typename Handler>
+bool sol_lua_check(sol::types<GlobalResource*>, lua_State* L, int /*index*/, Handler&& handler, sol::stack::record& tracking) {
+ // not actually taking anything off the stack
+ tracking.use(0);
+ // get the field from global storage
+ sol::stack::get_field<true>(L, script_key);
+ // verify type
+ sol::type t = static_cast<sol::type>(lua_type(L, -1));
+ lua_pop(L, 1);
+ if (t != sol::type::lightuserdata) {
+ handler(L, 0, sol::type::lightuserdata, t, "global resource is not present as a light userdata");
+ return false;
+ }
+ return true;
+}
+
+GlobalResource* sol_lua_get(sol::types<GlobalResource*>, lua_State* L, int /*index*/, sol::stack::record& tracking) {
+ // retrieve the (light) userdata for this type
+
+ // not actually pulling anything off the stack
+ tracking.use(0);
+ sol::stack::get_field<true>(L, script_key);
+ GlobalResource* ls = static_cast<GlobalResource*>(lua_touserdata(L, -1));
+
+ // clean up stack value returned by `get_field`
+ lua_pop(L, 1);
+ return ls;
+}
+
+int sol_lua_push(sol::types<GlobalResource*>, lua_State* L, GlobalResource* ls) {
+ // push light userdata
+ return sol::stack::push(L, static_cast<void*>(ls));
+}
+
+int main() {
+
+ sol::state lua;
+ lua.open_libraries(sol::lib::base);
+
+ GlobalResource instance;
+
+ // get GlobalResource
+ lua.set_function("f", [](GlobalResource* l, int value) {
+ return l->value + value;
+ });
+ lua.set(script_key, &instance);
+
+ // note only 1 argument,
+ // despite f having 2 arguments to recieve
+ lua.script("assert(f(1) == 3)");
+
+ return 0;
+}