Удалил UType::object_ptr и заменил его на комбинацию из std::shared_ptr и std::weak_ptr из-за ужасной реализации.
This commit is contained in:
@@ -74,6 +74,7 @@ CoreInstance::CoreInstance()
|
||||
render_engine_ = RenderEngineFactory(*this);
|
||||
} catch (const std::exception& e)
|
||||
{
|
||||
render_engine_ = nullptr;
|
||||
std::cerr << "[!] Init render engine: " << e.what() << '\n';
|
||||
return;
|
||||
}
|
||||
@@ -83,6 +84,7 @@ CoreInstance::CoreInstance()
|
||||
game_ = GameFactory(*this);
|
||||
} catch (const std::exception& e)
|
||||
{
|
||||
game_ = nullptr;
|
||||
std::cout << "[!] Init game instance: " << e.what() << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
#include "Game/SaveMap/SaveMap.hpp"
|
||||
#include "Game/World/World.hpp"
|
||||
#include "Log/Log.hpp"
|
||||
|
||||
void Actor::OnDestroy()
|
||||
{
|
||||
@@ -16,17 +15,17 @@ Actor::Actor() : loc_(new Vector3D), rot_(new Vector3D), scale_(new Vector3D)
|
||||
std::shared_ptr<SaveMap> Actor::save()
|
||||
{
|
||||
return std::make_shared<SaveMap>("Actor")
|
||||
->SaveObject("loc", static_cast<UType::object_ptr<ISave>>(loc_))
|
||||
->SaveObject("rot", static_cast<UType::object_ptr<ISave>>(rot_))
|
||||
->SaveObject("scale", UType::object_ptr<ISave>(scale_))
|
||||
->SaveObject("loc", loc_)
|
||||
->SaveObject("rot", rot_)
|
||||
->SaveObject("scale", scale_)
|
||||
->SaveListStrings("tags", std::move(tags));
|
||||
}
|
||||
|
||||
void Actor::load(std::shared_ptr<SaveMap> save)
|
||||
{
|
||||
loc_ = static_cast<UType::object_ptr<Vector3D>>(save->GetObject("loc"));
|
||||
rot_ = static_cast<UType::object_ptr<Vector3D>>(save->GetObject("rot"));
|
||||
scale_ = static_cast<UType::object_ptr<Vector3D>>(save->GetObject("scale"));
|
||||
loc_ = std::static_pointer_cast<Vector3D>(save->GetObject("loc"));
|
||||
rot_ = std::static_pointer_cast<Vector3D>(save->GetObject("rot"));
|
||||
scale_ = std::static_pointer_cast<Vector3D>(save->GetObject("scale"));
|
||||
tags = std::move(save->GetListString("tags"));
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
#include "Game/SaveMap/ISave.h"
|
||||
#include "Delegate/Delegate.h"
|
||||
#include "Math/Vector.hpp"
|
||||
#include "Types/object_ptr.hpp"
|
||||
|
||||
class World;
|
||||
GENERATE_META(Actor)
|
||||
@@ -17,7 +16,7 @@ class Actor : public ISave, public IRTTI
|
||||
World* world_;
|
||||
std::string name_;
|
||||
|
||||
UType::object_ptr<Vector3D> loc_, rot_, scale_;
|
||||
std::shared_ptr<Vector3D> loc_, rot_, scale_;
|
||||
|
||||
std::list<std::string> tags;
|
||||
|
||||
@@ -42,14 +41,14 @@ public:
|
||||
* Вызывает делегат OnSetActorLocate
|
||||
*/
|
||||
void SetActorLocate(const Vector3D& loc) noexcept;
|
||||
inline const UType::object_ptr<Vector3D> GetActorLocate() const { return loc_; }
|
||||
inline Vector3D GetActorLocate() const { return *loc_; }
|
||||
|
||||
/*
|
||||
* Изменяет ориентацию в пространстве
|
||||
* Вызывает делегат OnSetActorRotate
|
||||
*/
|
||||
void SetActorRotate(const Vector3D& rot) noexcept;
|
||||
inline const UType::object_ptr<Vector3D> GetActorRotate() const { return rot_; }
|
||||
inline Vector3D GetActorRotate() const { return *rot_; }
|
||||
|
||||
void AddTag(const std::string& tag) noexcept;
|
||||
void RemoveTag(const std::string& tag) noexcept;
|
||||
|
||||
@@ -97,7 +97,7 @@ SaveMap::SaveMap(const std::string& json_data)
|
||||
end = object_json.find('\"', begin);
|
||||
|
||||
std::string object_name = object_json.substr(begin, end - begin);
|
||||
UType::object_ptr<ISave> object_ptr(MakeObjectByName(object_name));
|
||||
std::shared_ptr<ISave> object_ptr(MakeObjectByName(object_name));
|
||||
|
||||
object_ptr->load(std::make_shared<SaveMap>(object_json));
|
||||
save_objects[name.c_str() + 1] = object_ptr;
|
||||
@@ -171,10 +171,10 @@ SaveMap::SaveMap(const std::string& json_data)
|
||||
}
|
||||
} else if (name[1] == 'o')
|
||||
{
|
||||
save_vector_objects[key] = std::vector<UType::object_ptr<ISave>>();
|
||||
save_vector_objects[key] = std::vector<std::shared_ptr<ISave>>();
|
||||
if (arr_data.length() == 2)
|
||||
continue;
|
||||
std::vector<UType::object_ptr<ISave>>& vector_object = save_vector_objects[key];
|
||||
std::vector<std::shared_ptr<ISave>>& vector_object = save_vector_objects[key];
|
||||
|
||||
size_t j = 1;
|
||||
while (j < arr_data.length())
|
||||
@@ -198,7 +198,7 @@ SaveMap::SaveMap(const std::string& json_data)
|
||||
size_t end_name = object_json.find('\"', begin_name);
|
||||
|
||||
std::string object_name = object_json.substr(begin_name, end_name - begin_name);
|
||||
UType::object_ptr<ISave> object_ptr(MakeObjectByName(object_name));
|
||||
std::shared_ptr<ISave> object_ptr(MakeObjectByName(object_name));
|
||||
|
||||
object_ptr->load(std::make_shared<SaveMap>(object_json));
|
||||
vector_object.push_back(object_ptr);
|
||||
@@ -272,10 +272,10 @@ SaveMap::SaveMap(const std::string& json_data)
|
||||
}
|
||||
} else if (name[1] == 'o')
|
||||
{
|
||||
save_list_objects[key] = std::list<UType::object_ptr<ISave>>();
|
||||
save_list_objects[key] = std::list<std::shared_ptr<ISave>>();
|
||||
if (arr_data.length() == 2)
|
||||
continue;
|
||||
std::list<UType::object_ptr<ISave>>& list_object = save_list_objects[key];
|
||||
std::list<std::shared_ptr<ISave>>& list_object = save_list_objects[key];
|
||||
|
||||
size_t j = 1;
|
||||
while (j < arr_data.length())
|
||||
@@ -299,7 +299,7 @@ SaveMap::SaveMap(const std::string& json_data)
|
||||
size_t end_name = object_json.find('\"', begin_name);
|
||||
|
||||
std::string object_name = object_json.substr(begin_name, end_name - begin_name);
|
||||
UType::object_ptr<ISave> object_ptr(MakeObjectByName(object_name));
|
||||
std::shared_ptr<ISave> object_ptr(MakeObjectByName(object_name));
|
||||
|
||||
object_ptr->load(std::make_shared<SaveMap>(object_json));
|
||||
list_object.push_back(object_ptr);
|
||||
@@ -389,7 +389,7 @@ std::shared_ptr<SaveMap> SaveMap::SaveString(const char* name, const std::string
|
||||
return std::shared_ptr<SaveMap>(this);
|
||||
}
|
||||
|
||||
std::shared_ptr<SaveMap> SaveMap::SaveObject(const char* name, UType::object_ptr<ISave> object) noexcept
|
||||
std::shared_ptr<SaveMap> SaveMap::SaveObject(const char* name, std::shared_ptr<ISave> object) noexcept
|
||||
{
|
||||
save_objects[name] = object;
|
||||
return std::shared_ptr<SaveMap>(this);
|
||||
@@ -413,7 +413,7 @@ std::shared_ptr<SaveMap> SaveMap::SaveVectorStrings(const char* name, std::vecto
|
||||
return std::shared_ptr<SaveMap>(this);
|
||||
}
|
||||
|
||||
std::shared_ptr<SaveMap> SaveMap::SaveVectorObject(const char* name, std::vector<UType::object_ptr<ISave>>&& value) noexcept
|
||||
std::shared_ptr<SaveMap> SaveMap::SaveVectorObject(const char* name, std::vector<std::shared_ptr<ISave>>&& value) noexcept
|
||||
{
|
||||
save_vector_objects[name] = std::move(value);
|
||||
return std::shared_ptr<SaveMap>(this);
|
||||
@@ -437,7 +437,7 @@ std::shared_ptr<SaveMap> SaveMap::SaveListStrings(const char* name, std::list<st
|
||||
return std::shared_ptr<SaveMap>(this);
|
||||
}
|
||||
|
||||
std::shared_ptr<SaveMap> SaveMap::SaveListObject(const char* name, std::list<UType::object_ptr<ISave>>&& value) noexcept
|
||||
std::shared_ptr<SaveMap> SaveMap::SaveListObject(const char* name, std::list<std::shared_ptr<ISave>>&& value) noexcept
|
||||
{
|
||||
save_list_objects[name] = std::move(value);
|
||||
return std::shared_ptr<SaveMap>(this);
|
||||
@@ -458,7 +458,7 @@ std::string SaveMap::GetString(const char* name)
|
||||
return save_string[name];
|
||||
}
|
||||
|
||||
UType::object_ptr<ISave> SaveMap::GetObject(const char* name)
|
||||
std::shared_ptr<ISave> SaveMap::GetObject(const char* name)
|
||||
{
|
||||
return save_objects[name];
|
||||
}
|
||||
@@ -478,7 +478,7 @@ std::vector<std::string> SaveMap::GetVectorString(const char* name)
|
||||
return save_vector_strings[name];
|
||||
}
|
||||
|
||||
std::vector<UType::object_ptr<ISave>>& SaveMap::GetVectorObject(const char* name)
|
||||
std::vector<std::shared_ptr<ISave>>& SaveMap::GetVectorObject(const char* name)
|
||||
{
|
||||
return save_vector_objects[name];
|
||||
}
|
||||
@@ -498,7 +498,7 @@ std::list<std::string>& SaveMap::GetListString(const char* name)
|
||||
return save_list_strings[name];
|
||||
}
|
||||
|
||||
std::list<UType::object_ptr<ISave>>& SaveMap::GetListObject(const char* name)
|
||||
std::list<std::shared_ptr<ISave>>& SaveMap::GetListObject(const char* name)
|
||||
{
|
||||
return save_list_objects[name];
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "ISave.h"
|
||||
#include "Types/object_ptr.hpp"
|
||||
|
||||
#include <unordered_map>
|
||||
#include <memory>
|
||||
@@ -18,18 +17,18 @@ class SaveMap final
|
||||
std::unordered_map<std::string, long long> save_long;
|
||||
std::unordered_map<std::string, double> save_double;
|
||||
std::unordered_map<std::string, std::string> save_string;
|
||||
std::unordered_map<std::string, UType::object_ptr<ISave>> save_objects;
|
||||
std::unordered_map<std::string, std::shared_ptr<ISave>> save_objects;
|
||||
|
||||
// Массивы
|
||||
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<UType::object_ptr<ISave>>> save_vector_objects;
|
||||
std::unordered_map<std::string, std::vector<std::shared_ptr<ISave>>> save_vector_objects;
|
||||
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<double>> save_list_double;
|
||||
std::unordered_map<std::string, std::list<UType::object_ptr<ISave>>> save_list_objects;
|
||||
std::unordered_map<std::string, std::list<std::shared_ptr<ISave>>> save_list_objects;
|
||||
std::unordered_map<std::string, std::list<std::string>> save_list_strings;
|
||||
|
||||
static ISave* MakeObjectByName(const std::string& name);
|
||||
@@ -48,29 +47,29 @@ public:
|
||||
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> SaveString(const char* name, const std::string& value) noexcept;
|
||||
std::shared_ptr<SaveMap> SaveObject(const char* name, UType::object_ptr<ISave> object) noexcept;
|
||||
std::shared_ptr<SaveMap> SaveObject(const char* name, std::shared_ptr<ISave> object) 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> SaveVectorStrings(const char* name, std::vector<std::string>&& value) noexcept;
|
||||
std::shared_ptr<SaveMap> SaveVectorObject(const char* name, std::vector<UType::object_ptr<ISave>>&& value) noexcept;
|
||||
std::shared_ptr<SaveMap> SaveVectorObject(const char* name, std::vector<std::shared_ptr<ISave>>&& 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> SaveListStrings(const char* name, std::list<std::string>&& value) noexcept;
|
||||
std::shared_ptr<SaveMap> SaveListObject(const char* name, std::list<UType::object_ptr<ISave>>&& value) noexcept;
|
||||
std::shared_ptr<SaveMap> SaveListObject(const char* name, std::list<std::shared_ptr<ISave>>&& value) noexcept;
|
||||
|
||||
// Методы получения значенй
|
||||
long long GetInteger(const char* name);
|
||||
double GetDouble(const char* name);
|
||||
std::string GetString(const char* name);
|
||||
UType::object_ptr<ISave> GetObject(const char* name);
|
||||
std::shared_ptr<ISave> GetObject(const char* name);
|
||||
std::vector<long long>& GetVectorInteger(const char* name);
|
||||
std::vector<double>& GetVectorDouble(const char* name);
|
||||
std::vector<std::string> GetVectorString(const char* name);
|
||||
std::vector<UType::object_ptr<ISave>>& GetVectorObject(const char* name);
|
||||
std::vector<std::shared_ptr<ISave>>& GetVectorObject(const char* name);
|
||||
std::list<long long>& GetListInteger(const char* name);
|
||||
std::list<double>& GetListDouble(const char* name);
|
||||
std::list<std::string>& GetListString(const char* name);
|
||||
std::list<UType::object_ptr<ISave>>& GetListObject(const char* name);
|
||||
std::list<std::shared_ptr<ISave>>& GetListObject(const char* name);
|
||||
|
||||
std::shared_ptr<SaveMap> getParent() const { return parent_; }
|
||||
const std::string& getClassName() const { return class_name; }
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
#include "Game/SaveMap/SaveMap.hpp"
|
||||
|
||||
World::World(GameInstance& game_instance) : game_instance(game_instance)
|
||||
World::World(GameInstance& game_instance) : game_instance_(game_instance)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ std::shared_ptr<SaveMap> World::save()
|
||||
void World::load(std::shared_ptr<SaveMap> save)
|
||||
{
|
||||
std::vector<std::string> actors_IDs = save->GetVectorString("ActorsIDs");
|
||||
std::list<UType::object_ptr<Actor>> act = std::move(reinterpret_cast<std::list<UType::object_ptr<Actor>>&>(save->GetListObject("Actors")));
|
||||
std::list<std::shared_ptr<Actor>> act = std::move(reinterpret_cast<std::list<std::shared_ptr<Actor>>&>(save->GetListObject("Actors")));
|
||||
|
||||
size_t i_id = 0;
|
||||
for (auto& i : act)
|
||||
@@ -46,13 +46,13 @@ void World::load(std::shared_ptr<SaveMap> save)
|
||||
}
|
||||
}
|
||||
|
||||
void World::DestroyActor(UType::object_ptr<Actor> ptr)
|
||||
void World::DestroyActor(std::weak_ptr<Actor> ptr)
|
||||
{
|
||||
if (ptr.get() == nullptr)
|
||||
std::shared_ptr<Actor> actor = ptr.lock();
|
||||
if (actor.get() == nullptr)
|
||||
return;
|
||||
|
||||
std::string name = ptr->GetName();
|
||||
std::string name = actor->GetName();
|
||||
actor->OnDestroy();
|
||||
actors_map.erase(name);
|
||||
ptr->OnDestroy();
|
||||
ptr.destroy();
|
||||
}
|
||||
|
||||
+11
-12
@@ -7,16 +7,15 @@
|
||||
|
||||
#include "RTTI.h"
|
||||
#include "Game/Actors/Actor.hpp"
|
||||
#include "Types/object_ptr.hpp"
|
||||
|
||||
class GameInstance;
|
||||
|
||||
GENERATE_META(World);
|
||||
class World : public ISave
|
||||
{
|
||||
GameInstance& game_instance;
|
||||
GameInstance& game_instance_;
|
||||
|
||||
std::unordered_map<std::string, UType::object_ptr<Actor>> actors_map;
|
||||
std::unordered_map<std::string, std::shared_ptr<Actor>> actors_map;
|
||||
|
||||
public:
|
||||
World(GameInstance& game_instance);
|
||||
@@ -25,7 +24,7 @@ public:
|
||||
virtual void BeginPlay();
|
||||
virtual void Tick(double delta_time);
|
||||
|
||||
GameInstance& GetGameInstance() const { return game_instance; }
|
||||
GameInstance& GetGameInstance() const { return game_instance_; }
|
||||
|
||||
#pragma region ISave
|
||||
std::shared_ptr<SaveMap> save() override;
|
||||
@@ -33,21 +32,21 @@ public:
|
||||
#pragma endregion
|
||||
|
||||
template<class T>
|
||||
std::vector<UType::object_ptr<T>> GetActorsByClass()
|
||||
std::vector<std::weak_ptr<T>> GetActorsByClass()
|
||||
{
|
||||
std::vector<UType::object_ptr<T>> list;
|
||||
std::vector<std::weak_ptr<T>> list;
|
||||
for (auto i = actors_map.cbegin(); i != actors_map.cend(); ++i)
|
||||
{
|
||||
if (T* cast_object = RTTI::dyn_cast<T, Actor>(i->second.get()))
|
||||
list.push_back(UType::object_ptr<T>(cast_object));
|
||||
if (RTTI::dyn_cast<T, Actor>(i->second.get()))
|
||||
list.push_back(std::static_pointer_cast<T>(i->second));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
UType::object_ptr<T> SpawnActorFormClass(std::string name, const Vector3D& loc = { 0, 0, 0 }, const Vector3D& rot = { 0, 0, 0 })
|
||||
std::weak_ptr<T> SpawnActorFormClass(std::string name, const Vector3D& loc = { 0, 0, 0 }, const Vector3D& rot = { 0, 0, 0 })
|
||||
{
|
||||
UType::object_ptr<T> object(new T());
|
||||
std::shared_ptr<T> object = std::make_shared<T>();
|
||||
object->SetActorLocate(loc);
|
||||
object->SetActorRotate(rot);
|
||||
static_cast<Actor*>(object)->world_ = this;
|
||||
@@ -55,7 +54,7 @@ public:
|
||||
return object;
|
||||
}
|
||||
|
||||
void DestroyActor(UType::object_ptr<Actor> ptr);
|
||||
void DestroyActor(std::weak_ptr<Actor> ptr);
|
||||
|
||||
template<class T, class Container = std::vector<T*>>
|
||||
Container GetActorsByTag(std::string tag)
|
||||
@@ -76,7 +75,7 @@ public:
|
||||
}
|
||||
|
||||
template<class T>
|
||||
UType::object_ptr<T> GetActorByID(const std::string& id)
|
||||
std::weak_ptr<T> GetActorByID(const std::string& id)
|
||||
{
|
||||
return actors_map[id];
|
||||
}
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
#include "object_ptr.hpp"
|
||||
|
||||
std::unordered_map<void*, UType::counter::counter_owners> UType::counter::owners_map;
|
||||
@@ -1,185 +0,0 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <mutex>
|
||||
#include <unordered_map>
|
||||
#include <list>
|
||||
|
||||
namespace UType
|
||||
{
|
||||
template<typename Ty>
|
||||
class object_ptr;
|
||||
|
||||
class counter
|
||||
{
|
||||
struct counter_owners
|
||||
{
|
||||
std::atomic_bool is_destroyed;
|
||||
std::mutex mutex;
|
||||
std::list<void*> owners;
|
||||
|
||||
counter_owners()
|
||||
: is_destroyed(false)
|
||||
{
|
||||
}
|
||||
|
||||
counter_owners(const counter_owners&) = delete;
|
||||
counter_owners& operator=(const counter_owners&) = delete;
|
||||
|
||||
counter_owners(counter_owners&& other) noexcept
|
||||
: is_destroyed(other.is_destroyed.load())
|
||||
, owners(std::move(other.owners))
|
||||
{
|
||||
}
|
||||
|
||||
counter_owners& operator=(counter_owners&& other) noexcept
|
||||
{
|
||||
if (this != &other)
|
||||
{
|
||||
is_destroyed.store(other.is_destroyed.load());
|
||||
owners = std::move(other.owners);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
static std::unordered_map<void*, counter_owners> owners_map;
|
||||
|
||||
public:
|
||||
template<typename Ty>
|
||||
static void add_owner(object_ptr<Ty>& ptr)
|
||||
{
|
||||
auto counter = owners_map.find(ptr.ptr_.load());
|
||||
if (counter == owners_map.end())
|
||||
{
|
||||
counter_owners owners;
|
||||
owners.owners.push_back(&ptr);
|
||||
auto [it, inserted] = owners_map.try_emplace(ptr.ptr_.load(), std::move(owners));
|
||||
if (!inserted)
|
||||
{
|
||||
ptr.ptr_.store(nullptr);
|
||||
}
|
||||
}
|
||||
else if (!counter->second.is_destroyed)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(counter->second.mutex);
|
||||
counter->second.owners.push_back(&ptr);
|
||||
}
|
||||
else
|
||||
ptr.ptr_.store(nullptr);
|
||||
}
|
||||
|
||||
template<typename Ty>
|
||||
static void remove_owner(object_ptr<Ty>& ptr)
|
||||
{
|
||||
auto counter = owners_map.find(ptr.ptr_);
|
||||
if (counter != owners_map.end())
|
||||
{
|
||||
counter->second.owners.remove(&ptr);
|
||||
|
||||
if (counter->second.is_destroyed)
|
||||
{
|
||||
ptr.ptr_.store(nullptr);
|
||||
}
|
||||
else if (counter->second.owners.empty())
|
||||
{
|
||||
counter->second.is_destroyed.store(true);
|
||||
for (auto& owner : counter->second.owners)
|
||||
static_cast<object_ptr<Ty>*>(owner)->ptr_.store(nullptr);
|
||||
delete static_cast<Ty*>(counter->first);
|
||||
owners_map.erase(counter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Ty>
|
||||
static void remove_object(Ty* ptr)
|
||||
{
|
||||
auto counter = owners_map.find(ptr);
|
||||
if (counter != owners_map.end())
|
||||
{
|
||||
counter->second.is_destroyed.store(true);
|
||||
for (auto& owner : counter->second.owners)
|
||||
static_cast<object_ptr<Ty>*>(owner)->ptr_.store(nullptr);
|
||||
delete ptr;
|
||||
owners_map.erase(counter);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Ty>
|
||||
class object_ptr
|
||||
{
|
||||
std::atomic<Ty*> ptr_;
|
||||
public:
|
||||
explicit object_ptr(Ty* ptr = nullptr)
|
||||
{
|
||||
ptr_.store(ptr);
|
||||
if (ptr) counter::add_owner<Ty>(*this);
|
||||
}
|
||||
|
||||
object_ptr(const object_ptr& ptr)
|
||||
{
|
||||
ptr_.store(ptr.ptr_);
|
||||
if (ptr.ptr_) counter::add_owner<Ty>(*this);
|
||||
}
|
||||
|
||||
object_ptr(object_ptr&& ptr) noexcept
|
||||
{
|
||||
ptr_.store(ptr.ptr_);
|
||||
if (ptr.ptr_) counter::add_owner<Ty>(*this);
|
||||
ptr.ptr_.store(nullptr);
|
||||
}
|
||||
|
||||
~object_ptr()
|
||||
{
|
||||
if (ptr_) counter::remove_owner<Ty>(*this);
|
||||
}
|
||||
|
||||
void reset(Ty* new_ptr = nullptr)
|
||||
{
|
||||
if (ptr_) counter::remove_owner<Ty>(*this);
|
||||
ptr_.store(new_ptr);
|
||||
if (new_ptr) counter::add_owner<Ty>(*this);
|
||||
}
|
||||
|
||||
void reset(const object_ptr& ptr)
|
||||
{
|
||||
if (ptr_) counter::remove_owner<Ty>(*this);
|
||||
ptr_.store(ptr.ptr_);
|
||||
if (ptr.ptr_) counter::add_owner<Ty>(*this);
|
||||
}
|
||||
|
||||
void destroy()
|
||||
{
|
||||
if (ptr_) counter::remove_object<Ty>(ptr_);
|
||||
}
|
||||
|
||||
Ty* get() const { return ptr_.load(); }
|
||||
Ty& operator*() const
|
||||
{
|
||||
if (!ptr_.load())
|
||||
throw std::runtime_error("Null pointer");
|
||||
return *ptr_;
|
||||
}
|
||||
Ty* operator->() const
|
||||
{
|
||||
if (!ptr_.load())
|
||||
throw std::runtime_error("Null pointer");
|
||||
return ptr_.load();
|
||||
}
|
||||
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;
|
||||
};
|
||||
}
|
||||
@@ -11,9 +11,11 @@ TestWorld::TestWorld(GameInstance& game_instance) : World(game_instance)
|
||||
void TestWorld::BeginPlay()
|
||||
{
|
||||
World::BeginPlay();
|
||||
std::vector<UType::object_ptr<Actor>> meshes = GetActorsByClass<Actor>();
|
||||
std::vector<std::weak_ptr<Actor>> meshes = GetActorsByClass<Actor>();
|
||||
for (const auto& i : meshes)
|
||||
Loging::Log("Load actor: " + i->GetName());
|
||||
{
|
||||
Loging::Log("Load actor: " + i.lock()->GetName());
|
||||
}
|
||||
}
|
||||
|
||||
void TestWorld::Tick(double delta_time)
|
||||
@@ -23,11 +25,12 @@ void TestWorld::Tick(double delta_time)
|
||||
time += delta_time;
|
||||
if (time >= 10.0)
|
||||
{
|
||||
std::vector<UType::object_ptr<Mesh>> meshes = GetActorsByClass<Mesh>();
|
||||
std::vector<std::weak_ptr<Mesh>> meshes = GetActorsByClass<Mesh>();
|
||||
if (!meshes.empty())
|
||||
{
|
||||
Loging::Message("TestWorld destroy: " + meshes[0]->GetName());
|
||||
DestroyActor(static_cast<UType::object_ptr<Actor>>(meshes[0]));
|
||||
std::shared_ptr<Mesh> mesh = meshes[0].lock();
|
||||
Loging::Message("TestWorld destroy: " + mesh->GetName());
|
||||
DestroyActor(meshes[0]);
|
||||
time = 7.0;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -15,7 +15,6 @@ file(GLOB SRC
|
||||
"ObjectPtrTest.cpp"
|
||||
|
||||
"${PROJECT_SOURCE_DIR}/Core/Game/SaveMap/SaveMap.cpp"
|
||||
"${PROJECT_SOURCE_DIR}/Core/Types/object_ptr.cpp"
|
||||
)
|
||||
|
||||
add_executable(Tests ${SRC})
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "Types/object_ptr.hpp"
|
||||
|
||||
class TestClass
|
||||
{
|
||||
int* test_ptr_;
|
||||
public:
|
||||
TestClass(int* test_ptr) : test_ptr_(test_ptr)
|
||||
{
|
||||
*test_ptr_ = 1;
|
||||
}
|
||||
|
||||
~TestClass()
|
||||
{
|
||||
*test_ptr_ = 2;
|
||||
}
|
||||
};
|
||||
|
||||
TEST(object_ptr_test, base_test)
|
||||
{
|
||||
int* test = new int(123);
|
||||
UType::object_ptr<int> ptr(test);
|
||||
EXPECT_EQ(*ptr.get(), 123);
|
||||
|
||||
UType::object_ptr<int> ptr2(ptr);
|
||||
EXPECT_EQ(*ptr2.get(), 123);
|
||||
EXPECT_EQ(ptr2.get(), ptr.get());
|
||||
|
||||
ptr.destroy();
|
||||
EXPECT_EQ(ptr.get(), nullptr);
|
||||
EXPECT_EQ(ptr2.get(), nullptr);
|
||||
}
|
||||
|
||||
TEST(object_ptr_test, check_destry)
|
||||
{
|
||||
UType::object_ptr<int> ptr(new int(123)), ptr2(ptr);
|
||||
|
||||
ptr.destroy();
|
||||
EXPECT_EQ(ptr.get(), nullptr);
|
||||
EXPECT_EQ(ptr2.get(), nullptr);
|
||||
}
|
||||
|
||||
TEST(object_ptr_test, check_destroy)
|
||||
{
|
||||
int* test = new int(0);
|
||||
{
|
||||
UType::object_ptr<TestClass> ptr(new TestClass(test));
|
||||
EXPECT_EQ(*test, 1);
|
||||
}
|
||||
EXPECT_EQ(*test, 2);
|
||||
|
||||
*test = 0;
|
||||
{
|
||||
UType::object_ptr<TestClass> ptr(new TestClass(test));
|
||||
UType::object_ptr<TestClass> ptr2(ptr);
|
||||
EXPECT_EQ(*test, 1);
|
||||
}
|
||||
EXPECT_EQ(*test, 2);
|
||||
}
|
||||
@@ -158,14 +158,15 @@ TEST(SaveMapTest, check_load_with_custom_type)
|
||||
EXPECT_EQ(test_vector_string[i], save_map.GetVectorString("TestVectorString")[i]);
|
||||
}
|
||||
|
||||
UType::object_ptr<CustomObject> obj = static_cast<UType::object_ptr<CustomObject>>(save_map.GetObject("CustomObject"));
|
||||
std::shared_ptr obj = std::static_pointer_cast<CustomObject>(save_map.GetObject("CustomObject"));
|
||||
|
||||
CustomObject test_obj;
|
||||
test_obj.num = 123;
|
||||
test_obj.str = "str123";
|
||||
test_obj.dbl = 123.45;
|
||||
EXPECT_EQ(*obj.get(), test_obj);
|
||||
|
||||
std::vector<UType::object_ptr<CustomObject>> vec_obj = reinterpret_cast<std::vector<UType::object_ptr<CustomObject>>&>(save_map.GetVectorObject("CustomObjects"));
|
||||
std::vector<std::shared_ptr<CustomObject>> vec_obj = reinterpret_cast<std::vector<std::shared_ptr<CustomObject>>&>(save_map.GetVectorObject("CustomObjects"));
|
||||
std::vector<CustomObject*> vec_obj_test = {
|
||||
new CustomObject,
|
||||
new CustomObject,
|
||||
@@ -179,7 +180,6 @@ TEST(SaveMapTest, check_load_with_custom_type)
|
||||
for (auto i = 0; i < 2; i++)
|
||||
{
|
||||
EXPECT_EQ(*vec_obj[i].get(), *vec_obj_test[i]);
|
||||
vec_obj[i].destroy();
|
||||
delete vec_obj_test[i];
|
||||
}
|
||||
}
|
||||
@@ -1043,9 +1043,12 @@ UwURenderEngine::~UwURenderEngine()
|
||||
void UwURenderEngine::start()
|
||||
{
|
||||
World* world = core_.GetGameInstance()->GetWorld();
|
||||
std::vector<UType::object_ptr<Mesh>> meshes = world->GetActorsByClass<Mesh>();
|
||||
std::vector<std::weak_ptr<Mesh>> meshes = world->GetActorsByClass<Mesh>();
|
||||
for (auto& i : meshes)
|
||||
i->load_model(i->GetModelName());
|
||||
{
|
||||
std::shared_ptr<Mesh> mesh = i.lock();
|
||||
mesh->load_model(mesh->GetModelName());
|
||||
}
|
||||
|
||||
while (!glfwWindowShouldClose(get_window()))
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user