-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathDefaultObjectsStorage.cpp
More file actions
65 lines (53 loc) · 1.56 KB
/
DefaultObjectsStorage.cpp
File metadata and controls
65 lines (53 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "DefaultObjectsStorage.h"
#include <algorithm>
namespace Patterns
{
DefaultObjectsStorage::DefaultObjectsStorage()
{
// Recalculate hash
// Add to cache
}
GameGlobalObject* DefaultObjectsStorage::GetGlobalObjectImpl(size_t i_type_code) const
{
for (auto* p_object : m_cache_objects)
{
if (p_object->GetTypeHashCode() == i_type_code)
return p_object;
}
return nullptr;
}
void DefaultObjectsStorage::AddGlobalObjectImpl(std::unique_ptr<GameGlobalObject> ip_object)
{
#if defined(_DEBUG)
if (GetGlobalObjectImpl(ip_object->GetTypeHashCode()) != nullptr)
{
assert(false);
return;
}
#endif
// cache object and add it to pool
m_cache_objects.push_back(ip_object.get());
m_dynamic_objects.emplace_back(std::move(ip_object));
}
void DefaultObjectsStorage::RemoveGlobalObjectImpl(size_t i_type_code)
{
const auto it_dyn = std::find_if(m_dynamic_objects.begin(), m_dynamic_objects.end(), [i_type_code](ObjPtr& p_obj)
{
return p_obj->GetTypeHashCode() == i_type_code;
});
// we can delete from cache only dynamic objects - not static which are defined inside this class
// and will be removed with destruction of getter
if (it_dyn != m_dynamic_objects.end())
{
// remove from cache
const auto it = std::find_if(m_cache_objects.begin(), m_cache_objects.end(), [i_type_code](GameGlobalObject* p_obj)
{
return p_obj->GetTypeHashCode() == i_type_code;
});
it_dyn->reset(nullptr);
if (it != m_cache_objects.end())
m_cache_objects.erase(it);
m_dynamic_objects.erase(it_dyn);
}
}
} // Patterns