-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPool.h
66 lines (53 loc) · 940 Bytes
/
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
#pragma once
#include <vector>
template<typename T>
class Pool
{
public:
typedef uint8_t Size;
Pool(Size capacity);
virtual ~Pool();
bool isEmpty() const;
protected:
std::vector<T *> objects_;
Size capacity_;
Size numLive_;
void add_(T *object);
void remove_(Size index);
bool isFull_() const;
};
template<typename T>
Pool<T>::Pool(Size capacity) :
objects_(capacity, nullptr),
capacity_(capacity)
{}
template<typename T>
Pool<T>::~Pool()
{
for (Size i = 0; i < numLive_; ++i)
{
delete objects_[i];
}
}
template<typename T>
bool Pool<T>::isEmpty() const
{
return numLive_ == 0;
}
template<typename T>
void Pool<T>::add_(T *object)
{
objects_[numLive_++] = object;
}
template<typename T>
void Pool<T>::remove_(Size index)
{
delete objects_[index];
objects_[index] = objects_[--numLive_];
objects_[numLive_] = nullptr;
}
template<typename T>
bool Pool<T>::isFull_() const
{
return numLive_ >= capacity_;
}