feat: use shared_ptr for object state

This commit is contained in:
k0t9i 2023-01-23 23:40:51 +04:00
parent 3ccdf1d9e4
commit 032581512d
9 changed files with 19 additions and 22 deletions

View File

@ -1,5 +1,6 @@
#pragma once
#include <vector>
#include <memory>
#include "../Entities/WorldObject.h"
#include "../DTO/EntityState.h"
@ -8,7 +9,7 @@ namespace L2Bot::Domain::Repositories
class EntityRepositoryInterface
{
public:
virtual const std::vector<DTO::EntityState*> GetEntities() = 0;
virtual const std::vector<std::shared_ptr<DTO::EntityState>> GetEntities() = 0;
virtual void Reset() = 0;
};
}

View File

@ -42,7 +42,7 @@ namespace L2Bot::Domain::Serializers
return result;
}
SerializableStateContainer(const std::vector<DTO::EntityState*> objects, const std::string containerName) :
SerializableStateContainer(const std::vector<std::shared_ptr<DTO::EntityState>> objects, const std::string containerName) :
m_Objects(objects), m_ContainerName(containerName)
{
@ -50,7 +50,7 @@ namespace L2Bot::Domain::Serializers
SerializableStateContainer() = delete;
virtual ~SerializableStateContainer() = default;
private:
const std::vector<DTO::EntityState*> m_Objects;
const std::vector<std::shared_ptr<DTO::EntityState>> m_Objects;
const std::string m_ContainerName;
};
}

View File

@ -19,7 +19,7 @@ namespace L2Bot::Domain::Services
EntityService() = delete;
virtual ~EntityService() = default;
virtual const std::vector<DTO::EntityState*> GetEntities()
virtual const std::vector<std::shared_ptr<DTO::EntityState>> GetEntities()
{
return m_Repository.GetEntities();
}

View File

@ -12,7 +12,7 @@ class EntityHandler
{
public:
template<typename T>
const std::map<uint32_t, DTO::EntityState*> GetEntities(const std::map<uint32_t, T> items, std::function<std::unique_ptr<Entities::EntityInterface>(T)> callback)
const std::map<uint32_t, std::shared_ptr<DTO::EntityState>> GetEntities(const std::map<uint32_t, T> items, std::function<std::unique_ptr<Entities::EntityInterface>(T)> callback)
{
RemoveOutdatedStates();
@ -36,7 +36,7 @@ public:
const auto objectId = newObject->GetId();
m_Objects.emplace(
objectId,
new DTO::EntityState{ std::move(newObject), Enums::EntityStateEnum::created }
std::make_shared<DTO::EntityState>(std::move(newObject), Enums::EntityStateEnum::created)
);
}
}
@ -55,10 +55,6 @@ public:
void Reset()
{
for (const auto& object : m_Objects)
{
delete object.second;
}
m_Objects.clear();
}
@ -75,7 +71,6 @@ private:
{
if (it->second->GetState() == Enums::EntityStateEnum::deleted)
{
delete it->second;
m_Objects.erase(it++);
}
else
@ -86,5 +81,5 @@ private:
}
private:
std::map<uint32_t, DTO::EntityState*> m_Objects;
std::map<uint32_t, std::shared_ptr<DTO::EntityState>> m_Objects;
};

View File

@ -1,6 +1,7 @@
#pragma once
#include <map>
#include <memory>
#include "Domain/Repositories/EntityRepositoryInterface.h"
#include "Domain/DTO/EntityState.h"
#include "../Factories/DropFactory.h"
@ -15,7 +16,7 @@ namespace Interlude
class DropRepository : public Repositories::EntityRepositoryInterface, public FindObjectsTrait
{
public:
const std::vector<DTO::EntityState*> GetEntities() override
const std::vector<std::shared_ptr<DTO::EntityState>> GetEntities() override
{
const std::map<uint32_t, Item*> items = FindAllObjects<Item*>(m_Radius, [this](float_t radius, int32_t prevId) {
return m_NetworkHandler.GetNextItem(radius, prevId);
@ -24,7 +25,7 @@ namespace Interlude
return m_Factory.Create(item);
});
auto result = std::vector<DTO::EntityState*>();
auto result = std::vector<std::shared_ptr<DTO::EntityState>>();
for (const auto kvp : objects)
{

View File

@ -15,7 +15,7 @@ namespace Interlude
class HeroRepository : public Repositories::EntityRepositoryInterface
{
public:
const std::vector<DTO::EntityState*> GetEntities() override
const std::vector<std::shared_ptr<DTO::EntityState>> GetEntities() override
{
auto hero = m_NetworkHandler.GetHero();
@ -41,7 +41,7 @@ namespace Interlude
return m_Factory.Create(item);
});
auto result = std::vector<DTO::EntityState*>();
auto result = std::vector<std::shared_ptr<DTO::EntityState>>();
for (const auto kvp : objects)
{

View File

@ -17,7 +17,7 @@ namespace Interlude
class NPCRepository : public Repositories::EntityRepositoryInterface, public FindObjectsTrait
{
public:
const std::vector<DTO::EntityState*> GetEntities() override
const std::vector<std::shared_ptr<DTO::EntityState>> GetEntities() override
{
const auto creatures = FindAllObjects<User*>(m_Radius, [this](float_t radius, int32_t prevId) {
return m_NetworkHandler.GetNextCreature(radius, prevId);
@ -37,7 +37,7 @@ namespace Interlude
return m_Factory.Create(item, spoilState);
});
auto result = std::vector<DTO::EntityState*>();
auto result = std::vector<std::shared_ptr<DTO::EntityState>>();
for (const auto kvp : objects)
{

View File

@ -13,7 +13,7 @@ namespace Interlude
class PlayerRepository : public Repositories::EntityRepositoryInterface, public FindObjectsTrait
{
public:
const std::vector<DTO::EntityState*> GetEntities() override
const std::vector<std::shared_ptr<DTO::EntityState>> GetEntities() override
{
const auto creatures = FindAllObjects<User*>(m_Radius, [this](float_t radius, int32_t prevId) {
return m_NetworkHandler.GetNextCreature(radius, prevId);
@ -32,7 +32,7 @@ namespace Interlude
return m_Factory.Create(item);
});
auto result = std::vector<DTO::EntityState*>();
auto result = std::vector<std::shared_ptr<DTO::EntityState>>();
for (const auto kvp : objects)
{

View File

@ -22,7 +22,7 @@ namespace Interlude
class SkillRepository : public Repositories::EntityRepositoryInterface
{
public:
const std::vector<DTO::EntityState*> GetEntities() override
const std::vector<std::shared_ptr<DTO::EntityState>> GetEntities() override
{
std::unique_lock<std::shared_timed_mutex>(m_Mutex);
@ -36,7 +36,7 @@ namespace Interlude
return std::make_unique<Entities::Skill>(item);
});
auto result = std::vector<DTO::EntityState*>();
auto result = std::vector<std::shared_ptr<DTO::EntityState>>();
for (const auto kvp : objects)
{