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

Function main

examples/source/static_variables.cpp:12–63  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10
11
12int main() {
13 std::cout << "=== static_variables ===" << std::endl;
14
15 sol::state lua;
16 lua.open_libraries();
17 lua.new_usertype<test>("test",
18 "direct",
19 sol::var(2),
20 "global",
21 sol::var(test::muh_variable),
22 "ref_global",
23 sol::var(std::ref(test::muh_variable)));
24
25 int direct_value = lua["test"]["direct"];
26 // direct_value == 2
27 SOL_ASSERT(direct_value == 2);
28 std::cout << "direct_value: " << direct_value << std::endl;
29
30 int global = lua["test"]["global"];
31 int global2 = lua["test"]["ref_global"];
32 // global == 25
33 // global2 == 25
34 SOL_ASSERT(global == 25);
35 SOL_ASSERT(global2 == 25);
36
37 std::cout << "First round of values --" << std::endl;
38 std::cout << global << std::endl;
39 std::cout << global2 << std::endl;
40
41 test::muh_variable = 542;
42
43 global = lua["test"]["global"];
44 // global == 25
45 // global is its own memory: was passed by value
46
47 global2 = lua["test"]["ref_global"];
48 // global2 == 542
49 // global2 was passed through std::ref
50 // global2 holds a reference to muh_variable
51 // if muh_variable goes out of scope or is deleted
52 // problems could arise, so be careful!
53
54 SOL_ASSERT(global == 25);
55 SOL_ASSERT(global2 == 542);
56
57 std::cout << "Second round of values --" << std::endl;
58 std::cout << "global : " << global << std::endl;
59 std::cout << "global2: " << global2 << std::endl;
60 std::cout << std::endl;
61
62 return 0;
63}

Callers

nothing calls this directly

Calls 2

varFunction · 0.85
open_librariesMethod · 0.80

Tested by

no test coverage detected