| 19 | }; |
| 20 | |
| 21 | int main(int, char*[]) { |
| 22 | std::cout << "=== userdata memory reference ===" |
| 23 | << std::endl; |
| 24 | |
| 25 | sol::state lua; |
| 26 | lua.open_libraries(sol::lib::base); |
| 27 | |
| 28 | Doge dog {}; // Kept alive somehow |
| 29 | |
| 30 | // Later... |
| 31 | // The following stores a reference, and does not copy/move |
| 32 | // lifetime is same as dog in C++ |
| 33 | // (access after it is destroyed is bad) |
| 34 | lua["dog"] = &dog; |
| 35 | // Same as above: respects std::reference_wrapper |
| 36 | lua["dog"] = std::ref(dog); |
| 37 | // These two are identical to above |
| 38 | lua.set("dog", &dog); |
| 39 | lua.set("dog", std::ref(dog)); |
| 40 | |
| 41 | |
| 42 | Doge& dog_ref = lua["dog"]; // References Lua memory |
| 43 | Doge* dog_pointer = lua["dog"]; // References Lua memory |
| 44 | Doge dog_copy = lua["dog"]; // Copies, will not affect lua |
| 45 | |
| 46 | lua.new_usertype<Doge>("Doge", "tailwag", &Doge::tailwag); |
| 47 | |
| 48 | dog_copy.tailwag = 525; |
| 49 | // Still 50 |
| 50 | lua.script("assert(dog.tailwag == 50)"); |
| 51 | |
| 52 | dog_ref.tailwag = 100; |
| 53 | // Now 100 |
| 54 | lua.script("assert(dog.tailwag == 100)"); |
| 55 | |
| 56 | dog_pointer->tailwag = 345; |
| 57 | // Now 345 |
| 58 | lua.script("assert(dog.tailwag == 345)"); |
| 59 | |
| 60 | std::cout << std::endl; |
| 61 | |
| 62 | return 0; |
| 63 | } |
nothing calls this directly
no test coverage detected