| 44 | }; |
| 45 | |
| 46 | int main() { |
| 47 | |
| 48 | std::cout << "=== unique_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::unique_ptr<my_type> unique |
| 55 | = std::make_unique<my_type>(); |
| 56 | lua["unique"] = std::move(unique); |
| 57 | } |
| 58 | { |
| 59 | std::cout << "getting reference to unique_ptr..." |
| 60 | << std::endl; |
| 61 | std::unique_ptr<my_type>& ref_to_unique_ptr |
| 62 | = lua["unique"]; |
| 63 | my_type& ref_to_my_type = lua["unique"]; |
| 64 | my_type* ptr_to_my_type = lua["unique"]; |
| 65 | |
| 66 | SOL_ASSERT( |
| 67 | ptr_to_my_type == ref_to_unique_ptr.get()); |
| 68 | SOL_ASSERT( |
| 69 | &ref_to_my_type == ref_to_unique_ptr.get()); |
| 70 | SOL_ASSERT(ref_to_unique_ptr->value == 10); |
| 71 | |
| 72 | // script affects all of them equally |
| 73 | lua.script("unique.value = 20"); |
| 74 | |
| 75 | SOL_ASSERT(ptr_to_my_type->value == 20); |
| 76 | SOL_ASSERT(ref_to_my_type.value == 20); |
| 77 | SOL_ASSERT(ref_to_unique_ptr->value == 20); |
| 78 | } |
| 79 | { |
| 80 | std::cout << "getting copy of unique_ptr..." |
| 81 | << std::endl; |
| 82 | my_type copy_of_value = lua["unique"]; |
| 83 | |
| 84 | SOL_ASSERT(copy_of_value.value == 20); |
| 85 | |
| 86 | // script still affects pointer, but does not affect |
| 87 | // copy of `my_type` |
| 88 | lua.script("unique.value = 30"); |
| 89 | |
| 90 | SOL_ASSERT(copy_of_value.value == 20); |
| 91 | } |
| 92 | // set to nil and collect garbage to destroy it |
| 93 | lua.script("unique = nil"); |
| 94 | lua.collect_garbage(); |
| 95 | lua.collect_garbage(); |
| 96 | |
| 97 | std::cout << "garbage has been collected" << std::endl; |
| 98 | std::cout << std::endl; |
| 99 | |
| 100 | return 0; |
| 101 | } |
nothing calls this directly
no test coverage detected