-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_pool.hpp
171 lines (152 loc) · 3.66 KB
/
resource_pool.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* @file resource_pool.hpp
* @author Mihai Dragusin ([email protected])
* @brief [Header file] Generic resource pool with optional adaptor for resource initialization logic.
* @version 0.1
* @date 2022-08-18
*
* @copyright Copyright (c) 2022
*
*/
#include <iostream>
#include <deque>
#include <mutex>
#include <functional>
#include <memory>
template <typename T>
class PoolObject;
template <typename T>
class Pool
{
private:
/**
* @brief pool is made available if an optional initializer fn is passed.
*
*/
std::deque<T> pool;
/**
* @brief Allows concurrent access, private.
*
*/
std::mutex lock;
public:
/**
* @brief The constructor can take an allocator function as argument, capacity or both.
* Meant to be used for resources that require initialization, such as network sockets.
* @return void
*/
Pool();
/**
* @brief The constructor can take an allocator function as argument, capacity or both.
* Meant to be used for resources that require initialization, such as network sockets.
* @param capacity
* @return void
*/
Pool(int capacity);
std::function<void(T , std::deque<T> &)> addAlloc;
void _add(T t, std::deque<T> &p);
void add(T t);
/**
* @brief The constructor can take an allocator function as argument, capacity or both.
* Meant to be used for resources that require initialization, such as network sockets.
* @param TAllocAdd
* @return void
*/
Pool(const std::function<void(T , std::deque<T> &)> TAllocAdd);
/**
* @brief The constructor can take an allocator function as argument, capacity or both.
* Meant to be used for resources that require initialization, such as network sockets.
* @param capacity
* @param TAllocAdd
* @return void
*/
Pool(int capacity, const std::function<void(T , std::deque<T> &)> TAllocAdd);
T get();
PoolObject<T> getManaged();
};
// --- Implementation
template <typename T>
Pool<T>::Pool()
{
addAlloc = _add;
}
template <typename T>
Pool<T>::Pool(int capacity)
{
pool.resize(capacity);
addAlloc = _add;
}
template <typename T>
void Pool<T>::_add(T t, std::deque<T> &p)
{
std::lock_guard<std::mutex> guard(lock);
pool.push_back(t);
}
template <typename T>
void Pool<T>::add(T t)
{
std::lock_guard<std::mutex> guard(lock);
addAlloc(t, pool);
}
template <typename T>
Pool<T>::Pool(const std::function<void(T , std::deque<T> &)> TAllocAdd)
{
addAlloc = TAllocAdd;
}
template <typename T>
Pool<T>::Pool(int capacity, const std::function<void(T , std::deque<T> &)> TAllocAdd)
{
pool.resize(capacity);
addAlloc = TAllocAdd;
}
template <typename T>
T Pool<T>::get()
{
std::lock_guard<std::mutex> guard(lock);
T &t = pool.back();
pool.pop_back();
return std::move(t);
}
/**
* @brief Class that handles recycling back to the pool.
*
*/
template <typename T>
class PoolObject {
public:
T value;
Pool<T> *pool;
/**
* @brief Construct a new Pool Object
*
* @param t
* @param p
*/
PoolObject(T t, Pool<T> *p) {
value = t;
pool = p;
}
/**
* @brief Recycle the Pool Object
*
*/
~PoolObject() {
pool->add(value);
}
/**
* @brief overloaded * simplifies usage of the object to be used as *o instead of *o.value
*
* @return T
*/
T operator *() {
return this->value;
}
};
/**
* @brief Returns a managed object class of type T which handles recycling T back to the pool
*
*/
template <typename T>
PoolObject<T> Pool<T>::getManaged() {
return PoolObject<T>(this->get(),this);
}