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

Function main

examples/source/tutorials/pointer_lifetime.cpp:9–82  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7};
8
9int main() {
10
11 sol::state lua;
12
13 /*
14 // AAAHHH BAD
15 // dangling pointer!
16 lua["my_func"] = []() -> my_type* { return new my_type(); };
17
18 // AAAHHH!
19 lua.set("something", new my_type());
20
21 // AAAAAAHHH!!!
22 lua["something_else"] = new my_type();
23 */
24
25 // :ok:
26 lua["my_func0"] = []() -> std::unique_ptr<my_type> {
27 return std::make_unique<my_type>();
28 };
29
30 // :ok:
31 lua["my_func1"] = []() -> std::shared_ptr<my_type> {
32 return std::make_shared<my_type>();
33 };
34
35 // :ok:
36 lua["my_func2"] = []() -> my_type { return my_type(); };
37
38 // :ok:
39 lua.set(
40 "something", std::unique_ptr<my_type>(new my_type()));
41
42 std::shared_ptr<my_type> my_shared
43 = std::make_shared<my_type>();
44 // :ok:
45 lua.set("something_else", my_shared);
46
47 // :ok:
48 auto my_unique = std::make_unique<my_type>();
49 lua["other_thing"] = std::move(my_unique);
50
51 // :ok:
52 lua["my_func5"] = []() -> my_type* {
53 static my_type mt;
54 return &mt;
55 };
56
57 // THIS IS STILL BAD DON'T DO IT AAAHHH BAD
58 // return a unique_ptr that's empty instead
59 // or be explicit!
60 lua["my_func6"] = []() -> my_type* { return nullptr; };
61
62 // :ok:
63 lua["my_func7"]
64 = []() -> std::nullptr_t { return nullptr; };
65
66 // :ok:

Callers

nothing calls this directly

Calls 2

my_typeClass · 0.70
setMethod · 0.45

Tested by

no test coverage detected