Make enable_lua_ptr movable and non-copyable

This commit is contained in:
Celtic Minstrel 2024-09-15 13:25:53 -04:00 committed by Celtic Minstrel
parent e075700115
commit ef66ad5aae
5 changed files with 72 additions and 1 deletions

View File

@ -1176,6 +1176,7 @@
<Unit filename="../../src/tests/test_image_modifications.cpp" />
<Unit filename="../../src/tests/test_irdya_date.cpp" />
<Unit filename="../../src/tests/test_lexical_cast.cpp" />
<Unit filename="../../src/tests/test_lua_ptr.cpp" />
<Unit filename="../../src/tests/test_map_location.cpp" />
<Unit filename="../../src/tests/test_mp_connect.cpp" />
<Unit filename="../../src/tests/test_recall_list.cpp" />

View File

@ -22,6 +22,7 @@
0DA840E1AD033775DD626F42 /* markup.hpp in Headers */ = {isa = PBXBuildFile; fileRef = D7B540678519F0EBD7C19A17 /* markup.hpp */; };
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 */; };
@ -1607,6 +1608,7 @@
20E644DC98F26C756364EC2C /* choose_addon.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = choose_addon.cpp; sourceTree = "<group>"; };
26A04033A9545CFE8A226FBD /* test_schema_self_validator.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = test_schema_self_validator.cpp; sourceTree = "<group>"; };
27764FB68F02032F1C0B6748 /* statistics_record.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = statistics_record.cpp; sourceTree = "<group>"; };
2AC74C76BE76F62C771A81E1 /* test_lua_ptr.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = test_lua_ptr.cpp; sourceTree = "<group>"; };
2CFD4922B64EA6C9F71F71A2 /* preferences.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = preferences.hpp; path = preferences/preferences.hpp; sourceTree = "<group>"; };
3975405BB582CA290366CD21 /* test_help_markup.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = test_help_markup.cpp; sourceTree = "<group>"; };
39A1469A99EFCB311DF640CB /* addon_server_info.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = addon_server_info.cpp; path = addon_server_info.cpp; sourceTree = "<group>"; };
@ -4876,6 +4878,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 */,
@ -6647,13 +6650,13 @@
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 */,
DA6F4A0B9083938EDB32E7C9 /* markup.cpp in Sources */,
362245818DDA4C7E4CC8165A /* charconv.cpp in Sources */,
355942A786D57DD0A6A93E2A /* units_dialog.cpp in Sources */,
52074B55B6C7AC8A1AE8BEA8 /* addon_server_info.cpp in Sources */,
144E49509EAC409649899BD4 /* test_lua_ptr.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@ -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

View File

@ -23,7 +23,18 @@ class enable_lua_ptr
{
public:
enable_lua_ptr(T* tp) : self_(std::make_shared<T*>(tp)) {}
enable_lua_ptr(enable_lua_ptr&& o) : self_(std::move(o.self_))
{
*self_ = static_cast<T*>(this);
}
enable_lua_ptr& operator=(enable_lua_ptr&& o)
{
self_ = std::move(o.self_);
*self_ = static_cast<T*>(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<T>;
std::shared_ptr<T*> 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<T*> self_;
};

View File

@ -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 <boost/test/unit_test.hpp>
#include <string>
#include <vector>
struct dummy_object : public enable_lua_ptr<dummy_object> {
std::string value;
dummy_object(const std::string& s) : enable_lua_ptr<dummy_object>(this), value(s) {}
};
BOOST_AUTO_TEST_CASE(test_lua_ptr) {
std::vector<dummy_object> vec;
auto& obj = vec.emplace_back("test");
BOOST_CHECK_EQUAL(obj.value, "test");
lua_ptr<dummy_object> 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);
}