-
Notifications
You must be signed in to change notification settings - Fork 5
/
ObjectPool.hpp
66 lines (53 loc) · 1.33 KB
/
ObjectPool.hpp
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
#ifndef ___INANITY_OBJECT_POOL_HPP___
#define ___INANITY_OBJECT_POOL_HPP___
#include "ChunkPool.hpp"
#include "PoolObject.hpp"
BEGIN_INANITY
/// Base class for pool of objects.
/** Objects should be inherited from PoolObject. */
class ObjectPoolBase : public ChunkPool
{
friend class PoolObject;
public:
ObjectPoolBase(size_t chunkSize) : ChunkPool(chunkSize) {}
/// Create new object, calling constructor with arguments.
template <typename T, typename... Args>
ptr<T> New(Args... args)
{
// allocate space for object
void* chunk = ChunkPool::Allocate();
// initialize object
T* object = new (chunk) T(args...);
// set pool to object
object->pool = this;
// return object (wrapping it into ptr)
return object;
}
private:
/// Free object.
/** Accessible from friend class PoolObject. */
void Free(PoolObject* object)
{
// hold a reference to itself, because
// object may hold a very last reference
ptr<ObjectPoolBase> self = this;
// destroy object
object->~PoolObject();
// free object's space
ChunkPool::Free(object);
}
};
/// Explicitly typed object pool.
template <typename T>
class ObjectPool : public ObjectPoolBase
{
public:
ObjectPool() : ObjectPoolBase(sizeof(T)) {}
template <typename... Args>
ptr<T> New(Args... args)
{
return ObjectPoolBase::New<T, Args...>(args...);
}
};
END_INANITY
#endif