-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.h
executable file
·111 lines (88 loc) · 2.35 KB
/
pool.h
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// APK - Copyright (c) 2024 by Robin Southern. https://github.com/betajaen/apk
// Licensed under the MIT License; see LICENSE file.
#pragma once
#include "pod.h"
#include "assert.h"
#include "compat.h"
namespace apk {
template<typename T, ssize_t N>
class Pool {
T m_data[N];
public:
Pool()
{
memset(m_data, 0, sizeof(m_data));
for(uint32 i=0;i < N;i++) {
m_data[i].pool_index = -1;
}
}
~Pool() {
release();
}
Pool(const Pool& pool) {
copyFrom(pool);
}
Pool(Pool&& pool) {
moveFrom(pool);
}
Pool& operator=(const Pool& pool) {
if (&pool == this) {
return *this;
}
copyFrom(pool);
return *this;
}
Pool& operator=(Pool&& pool) {
if (&pool == this) {
return *this;
}
moveFrom(pool);
return *this;
}
void copyFrom(const Pool& other) {
release();
for(uint32_t i=0;i < N;i++) {
new ((void*) &m_data[i]) T(other[i]);
}
}
void moveFrom(Pool& other) {
release();
for(uint32_t i=0;i < N;i++) {
new ((void*) &m_data[i]) T(other.m_data[i]);
}
other.release();
}
void release() {
for(uint32_t i=0; i < N;i++) {
m_data[i].~T();
}
}
T* create() {
for(uint32 i=0; i < N;i++) {
T* item = &m_data[i];
if (item->pool_index == -1) {
item->pool_index = i;
return item;
}
}
T* item = apk_new T();
item->pool_index = -2;
return item;
}
void destroy(T* item) {
if (item->pool_index == -2) {
apk_delete(item);
return;
}
apk::memset(item, 0, sizeof(T));
item->pool_index = -1;
}
void destroyQuick(T* item) {
if (item->pool_index == -2) {
apk_delete(item);
return;
}
item->pool_index = -1;
}
};
}