forked from Shell4026/ShellEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleton.cpp
More file actions
43 lines (37 loc) · 960 Bytes
/
Copy pathSingleton.cpp
File metadata and controls
43 lines (37 loc) · 960 Bytes
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
#include "PCH.h"
#include "Singleton.hpp"
#include <cstring>
namespace sh::core
{
std::mutex ISingleton::mu{};
std::vector<std::pair<uint64_t, void*>> ISingleton::instance{};
auto ISingleton::CreateInstance(uint64_t hash, std::size_t size) -> Result
{
std::lock_guard<std::mutex> lock{ mu };
for (auto& [instanceHash, instancePtr] : instance)
{
if(instanceHash == hash)
return Result{ instancePtr, false };
}
void* ptr = ::operator new(size);
std::memset(ptr, 0, size);
instance.push_back({ hash, ptr });
return Result{ ptr, true };
}
void ISingleton::DeleteInstance(uint64_t hash)
{
std::lock_guard<std::mutex> lock{ mu };
for (int i = 0; i < instance.size(); ++i)
{
uint64_t instanceHash = instance[i].first;
void* instancePtr = instance[i].second;
if (instanceHash == hash)
{
::operator delete(instancePtr);
instance.erase(instance.begin() + i);
return;
}
}
return;
}
}//namespace