Embedded Template Library 1.0
pool.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2014 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_POOL_INCLUDED
32#define ETL_POOL_INCLUDED
33
34#include "platform.h"
35#include "ipool.h"
36#include "generic_pool.h"
37
38#define ETL_POOL_CPP03_CODE 0
39
40//*****************************************************************************
44//*****************************************************************************
45
46namespace etl
47{
48 //*************************************************************************
51 //*************************************************************************
52 template <typename T, const size_t VSize>
53 class pool : public etl::generic_pool<sizeof(T), etl::alignment_of<T>::value, VSize>
54 {
55 private:
56
57 typedef etl::generic_pool<sizeof(T), etl::alignment_of<T>::value, VSize> base_t;
58
59 public:
60
61 using base_t::SIZE;
62 using base_t::ALIGNMENT;
63 using base_t::TYPE_SIZE;
64
65 //*************************************************************************
67 //*************************************************************************
69 {
70 }
71
72 //*************************************************************************
78 //*************************************************************************
80 {
81 return base_t::template allocate<T>();
82 }
83
84#if ETL_CPP11_NOT_SUPPORTED || ETL_POOL_CPP03_CODE || ETL_USING_STLPORT
85 //*************************************************************************
89 //*************************************************************************
90 T* create()
91 {
92 return base_t::template create<T>();
93 }
94
95 //*************************************************************************
99 //*************************************************************************
100 template <typename T1>
101 T* create(const T1& value1)
102 {
103 return base_t::template create<T>(value1);
104 }
105
106 //*************************************************************************
110 //*************************************************************************
111 template <typename T1, typename T2>
112 T* create(const T1& value1, const T2& value2)
113 {
114 return base_t::template create<T>(value1, value2);
115 }
116
117 //*************************************************************************
121 //*************************************************************************
122 template <typename T1, typename T2, typename T3>
123 T* create(const T1& value1, const T2& value2, const T3& value3)
124 {
125 return base_t::template create<T>(value1, value2, value3);
126 }
127
128 //*************************************************************************
132 //*************************************************************************
133 template <typename T1, typename T2, typename T3, typename T4>
134 T* create(const T1& value1, const T2& value2, const T3& value3, const T4& value4)
135 {
136 return base_t::template create<T>(value1, value2, value3, value4);
137 }
138#else
139 //*************************************************************************
143 //*************************************************************************
144 template <typename... Args>
145 T* create(Args&&... args)
146 {
147 return base_t::template create<T>(etl::forward<Args>(args)...);
148 }
149#endif
150
151 //*************************************************************************
155 //*************************************************************************
156 template <typename U>
157 void release(const U* const p_object)
158 {
159 ETL_STATIC_ASSERT((etl::is_same<U, T>::value || etl::is_base_of<U, T>::value), "Pool does not contain this type");
160 base_t::release(p_object);
161 }
162
163 //*************************************************************************
167 //*************************************************************************
168 template <typename U>
169 void destroy(const U* const p_object)
170 {
171 ETL_STATIC_ASSERT((etl::is_base_of<U, T>::value), "Pool does not contain this type");
172 base_t::destroy(p_object);
173 }
174
175 private:
176
177 // Should not be copied.
178 pool(const pool&) ETL_DELETE;
179 pool& operator =(const pool&) ETL_DELETE;
180 };
181
182 //*************************************************************************
186 //*************************************************************************
187 template <typename T>
188 class pool_ext : public etl::generic_pool_ext<sizeof(T), etl::alignment_of<T>::value>
189 {
190 private:
192
193 public:
194 using base_t::ALIGNMENT;
195 using base_t::TYPE_SIZE;
196
197 //*************************************************************************
199 //*************************************************************************
200 pool_ext(typename base_t::element* buffer, size_t size)
201 : base_t(buffer, size)
202 {
203 }
204
205 //*************************************************************************
211 //*************************************************************************
213 {
214 return base_t::template allocate<T>();
215 }
216
217#if ETL_CPP11_NOT_SUPPORTED || ETL_POOL_CPP03_CODE || ETL_USING_STLPORT
218 //*************************************************************************
222 //*************************************************************************
223 T* create()
224 {
225 return base_t::template create<T>();
226 }
227
228 //*************************************************************************
232 //*************************************************************************
233 template <typename T1>
234 T* create(const T1& value1)
235 {
236 return base_t::template create<T>(value1);
237 }
238
239 //*************************************************************************
243 //*************************************************************************
244 template <typename T1, typename T2>
245 T* create(const T1& value1, const T2& value2)
246 {
247 return base_t::template create<T>(value1, value2);
248 }
249
250 //*************************************************************************
254 //*************************************************************************
255 template <typename T1, typename T2, typename T3>
256 T* create(const T1& value1, const T2& value2, const T3& value3)
257 {
258 return base_t::template create<T>(value1, value2, value3);
259 }
260
261 //*************************************************************************
265 //*************************************************************************
266 template <typename T1, typename T2, typename T3, typename T4>
267 T* create(const T1& value1, const T2& value2, const T3& value3, const T4& value4)
268 {
269 return base_t::template create<T>(value1, value2, value3, value4);
270 }
271#else
272 //*************************************************************************
276 //*************************************************************************
277 template <typename... Args>
278 T* create(Args&&... args)
279 {
280 return base_t::template create<T>(etl::forward<Args>(args)...);
281 }
282#endif
283
284 //*************************************************************************
288 //*************************************************************************
289 template <typename U>
290 void release(const U* const p_object)
291 {
292 ETL_STATIC_ASSERT((etl::is_same<U, T>::value || etl::is_base_of<U, T>::value), "Pool does not contain this type");
293 base_t::release(p_object);
294 }
295
296 //*************************************************************************
300 //*************************************************************************
301 template <typename U>
302 void destroy(const U* const p_object)
303 {
304 ETL_STATIC_ASSERT((etl::is_base_of<U, T>::value), "Pool does not contain this type");
305 base_t::destroy(p_object);
306 }
307
308 private:
309 // Should not be copied.
310 pool_ext(const pool_ext&) ETL_DELETE;
311 pool_ext& operator=(const pool_ext&) ETL_DELETE;
312 };
313}
314
315#endif
316
size_t size() const
Returns the number of allocated items in the pool.
Definition: ipool.h:293
pool_ext(typename base_t::element *buffer, size_t size)
Constructor.
Definition: pool.h:200
T * allocate()
Definition: pool.h:79
T * allocate()
Definition: pool.h:212
T * create(const T1 &value1, const T2 &value2, const T3 &value3, const T4 &value4)
Definition: pool.h:134
void release(const U *const p_object)
Definition: pool.h:290
T * create(const T1 &value1, const T2 &value2)
Definition: pool.h:112
void release(const void *const p_object)
Definition: ipool.h:239
T * create(const T1 &value1)
Definition: pool.h:234
T * create(const T1 &value1)
Definition: pool.h:101
void destroy(const U *const p_object)
Definition: pool.h:302
T * create()
Definition: pool.h:90
void destroy(const U *const p_object)
Definition: pool.h:169
void destroy(const U *const p_object)
Definition: generic_pool.h:169
T * create(const T1 &value1, const T2 &value2)
Definition: pool.h:245
T * create(const T1 &value1, const T2 &value2, const T3 &value3)
Definition: pool.h:123
T * create()
Definition: pool.h:223
T * create(const T1 &value1, const T2 &value2, const T3 &value3)
Definition: pool.h:256
pool()
Constructor.
Definition: pool.h:68
void release(const U *const p_object)
Definition: pool.h:157
T * create(const T1 &value1, const T2 &value2, const T3 &value3, const T4 &value4)
Definition: pool.h:267
void destroy(const U *const p_object)
Definition: generic_pool.h:337
Definition: generic_pool.h:56
Definition: generic_pool.h:213
Definition: pool.h:54
Definition: pool.h:189
add_rvalue_reference
Definition: type_traits_generator.h:1327
is_base_of
Definition: type_traits_generator.h:1252
is_same
Definition: type_traits_generator.h:1041
bitset_ext
Definition: absolute.h:38
Definition: alignment.h:223