diff --git a/projectfiles/CodeBlocks/tests.cbp b/projectfiles/CodeBlocks/tests.cbp index 643dbd3ff54..d853f60f614 100644 --- a/projectfiles/CodeBlocks/tests.cbp +++ b/projectfiles/CodeBlocks/tests.cbp @@ -1188,6 +1188,7 @@ + diff --git a/projectfiles/Xcode/The Battle for Wesnoth.xcodeproj/project.pbxproj b/projectfiles/Xcode/The Battle for Wesnoth.xcodeproj/project.pbxproj index b053803b6b2..b3c55322762 100644 --- a/projectfiles/Xcode/The Battle for Wesnoth.xcodeproj/project.pbxproj +++ b/projectfiles/Xcode/The Battle for Wesnoth.xcodeproj/project.pbxproj @@ -19,6 +19,7 @@ 0554467DB5FE99D85ABCDCA0 /* edit_pbl_translation.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 00574699A982AA23F12B39E0 /* edit_pbl_translation.cpp */; }; 1234567890ABCDEF12345678 /* file_progress.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1234567890ABCDEF12345680 /* file_progress.cpp */; }; 1234567890ABCDEF12345679 /* file_progress.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1234567890ABCDEF12345680 /* file_progress.cpp */; }; + 144E49509EAC409649899BD4 /* test_lua_ptr.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2AC74C76BE76F62C771A81E1 /* test_lua_ptr.cpp */; }; 172E48A5BD149999CE64EDF8 /* prompt.hpp in Headers */ = {isa = PBXBuildFile; fileRef = D4594633BF3F8A06D6AE752F /* prompt.hpp */; }; 179D4E93A08C5A67B071C6C1 /* spinner.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E4214F3DA80B54080C4B548F /* spinner.cpp */; }; 19B14238AD52EC06ED2094F1 /* tab_container.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 162C4B1E9F7373592D0F3B89 /* tab_container.cpp */; }; @@ -1605,6 +1606,7 @@ 20E644DC98F26C756364EC2C /* choose_addon.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = choose_addon.cpp; sourceTree = ""; }; 26A04033A9545CFE8A226FBD /* test_schema_self_validator.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = test_schema_self_validator.cpp; sourceTree = ""; }; 27764FB68F02032F1C0B6748 /* statistics_record.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = statistics_record.cpp; sourceTree = ""; }; + 2AC74C76BE76F62C771A81E1 /* test_lua_ptr.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = test_lua_ptr.cpp; sourceTree = ""; }; 2CFD4922B64EA6C9F71F71A2 /* preferences.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = preferences.hpp; path = preferences/preferences.hpp; sourceTree = ""; }; 3D284B9A81882806D8B25006 /* spritesheet_generator.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = spritesheet_generator.hpp; sourceTree = ""; }; 3975405BB582CA290366CD21 /* test_help_markup.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = test_help_markup.cpp; sourceTree = ""; }; @@ -4894,6 +4896,7 @@ 91E3560E1CACA6CB00774252 /* test_image_modifications.cpp */, 4649B879202886F000827CFB /* test_irdya_date.cpp */, B597C4AD0FACD42E00CE81F5 /* test_lexical_cast.cpp */, + 2AC74C76BE76F62C771A81E1 /* test_lua_ptr.cpp */, 91E356111CACA6CB00774252 /* test_map_location.cpp */, 91E356121CACA6CB00774252 /* test_mp_connect.cpp */, 91E356131CACA6CB00774252 /* test_recall_list.cpp */, @@ -6681,9 +6684,9 @@ DC764C9F94D8B634B47A92B0 /* rich_label.cpp in Sources */, DDA14069BCE29DE0FE71B970 /* gui_test_dialog.cpp in Sources */, DDE34117BDAA30C965F6E4DB /* preferences.cpp in Sources */, - 4A1D4916A16C7C6E07D0BAB2 /* spritesheet_generator.cpp in Sources */, C3854DF5A850564161932EE5 /* test_help_markup.cpp in Sources */, 4A1D4916A16C7C6E07D0BAB2 /* spritesheet_generator.cpp in Sources */, + 144E49509EAC409649899BD4 /* test_lua_ptr.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/source_lists/boost_unit_tests b/source_lists/boost_unit_tests index 0bedc19647d..9d8977c8df2 100644 --- a/source_lists/boost_unit_tests +++ b/source_lists/boost_unit_tests @@ -16,6 +16,7 @@ tests/test_help_markup.cpp tests/test_image_modifications.cpp tests/test_irdya_date.cpp tests/test_lexical_cast.cpp +tests/test_lua_ptr.cpp tests/test_map_location.cpp tests/test_mp_connect.cpp tests/test_recall_list.cpp diff --git a/src/scripting/lua_ptr.hpp b/src/scripting/lua_ptr.hpp index f39269e1c29..bd29b90a08b 100644 --- a/src/scripting/lua_ptr.hpp +++ b/src/scripting/lua_ptr.hpp @@ -23,7 +23,18 @@ class enable_lua_ptr { public: enable_lua_ptr(T* tp) : self_(std::make_shared(tp)) {} + enable_lua_ptr(enable_lua_ptr&& o) : self_(std::move(o.self_)) + { + *self_ = static_cast(this); + } + enable_lua_ptr& operator=(enable_lua_ptr&& o) + { + self_ = std::move(o.self_); + *self_ = static_cast(this); + } private: + enable_lua_ptr(const enable_lua_ptr& o) = delete; + enable_lua_ptr& operator=(const enable_lua_ptr& o) = delete; friend class lua_ptr; std::shared_ptr self_; }; @@ -41,5 +52,17 @@ public: } return nullptr; } + T* operator->() + { + return get_ptr(); + } + operator bool() const + { + return bool(self_.lock()); + } + bool operator!() const + { + return !operator bool(); + } std::weak_ptr self_; }; diff --git a/src/tests/test_lua_ptr.cpp b/src/tests/test_lua_ptr.cpp new file mode 100644 index 00000000000..b54b47689ff --- /dev/null +++ b/src/tests/test_lua_ptr.cpp @@ -0,0 +1,43 @@ +/* + Copyright (C) 2024 - 2024 + Part of the Battle for Wesnoth Project https://www.wesnoth.org/ + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY. + + See the COPYING file for more details. + COPYING file for more details. +*/ + +#define GETTEXT_DOMAIN "wesnoth-test" + +#include "scripting/lua_ptr.hpp" +#include +#include +#include + +struct dummy_object : public enable_lua_ptr { + std::string value; + dummy_object(const std::string& s) : enable_lua_ptr(this), value(s) {} +}; + +BOOST_AUTO_TEST_CASE(test_lua_ptr) { + std::vector vec; + auto& obj = vec.emplace_back("test"); + BOOST_CHECK_EQUAL(obj.value, "test"); + lua_ptr ptr(obj); + BOOST_CHECK(ptr); + BOOST_CHECK_EQUAL(ptr.get_ptr(), &obj); + { + auto obj_moved = std::move(obj); + BOOST_CHECK(ptr); + BOOST_CHECK_EQUAL(ptr.get_ptr(), &obj_moved); + BOOST_CHECK_EQUAL(ptr->value, "test"); + vec.clear(); + } + BOOST_CHECK(!ptr); +}