feat: use shared_ptr for object state
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <memory>
|
||||||
#include "../Entities/WorldObject.h"
|
#include "../Entities/WorldObject.h"
|
||||||
#include "../DTO/EntityState.h"
|
#include "../DTO/EntityState.h"
|
||||||
|
|
||||||
@@ -8,7 +9,7 @@ namespace L2Bot::Domain::Repositories
|
|||||||
class EntityRepositoryInterface
|
class EntityRepositoryInterface
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual const std::vector<DTO::EntityState*> GetEntities() = 0;
|
virtual const std::vector<std::shared_ptr<DTO::EntityState>> GetEntities() = 0;
|
||||||
virtual void Reset() = 0;
|
virtual void Reset() = 0;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@@ -42,7 +42,7 @@ namespace L2Bot::Domain::Serializers
|
|||||||
return result;
|
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)
|
m_Objects(objects), m_ContainerName(containerName)
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -50,7 +50,7 @@ namespace L2Bot::Domain::Serializers
|
|||||||
SerializableStateContainer() = delete;
|
SerializableStateContainer() = delete;
|
||||||
virtual ~SerializableStateContainer() = default;
|
virtual ~SerializableStateContainer() = default;
|
||||||
private:
|
private:
|
||||||
const std::vector<DTO::EntityState*> m_Objects;
|
const std::vector<std::shared_ptr<DTO::EntityState>> m_Objects;
|
||||||
const std::string m_ContainerName;
|
const std::string m_ContainerName;
|
||||||
};
|
};
|
||||||
}
|
}
|
@@ -19,7 +19,7 @@ namespace L2Bot::Domain::Services
|
|||||||
EntityService() = delete;
|
EntityService() = delete;
|
||||||
virtual ~EntityService() = default;
|
virtual ~EntityService() = default;
|
||||||
|
|
||||||
virtual const std::vector<DTO::EntityState*> GetEntities()
|
virtual const std::vector<std::shared_ptr<DTO::EntityState>> GetEntities()
|
||||||
{
|
{
|
||||||
return m_Repository.GetEntities();
|
return m_Repository.GetEntities();
|
||||||
}
|
}
|
||||||
|
@@ -12,7 +12,7 @@ class EntityHandler
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
template<typename T>
|
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();
|
RemoveOutdatedStates();
|
||||||
|
|
||||||
@@ -36,7 +36,7 @@ public:
|
|||||||
const auto objectId = newObject->GetId();
|
const auto objectId = newObject->GetId();
|
||||||
m_Objects.emplace(
|
m_Objects.emplace(
|
||||||
objectId,
|
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()
|
void Reset()
|
||||||
{
|
{
|
||||||
for (const auto& object : m_Objects)
|
|
||||||
{
|
|
||||||
delete object.second;
|
|
||||||
}
|
|
||||||
m_Objects.clear();
|
m_Objects.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,7 +71,6 @@ private:
|
|||||||
{
|
{
|
||||||
if (it->second->GetState() == Enums::EntityStateEnum::deleted)
|
if (it->second->GetState() == Enums::EntityStateEnum::deleted)
|
||||||
{
|
{
|
||||||
delete it->second;
|
|
||||||
m_Objects.erase(it++);
|
m_Objects.erase(it++);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -86,5 +81,5 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::map<uint32_t, DTO::EntityState*> m_Objects;
|
std::map<uint32_t, std::shared_ptr<DTO::EntityState>> m_Objects;
|
||||||
};
|
};
|
@@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <memory>
|
||||||
#include "Domain/Repositories/EntityRepositoryInterface.h"
|
#include "Domain/Repositories/EntityRepositoryInterface.h"
|
||||||
#include "Domain/DTO/EntityState.h"
|
#include "Domain/DTO/EntityState.h"
|
||||||
#include "../Factories/DropFactory.h"
|
#include "../Factories/DropFactory.h"
|
||||||
@@ -15,7 +16,7 @@ namespace Interlude
|
|||||||
class DropRepository : public Repositories::EntityRepositoryInterface, public FindObjectsTrait
|
class DropRepository : public Repositories::EntityRepositoryInterface, public FindObjectsTrait
|
||||||
{
|
{
|
||||||
public:
|
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) {
|
const std::map<uint32_t, Item*> items = FindAllObjects<Item*>(m_Radius, [this](float_t radius, int32_t prevId) {
|
||||||
return m_NetworkHandler.GetNextItem(radius, prevId);
|
return m_NetworkHandler.GetNextItem(radius, prevId);
|
||||||
@@ -24,7 +25,7 @@ namespace Interlude
|
|||||||
return m_Factory.Create(item);
|
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)
|
for (const auto kvp : objects)
|
||||||
{
|
{
|
||||||
|
@@ -15,7 +15,7 @@ namespace Interlude
|
|||||||
class HeroRepository : public Repositories::EntityRepositoryInterface
|
class HeroRepository : public Repositories::EntityRepositoryInterface
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
const std::vector<DTO::EntityState*> GetEntities() override
|
const std::vector<std::shared_ptr<DTO::EntityState>> GetEntities() override
|
||||||
{
|
{
|
||||||
|
|
||||||
auto hero = m_NetworkHandler.GetHero();
|
auto hero = m_NetworkHandler.GetHero();
|
||||||
@@ -41,7 +41,7 @@ namespace Interlude
|
|||||||
return m_Factory.Create(item);
|
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)
|
for (const auto kvp : objects)
|
||||||
{
|
{
|
||||||
|
@@ -17,7 +17,7 @@ namespace Interlude
|
|||||||
class NPCRepository : public Repositories::EntityRepositoryInterface, public FindObjectsTrait
|
class NPCRepository : public Repositories::EntityRepositoryInterface, public FindObjectsTrait
|
||||||
{
|
{
|
||||||
public:
|
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) {
|
const auto creatures = FindAllObjects<User*>(m_Radius, [this](float_t radius, int32_t prevId) {
|
||||||
return m_NetworkHandler.GetNextCreature(radius, prevId);
|
return m_NetworkHandler.GetNextCreature(radius, prevId);
|
||||||
@@ -37,7 +37,7 @@ namespace Interlude
|
|||||||
return m_Factory.Create(item, spoilState);
|
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)
|
for (const auto kvp : objects)
|
||||||
{
|
{
|
||||||
|
@@ -13,7 +13,7 @@ namespace Interlude
|
|||||||
class PlayerRepository : public Repositories::EntityRepositoryInterface, public FindObjectsTrait
|
class PlayerRepository : public Repositories::EntityRepositoryInterface, public FindObjectsTrait
|
||||||
{
|
{
|
||||||
public:
|
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) {
|
const auto creatures = FindAllObjects<User*>(m_Radius, [this](float_t radius, int32_t prevId) {
|
||||||
return m_NetworkHandler.GetNextCreature(radius, prevId);
|
return m_NetworkHandler.GetNextCreature(radius, prevId);
|
||||||
@@ -32,7 +32,7 @@ namespace Interlude
|
|||||||
return m_Factory.Create(item);
|
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)
|
for (const auto kvp : objects)
|
||||||
{
|
{
|
||||||
|
@@ -22,7 +22,7 @@ namespace Interlude
|
|||||||
class SkillRepository : public Repositories::EntityRepositoryInterface
|
class SkillRepository : public Repositories::EntityRepositoryInterface
|
||||||
{
|
{
|
||||||
public:
|
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);
|
std::unique_lock<std::shared_timed_mutex>(m_Mutex);
|
||||||
|
|
||||||
@@ -36,7 +36,7 @@ namespace Interlude
|
|||||||
return std::make_unique<Entities::Skill>(item);
|
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)
|
for (const auto kvp : objects)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user