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

Function main

examples/source/shared_ptr.cpp:46–120  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

44};
45
46int main() {
47
48 std::cout << "=== shared_ptr support ===" << std::endl;
49
50 sol::state lua;
51 lua.new_usertype<my_type>(
52 "my_type", "value", &my_type::value);
53 {
54 std::shared_ptr<my_type> shared
55 = std::make_shared<my_type>();
56 lua["shared"] = std::move(shared);
57 }
58 {
59 std::cout << "getting reference to shared_ptr..."
60 << std::endl;
61 std::shared_ptr<my_type>& ref_to_shared_ptr
62 = lua["shared"];
63 std::cout << "\tshared.use_count(): "
64 << ref_to_shared_ptr.use_count()
65 << std::endl;
66 my_type& ref_to_my_type = lua["shared"];
67 std::cout << "\tafter getting reference to my_type: "
68 << ref_to_shared_ptr.use_count()
69 << std::endl;
70 my_type* ptr_to_my_type = lua["shared"];
71 std::cout << "\tafter getting pointer to my_type: "
72 << ref_to_shared_ptr.use_count()
73 << std::endl;
74
75 SOL_ASSERT(
76 ptr_to_my_type == ref_to_shared_ptr.get());
77 SOL_ASSERT(
78 &ref_to_my_type == ref_to_shared_ptr.get());
79 SOL_ASSERT(ref_to_shared_ptr->value == 10);
80
81 // script affects all of them equally
82 lua.script("shared.value = 20");
83
84 SOL_ASSERT(ptr_to_my_type->value == 20);
85 SOL_ASSERT(ref_to_my_type.value == 20);
86 SOL_ASSERT(ref_to_shared_ptr->value == 20);
87 }
88 {
89 std::cout << "getting copy of shared_ptr..."
90 << std::endl;
91 std::shared_ptr<my_type> copy_of_shared_ptr
92 = lua["shared"];
93 std::cout << "\tshared.use_count(): "
94 << copy_of_shared_ptr.use_count()
95 << std::endl;
96 my_type copy_of_value = lua["shared"];
97 std::cout << "\tafter getting value copy of my_type: "
98 << copy_of_shared_ptr.use_count()
99 << std::endl;
100
101 SOL_ASSERT(copy_of_shared_ptr->value == 20);
102 SOL_ASSERT(copy_of_value.value == 20);
103

Callers

nothing calls this directly

Calls 3

scriptMethod · 0.80
collect_garbageMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected