MCPcopy Create free account
hub / github.com/ThePhD/sol2 / main

Function main

examples/source/self_from_lua.cpp:11–53  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9// low-level operation!
10
11int main() {
12 struct thing {
13
14 thing(sol::this_state ts) {
15 lua_State* L = ts;
16 // references the object that called this function
17 // in constructors:
18 sol::stack_object selfobj(L, 1);
19
20 // definitely the same
21 thing& self = selfobj.as<thing>();
22 SOL_ASSERT(&self == this);
23 }
24
25 void func(sol::this_state ts) const {
26 lua_State* L = ts;
27 // references the object that called this function
28 // in regular member functions:
29 sol::stack_object selfobj(L, 1);
30 // "1" is the bottom of the Lua stack
31 // 2 is one up, so on and so forth...
32 thing& self = selfobj.as<thing>();
33
34 // definitely the same
35 SOL_ASSERT(&self == this);
36 }
37 };
38
39 sol::state lua;
40 lua.open_libraries(sol::lib::base);
41
42 lua.new_usertype<thing>("thing",
43 sol::constructors<thing(sol::this_state)>(),
44 "func",
45 &thing::func);
46
47 lua.script(R"(
48obj = thing.new()
49obj:func()
50 )");
51
52 return 0;
53}

Callers

nothing calls this directly

Calls 2

open_librariesMethod · 0.80
scriptMethod · 0.80

Tested by

no test coverage detected