-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPool.hh
More file actions
135 lines (120 loc) · 2.7 KB
/
Pool.hh
File metadata and controls
135 lines (120 loc) · 2.7 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#ifndef _POOL_HH_
# define _POOL_HH_
# include <vector>
# include <typeinfo>
# include <memory>
# include <map>
# include <list>
# include <stdexcept>
# include <iostream>
# include <algorithm>
# include "PoolObject.hh"
class Factory
{
public:
Factory(void){};
~Factory(void){
for (auto item : _collection)
delete item.second;
};
public:
template <typename T>
void addObjectType(void)
{
_collection.push_back(std::make_pair(typeid(T).name(), new T()));
}
template <typename T>
T *create(void) const
{
for (auto it : _collection)
{
if (it.first == typeid(T).name())
return (static_cast<T*>(it.second->clone()));
}
std::cerr << "Factory : can't create this object (" << typeid(T).name() << " doesn't exist)" << std::endl;
return nullptr;
}
protected:
std::vector<std::pair<const char *, PoolObject*>> _collection;
};
class Pool
{
public:
enum PoolType
{
AUTO_EXTEND = 0,
FIXED
};
public:
Pool(const PoolType &p = AUTO_EXTEND) : _type(p) {}
virtual ~Pool(void){
for (auto item : _collection)
for (auto object : item.second)
delete object;
}
public:
template <typename T>
void configure(unsigned int size)
{
_factory.addObjectType<T>();
for (unsigned int i = 0; i != size; ++i)
{
T *ptr = _factory.create<T>();
if (ptr)
_collection[typeid(T).name()].push_back(ptr);
}
}
template <typename T>
unsigned int size(void)
{
const char *type = typeid(T).name();
if (_collection.find(type) != _collection.end())
return _collection[type].size();
return 0;
}
template <typename T>
T *get(void)
{
const char *type = typeid(T).name();
auto it = _collection.find(type);
if (it != _collection.end())
{
T *ptr = nullptr;
if (!_collection[type].empty())
{
ptr = static_cast<T *>(_collection[type].front());
_collection[type].pop_front();
}
else if (_type == AUTO_EXTEND) {
ptr = _factory.create<T>();
}
return ptr;
}
else
{
std::string typeStr(type);
throw std::logic_error("Pool : can't return an object of type " + typeStr);
}
}
template <typename T>
bool release(T* ptr)
{
const char *type = typeid(T).name();
auto found = find_if(_collection[type].begin(), _collection[type].end(),
[ptr](PoolObject *t) -> bool { return (ptr == t); });
auto it = _collection.find(type);
if (found == _collection[type].end() && it != _collection.end() && ptr)
{
ptr->reset();
_collection[type].push_back(ptr);
return true;
}
std::cerr << "Pool : can't release an object of type " + std::string(type) + " or a non-valid pointer" << std::endl;
return false;
}
protected:
std::map<const char *, std::list<PoolObject*>> _collection;
Factory _factory;
const PoolType _type;
};
#endif /*!_POOL_HH_*/