File: customization_global_transparent_argument.cpp

package info (click to toggle)
sol2 3.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,096 kB
  • sloc: cpp: 43,816; ansic: 1,018; python: 356; sh: 288; makefile: 202
file content (74 lines) | stat: -rw-r--r-- 1,879 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#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;
}