Move to object_ptr

This commit is contained in:
Jiga228
2025-10-17 21:39:30 +07:00
parent 136235749a
commit 137522cefa
8 changed files with 58 additions and 48 deletions
+8 -8
View File
@@ -16,17 +16,17 @@ Actor::Actor()
std::shared_ptr<SaveMap> Actor::save() std::shared_ptr<SaveMap> Actor::save()
{ {
return std::make_shared<SaveMap>("Actor") return std::make_shared<SaveMap>("Actor")
->SaveObject("loc", &loc) ->SaveObject("loc", static_cast<UType::object_ptr<ISave>>(loc))
->SaveObject("rot", &rot) ->SaveObject("rot", static_cast<UType::object_ptr<ISave>>(rot))
->SaveObject("scale", &scale) ->SaveObject("scale", UType::object_ptr<ISave>(scale))
->SaveListStrings("tags", std::move(tags)); ->SaveListStrings("tags", std::move(tags));
} }
void Actor::load(std::shared_ptr<SaveMap> save) void Actor::load(std::shared_ptr<SaveMap> save)
{ {
loc = *reinterpret_cast<Vector3D*>(save->GetObject("loc")); loc = static_cast<UType::object_ptr<Vector3D>>(save->GetObject("loc"));
rot = *reinterpret_cast<Vector3D*>(save->GetObject("rot")); rot = static_cast<UType::object_ptr<Vector3D>>(save->GetObject("rot"));
scale = *reinterpret_cast<Vector3D*>(save->GetObject("scale")); scale = static_cast<UType::object_ptr<Vector3D>>(save->GetObject("scale"));
tags = std::move(save->GetListString("tags")); tags = std::move(save->GetListString("tags"));
} }
@@ -41,13 +41,13 @@ void Actor::Tick(double delta_time)
void Actor::SetActorLocate(const Vector3D& loc) noexcept void Actor::SetActorLocate(const Vector3D& loc) noexcept
{ {
this->loc = loc; *this->loc = loc;
OnSetActorLocate.Call(loc); OnSetActorLocate.Call(loc);
} }
void Actor::SetActorRotate(const Vector3D& rot) noexcept void Actor::SetActorRotate(const Vector3D& rot) noexcept
{ {
this->rot = rot; *this->rot = rot;
OnSetActorRotate.Call(rot); OnSetActorRotate.Call(rot);
} }
+4 -5
View File
@@ -7,6 +7,7 @@
#include "Game/SaveMap/ISave.h" #include "Game/SaveMap/ISave.h"
#include "Delegate/Delegate.h" #include "Delegate/Delegate.h"
#include "Math/Vector.hpp" #include "Math/Vector.hpp"
#include "Types/object_ptr.hpp"
class World; class World;
GENERATE_META(Actor) GENERATE_META(Actor)
@@ -15,9 +16,7 @@ class Actor : public ISave, public IRTTI
{ {
World* world_; World* world_;
Vector3D loc; UType::object_ptr<Vector3D> loc, rot, scale;
Vector3D rot;
Vector3D scale;
std::list<std::string> tags; std::list<std::string> tags;
@@ -42,14 +41,14 @@ public:
* Вызывает делегат OnSetActorLocate * Вызывает делегат OnSetActorLocate
*/ */
void SetActorLocate(const Vector3D& loc) noexcept; void SetActorLocate(const Vector3D& loc) noexcept;
inline const Vector3D& GetActorLocate() const { return loc; } inline const UType::object_ptr<Vector3D> GetActorLocate() const { return loc; }
/* /*
* Изменяет ориентацию в пространстве * Изменяет ориентацию в пространстве
* Вызывает делегат OnSetActorRotate * Вызывает делегат OnSetActorRotate
*/ */
void SetActorRotate(const Vector3D& rot) noexcept; void SetActorRotate(const Vector3D& rot) noexcept;
inline const Vector3D& GetActorRotate() const { return rot; } inline const UType::object_ptr<Vector3D> GetActorRotate() const { return rot; }
void AddTag(const std::string& tag) noexcept; void AddTag(const std::string& tag) noexcept;
void RemoveTag(const std::string& tag) noexcept; void RemoveTag(const std::string& tag) noexcept;
+13 -13
View File
@@ -95,7 +95,7 @@ SaveMap::SaveMap(const std::string& json_data)
end = object_json.find('\"', begin); end = object_json.find('\"', begin);
std::string object_name = object_json.substr(begin, end - begin); std::string object_name = object_json.substr(begin, end - begin);
ISave* object_ptr = MakeObjectByName(object_name); UType::object_ptr<ISave> object_ptr(MakeObjectByName(object_name));
object_ptr->load(std::make_shared<SaveMap>(object_json)); object_ptr->load(std::make_shared<SaveMap>(object_json));
save_objects[name.c_str() + 1] = object_ptr; save_objects[name.c_str() + 1] = object_ptr;
@@ -169,10 +169,10 @@ SaveMap::SaveMap(const std::string& json_data)
} }
} else if (name[1] == 'o') } else if (name[1] == 'o')
{ {
save_vector_objects[key] = std::vector<ISave*>(); save_vector_objects[key] = std::vector<UType::object_ptr<ISave>>();
if (arr_data.length() == 2) if (arr_data.length() == 2)
continue; continue;
std::vector<ISave*>& vector_object = save_vector_objects[key]; std::vector<UType::object_ptr<ISave>>& vector_object = save_vector_objects[key];
size_t j = 1; size_t j = 1;
while (j < arr_data.length()) while (j < arr_data.length())
@@ -196,7 +196,7 @@ SaveMap::SaveMap(const std::string& json_data)
size_t end_name = object_json.find('\"', begin_name); size_t end_name = object_json.find('\"', begin_name);
std::string object_name = object_json.substr(begin_name, end_name - begin_name); std::string object_name = object_json.substr(begin_name, end_name - begin_name);
ISave* object_ptr = MakeObjectByName(object_name); UType::object_ptr<ISave> object_ptr(MakeObjectByName(object_name));
object_ptr->load(std::make_shared<SaveMap>(object_json)); object_ptr->load(std::make_shared<SaveMap>(object_json));
vector_object.push_back(object_ptr); vector_object.push_back(object_ptr);
@@ -270,10 +270,10 @@ SaveMap::SaveMap(const std::string& json_data)
} }
} else if (name[1] == 'o') } else if (name[1] == 'o')
{ {
save_list_objects[key] = std::list<ISave*>(); save_list_objects[key] = std::list<UType::object_ptr<ISave>>();
if (arr_data.length() == 2) if (arr_data.length() == 2)
continue; continue;
std::list<ISave*>& list_object = save_list_objects[key]; std::list<UType::object_ptr<ISave>>& list_object = save_list_objects[key];
size_t j = 1; size_t j = 1;
while (j < arr_data.length()) while (j < arr_data.length())
@@ -297,7 +297,7 @@ SaveMap::SaveMap(const std::string& json_data)
size_t end_name = object_json.find('\"', begin_name); size_t end_name = object_json.find('\"', begin_name);
std::string object_name = object_json.substr(begin_name, end_name - begin_name); std::string object_name = object_json.substr(begin_name, end_name - begin_name);
ISave* object_ptr = MakeObjectByName(object_name); UType::object_ptr<ISave> object_ptr(MakeObjectByName(object_name));
object_ptr->load(std::make_shared<SaveMap>(object_json)); object_ptr->load(std::make_shared<SaveMap>(object_json));
list_object.push_back(object_ptr); list_object.push_back(object_ptr);
@@ -387,7 +387,7 @@ std::shared_ptr<SaveMap> SaveMap::SaveString(const char* name, const std::string
return std::shared_ptr<SaveMap>(this); return std::shared_ptr<SaveMap>(this);
} }
std::shared_ptr<SaveMap> SaveMap::SaveObject(const char* name, ISave* object) noexcept std::shared_ptr<SaveMap> SaveMap::SaveObject(const char* name, UType::object_ptr<ISave> object) noexcept
{ {
save_objects[name] = object; save_objects[name] = object;
return std::shared_ptr<SaveMap>(this); return std::shared_ptr<SaveMap>(this);
@@ -411,7 +411,7 @@ std::shared_ptr<SaveMap> SaveMap::SaveVectorStrings(const char* name, std::vecto
return std::shared_ptr<SaveMap>(this); return std::shared_ptr<SaveMap>(this);
} }
std::shared_ptr<SaveMap> SaveMap::SaveVectorObject(const char* name, std::vector<ISave*>&& value) noexcept std::shared_ptr<SaveMap> SaveMap::SaveVectorObject(const char* name, std::vector<UType::object_ptr<ISave>>&& value) noexcept
{ {
save_vector_objects[name] = std::move(value); save_vector_objects[name] = std::move(value);
return std::shared_ptr<SaveMap>(this); return std::shared_ptr<SaveMap>(this);
@@ -435,7 +435,7 @@ std::shared_ptr<SaveMap> SaveMap::SaveListStrings(const char* name, std::list<st
return std::shared_ptr<SaveMap>(this); return std::shared_ptr<SaveMap>(this);
} }
std::shared_ptr<SaveMap> SaveMap::SaveListObject(const char* name, std::list<ISave*>&& value) noexcept std::shared_ptr<SaveMap> SaveMap::SaveListObject(const char* name, std::list<UType::object_ptr<ISave>>&& value) noexcept
{ {
save_list_objects[name] = std::move(value); save_list_objects[name] = std::move(value);
return std::shared_ptr<SaveMap>(this); return std::shared_ptr<SaveMap>(this);
@@ -456,7 +456,7 @@ std::string SaveMap::GetString(const char* name)
return save_string[name]; return save_string[name];
} }
ISave* SaveMap::GetObject(const char* name) UType::object_ptr<ISave> SaveMap::GetObject(const char* name)
{ {
return save_objects[name]; return save_objects[name];
} }
@@ -476,7 +476,7 @@ std::vector<std::string> SaveMap::GetVectorString(const char* name)
return save_vector_strings[name]; return save_vector_strings[name];
} }
std::vector<ISave*>& SaveMap::GetVectorObject(const char* name) std::vector<UType::object_ptr<ISave>>& SaveMap::GetVectorObject(const char* name)
{ {
return save_vector_objects[name]; return save_vector_objects[name];
} }
@@ -496,7 +496,7 @@ std::list<std::string>& SaveMap::GetListString(const char* name)
return save_list_strings[name]; return save_list_strings[name];
} }
std::list<ISave*>& SaveMap::GetListObject(const char* name) std::list<UType::object_ptr<ISave>>& SaveMap::GetListObject(const char* name)
{ {
return save_list_objects[name]; return save_list_objects[name];
} }
+10 -9
View File
@@ -1,6 +1,7 @@
#pragma once #pragma once
#include "ISave.h" #include "ISave.h"
#include "Types/object_ptr.hpp"
#include <unordered_map> #include <unordered_map>
#include <memory> #include <memory>
@@ -17,18 +18,18 @@ class SaveMap final
std::unordered_map<std::string, long long> save_long; std::unordered_map<std::string, long long> save_long;
std::unordered_map<std::string, double> save_double; std::unordered_map<std::string, double> save_double;
std::unordered_map<std::string, std::string> save_string; std::unordered_map<std::string, std::string> save_string;
std::unordered_map<std::string, ISave*> save_objects; std::unordered_map<std::string, UType::object_ptr<ISave>> save_objects;
// Массивы // Массивы
std::unordered_map<std::string, std::vector<long long>> save_vector_integer; std::unordered_map<std::string, std::vector<long long>> save_vector_integer;
std::unordered_map<std::string, std::vector<double>> save_vector_double; std::unordered_map<std::string, std::vector<double>> save_vector_double;
std::unordered_map<std::string, std::vector<ISave*>> save_vector_objects; std::unordered_map<std::string, std::vector<UType::object_ptr<ISave>>> save_vector_objects;
std::unordered_map<std::string, std::vector<std::string>> save_vector_strings; std::unordered_map<std::string, std::vector<std::string>> save_vector_strings;
// Связаные списки // Связаные списки
std::unordered_map<std::string, std::list<long long>> save_list_integer; std::unordered_map<std::string, std::list<long long>> save_list_integer;
std::unordered_map<std::string, std::list<double>> save_list_double; std::unordered_map<std::string, std::list<double>> save_list_double;
std::unordered_map<std::string, std::list<ISave*>> save_list_objects; std::unordered_map<std::string, std::list<UType::object_ptr<ISave>>> save_list_objects;
std::unordered_map<std::string, std::list<std::string>> save_list_strings; std::unordered_map<std::string, std::list<std::string>> save_list_strings;
static ISave* MakeObjectByName(const std::string& name); static ISave* MakeObjectByName(const std::string& name);
@@ -47,29 +48,29 @@ public:
std::shared_ptr<SaveMap> SaveInteger(const char* name, int value) noexcept; std::shared_ptr<SaveMap> SaveInteger(const char* name, int value) noexcept;
std::shared_ptr<SaveMap> SaveDouble(const char* name, double value) noexcept; std::shared_ptr<SaveMap> SaveDouble(const char* name, double value) noexcept;
std::shared_ptr<SaveMap> SaveString(const char* name, const std::string& value) noexcept; std::shared_ptr<SaveMap> SaveString(const char* name, const std::string& value) noexcept;
std::shared_ptr<SaveMap> SaveObject(const char* name, ISave* object) noexcept; std::shared_ptr<SaveMap> SaveObject(const char* name, UType::object_ptr<ISave> object) noexcept;
std::shared_ptr<SaveMap> SaveVectorInteger(const char* name, std::vector<long long>&& value) noexcept; std::shared_ptr<SaveMap> SaveVectorInteger(const char* name, std::vector<long long>&& value) noexcept;
std::shared_ptr<SaveMap> SaveVectorDouble(const char* name, std::vector<double>&& value) noexcept; std::shared_ptr<SaveMap> SaveVectorDouble(const char* name, std::vector<double>&& value) noexcept;
std::shared_ptr<SaveMap> SaveVectorStrings(const char* name, std::vector<std::string>&& value) noexcept; std::shared_ptr<SaveMap> SaveVectorStrings(const char* name, std::vector<std::string>&& value) noexcept;
std::shared_ptr<SaveMap> SaveVectorObject(const char* name, std::vector<ISave*>&& value) noexcept; std::shared_ptr<SaveMap> SaveVectorObject(const char* name, std::vector<UType::object_ptr<ISave>>&& value) noexcept;
std::shared_ptr<SaveMap> SaveListInteger(const char* name, std::list<long long>&& value) noexcept; std::shared_ptr<SaveMap> SaveListInteger(const char* name, std::list<long long>&& value) noexcept;
std::shared_ptr<SaveMap> SaveListDouble(const char* name, std::list<double>&& value) noexcept; std::shared_ptr<SaveMap> SaveListDouble(const char* name, std::list<double>&& value) noexcept;
std::shared_ptr<SaveMap> SaveListStrings(const char* name, std::list<std::string>&& value) noexcept; std::shared_ptr<SaveMap> SaveListStrings(const char* name, std::list<std::string>&& value) noexcept;
std::shared_ptr<SaveMap> SaveListObject(const char* name, std::list<ISave*>&& value) noexcept; std::shared_ptr<SaveMap> SaveListObject(const char* name, std::list<UType::object_ptr<ISave>>&& value) noexcept;
// Методы получения значенй // Методы получения значенй
long long GetInteger(const char* name); long long GetInteger(const char* name);
double GetDouble(const char* name); double GetDouble(const char* name);
std::string GetString(const char* name); std::string GetString(const char* name);
ISave* GetObject(const char* name); UType::object_ptr<ISave> GetObject(const char* name);
std::vector<long long>& GetVectorInteger(const char* name); std::vector<long long>& GetVectorInteger(const char* name);
std::vector<double>& GetVectorDouble(const char* name); std::vector<double>& GetVectorDouble(const char* name);
std::vector<std::string> GetVectorString(const char* name); std::vector<std::string> GetVectorString(const char* name);
std::vector<ISave*>& GetVectorObject(const char* name); std::vector<UType::object_ptr<ISave>>& GetVectorObject(const char* name);
std::list<long long>& GetListInteger(const char* name); std::list<long long>& GetListInteger(const char* name);
std::list<double>& GetListDouble(const char* name); std::list<double>& GetListDouble(const char* name);
std::list<std::string>& GetListString(const char* name); std::list<std::string>& GetListString(const char* name);
std::list<ISave*>& GetListObject(const char* name); std::list<UType::object_ptr<ISave>>& GetListObject(const char* name);
std::shared_ptr<SaveMap> getParent() const { return parent_; } std::shared_ptr<SaveMap> getParent() const { return parent_; }
const std::string& getClassName() const { return class_name; } const std::string& getClassName() const { return class_name; }
+2 -4
View File
@@ -8,8 +8,6 @@ World::World(GameInstance& game_instance) : game_instance(game_instance)
World::~World() World::~World()
{ {
for (auto& actor : actors)
delete actor;
actors.clear(); actors.clear();
} }
@@ -28,13 +26,13 @@ void World::Tick(double delta_time)
std::shared_ptr<SaveMap> World::save() std::shared_ptr<SaveMap> World::save()
{ {
std::shared_ptr<SaveMap> save = std::make_shared<SaveMap>("World"); std::shared_ptr<SaveMap> save = std::make_shared<SaveMap>("World");
save->SaveListObject("Actors", std::move(reinterpret_cast<std::list<ISave*>&>(actors))); save->SaveListObject("Actors", std::move(reinterpret_cast<std::list<UType::object_ptr<ISave>>&>(actors)));
return save; return save;
} }
void World::load(std::shared_ptr<SaveMap> save) void World::load(std::shared_ptr<SaveMap> save)
{ {
actors = std::move(reinterpret_cast<std::list<Actor*>&>(save->GetListObject("Actors"))); actors = std::move(reinterpret_cast<std::list<UType::object_ptr<Actor>>&>(save->GetListObject("Actors")));
for (auto& i : actors) for (auto& i : actors)
i->world_ = this; i->world_ = this;
+3 -2
View File
@@ -7,6 +7,7 @@
#include "RTTI.h" #include "RTTI.h"
#include "Game/Actors/Actor.hpp" #include "Game/Actors/Actor.hpp"
#include "Game/Actors/Mesh/Mesh.hpp" #include "Game/Actors/Mesh/Mesh.hpp"
#include "Types/object_ptr.hpp"
class GameInstance; class GameInstance;
@@ -16,7 +17,7 @@ class World : public ISave
GameInstance& game_instance; GameInstance& game_instance;
// Actors only // Actors only
std::list<Actor*> actors; std::list<UType::object_ptr<Actor>> actors;
public: public:
World(GameInstance& game_instance); World(GameInstance& game_instance);
@@ -38,7 +39,7 @@ public:
Container list; Container list;
for (auto i = actors.cbegin(); i != actors.cend(); ++i) for (auto i = actors.cbegin(); i != actors.cend(); ++i)
{ {
if (T* cast_object = RTTI::dyn_cast<T>(*i)) if (T* cast_object = RTTI::dyn_cast<T, Actor>((*i).get()))
list.push_back(cast_object); list.push_back(cast_object);
} }
return list; return list;
+12 -1
View File
@@ -111,7 +111,7 @@ static void add_owner(object_ptr<Ty>& ptr)
{ {
std::atomic<Ty*> ptr_; std::atomic<Ty*> ptr_;
public: public:
object_ptr(Ty* ptr = nullptr) explicit object_ptr(Ty* ptr = nullptr)
{ {
ptr_.store(ptr); ptr_.store(ptr);
if (ptr) counter::add_owner<Ty>(*this); if (ptr) counter::add_owner<Ty>(*this);
@@ -158,6 +158,17 @@ static void add_owner(object_ptr<Ty>& ptr)
Ty& operator*() const noexcept { return *ptr_; } Ty& operator*() const noexcept { return *ptr_; }
Ty* operator->() const noexcept { return ptr_.load(); } Ty* operator->() const noexcept { return ptr_.load(); }
operator bool() const noexcept { return ptr_.load() != nullptr; } operator bool() const noexcept { return ptr_.load() != nullptr; }
object_ptr<Ty>& operator=(const object_ptr<Ty>& ptr)
{
reset(ptr);
return *this;
}
template<typename target_Ty>
explicit operator object_ptr<target_Ty>() const noexcept
{
return object_ptr<target_Ty>(reinterpret_cast<target_Ty*>(ptr_.load()));
}
friend class counter; friend class counter;
}; };
+6 -6
View File
@@ -17,7 +17,7 @@ public:
} }
void load(std::shared_ptr<SaveMap> save) override void load(std::shared_ptr<SaveMap> save) override
{ {
num = save->GetInteger("num"); num = static_cast<int>(save->GetInteger("num"));
str = save->GetString("str"); str = save->GetString("str");
dbl = save->GetDouble("dbl"); dbl = save->GetDouble("dbl");
} }
@@ -158,14 +158,14 @@ TEST(SaveMapTest, check_load_with_custom_type)
EXPECT_EQ(test_vector_string[i], save_map.GetVectorString("TestVectorString")[i]); EXPECT_EQ(test_vector_string[i], save_map.GetVectorString("TestVectorString")[i]);
} }
CustomObject* obj = reinterpret_cast<CustomObject*>(save_map.GetObject("CustomObject")); UType::object_ptr<CustomObject> obj = static_cast<UType::object_ptr<CustomObject>>(save_map.GetObject("CustomObject"));
CustomObject test_obj; CustomObject test_obj;
test_obj.num = 123; test_obj.num = 123;
test_obj.str = "str123"; test_obj.str = "str123";
test_obj.dbl = 123.45; test_obj.dbl = 123.45;
EXPECT_EQ(*obj, test_obj); EXPECT_EQ(*obj.get(), test_obj);
std::vector<CustomObject*> vec_obj = reinterpret_cast<std::vector<CustomObject*>&>(save_map.GetVectorObject("CustomObjects")); std::vector<UType::object_ptr<CustomObject>> vec_obj = reinterpret_cast<std::vector<UType::object_ptr<CustomObject>>&>(save_map.GetVectorObject("CustomObjects"));
std::vector<CustomObject*> vec_obj_test = { std::vector<CustomObject*> vec_obj_test = {
new CustomObject, new CustomObject,
new CustomObject, new CustomObject,
@@ -178,8 +178,8 @@ TEST(SaveMapTest, check_load_with_custom_type)
vec_obj_test[1]->dbl = 543.21; vec_obj_test[1]->dbl = 543.21;
for (auto i = 0; i < 2; i++) for (auto i = 0; i < 2; i++)
{ {
EXPECT_EQ(*vec_obj[i], *vec_obj_test[i]); EXPECT_EQ(*vec_obj[i].get(), *vec_obj_test[i]);
delete vec_obj[i]; vec_obj[i].destroy();
delete vec_obj_test[i]; delete vec_obj_test[i];
} }
} }