Embedded Template Library  1.0
variant_pool.h
1 /******************************************************************************
2 The MIT License(MIT)
3 
4 Embedded Template Library.
5 https://github.com/ETLCPP/etl
6 https://www.etlcpp.com
7 
8 Copyright(c) 2017 jwellbelove
9 
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files(the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions :
16 
17 The above copyright notice and this permission notice shall be included in all
18 copies or substantial portions of the Software.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 SOFTWARE.
27 ******************************************************************************/
28 
29 #if 0
30 #error THIS HEADER IS A GENERATOR. DO NOT INCLUDE.
31 #endif
32 
33 //***************************************************************************
34 // THIS FILE HAS BEEN AUTO GENERATED. DO NOT EDIT THIS FILE.
35 //***************************************************************************
36 
37 //***************************************************************************
38 // To generate to header file, run this at the command line.
39 // Note: You will need Python and COG installed.
40 //
41 // python -m cogapp -d -e -ovariant_pool.h -DNTypes=<n> variant_pool_generator.h
42 // Where <n> is the number of types to support.
43 //
44 // e.g.
45 // To generate handlers for up to 16 types...
46 // python -m cogapp -d -e -ovariant_pool.h -DNTypes=16 variant_pool_generator.h
47 //
48 // See generate.bat
49 //***************************************************************************
50 
51 #ifndef ETL_VARIANT_POOL_INCLUDED
52 #define ETL_VARIANT_POOL_INCLUDED
53 
54 #include <stdint.h>
55 
56 #include "platform.h"
57 #include "error_handler.h"
58 #include "exception.h"
59 #include "largest.h"
60 #include "type_traits.h"
61 #include "alignment.h"
62 #include "static_assert.h"
63 #include "type_lookup.h"
64 #include "pool.h"
65 
66 #include "utility.h"
67 
68 #undef ETL_FILE
69 #define ETL_FILE "40"
70 
71 namespace etl
72 {
73  //***************************************************************************
74  class variant_pool_exception : public etl::exception
75  {
76  public:
77 
78  variant_pool_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
79  : exception(reason_, file_name_, line_number_)
80  {
81  }
82  };
83 
84  //***************************************************************************
85  class variant_pool_cannot_create : public etl::variant_pool_exception
86  {
87  public:
88 
89  variant_pool_cannot_create(string_type file_name_, numeric_type line_number_)
90  : variant_pool_exception(ETL_ERROR_TEXT("variant_pool:cannot create", ETL_FILE"A"), file_name_, line_number_)
91  {
92  }
93  };
94 
95  //***************************************************************************
96  class variant_pool_did_not_create : public etl::variant_pool_exception
97  {
98  public:
99 
100  variant_pool_did_not_create(string_type file_name_, numeric_type line_number_)
101  : variant_pool_exception(ETL_ERROR_TEXT("variant_pool:did not create", ETL_FILE"B"), file_name_, line_number_)
102  {
103  }
104  };
105 
106  //***************************************************************************
107  template <const size_t MAX_SIZE_,
108  typename T1,
109  typename T2 = void,
110  typename T3 = void,
111  typename T4 = void,
112  typename T5 = void,
113  typename T6 = void,
114  typename T7 = void,
115  typename T8 = void,
116  typename T9 = void,
117  typename T10 = void,
118  typename T11 = void,
119  typename T12 = void,
120  typename T13 = void,
121  typename T14 = void,
122  typename T15 = void,
123  typename T16 = void>
124  class variant_pool
125  {
126  public:
127 
128  static const size_t MAX_SIZE = MAX_SIZE_;
129 
130  //*************************************************************************
132  //*************************************************************************
134  {
135  }
136 
137 #if ETL_CPP11_NOT_SUPPORTED || ETL_USING_STLPORT
138  //*************************************************************************
140  //*************************************************************************
141  template <typename T>
142  T* create()
143  {
145 
146  T* p = ETL_NULLPTR;
147 
148  if (pool.full())
149  {
150  ETL_ASSERT(false, ETL_ERROR(etl::variant_pool_cannot_create));
151  }
152  else
153  {
154  p = pool.template allocate<T>();
155 
156  if (p != ETL_NULLPTR)
157  {
158  new (p) T();
159  }
160  }
161 
162  return p;
163  }
164 
165  //*************************************************************************
167  //*************************************************************************
168  template <typename T, typename TP1>
169  T* create(const TP1& p1)
170  {
172 
173  T* p = ETL_NULLPTR;
174 
175  if (pool.full())
176  {
177  ETL_ASSERT(false, ETL_ERROR(etl::variant_pool_cannot_create));
178  }
179  else
180  {
181  p = pool.template allocate<T>();
182 
183  if (p != ETL_NULLPTR)
184  {
185  new (p) T(p1);
186  }
187  }
188 
189  return p;
190  }
191 
192  //*************************************************************************
194  //*************************************************************************
195  template <typename T, typename TP1, typename TP2>
196  T* create(const TP1& p1, const TP2& p2)
197  {
199 
200  T* p = ETL_NULLPTR;
201 
202  if (pool.full())
203  {
204  ETL_ASSERT(false, ETL_ERROR(etl::variant_pool_cannot_create));
205  }
206  else
207  {
208  p = pool.template allocate<T>();
209 
210  if (p != ETL_NULLPTR)
211  {
212  new (p) T(p1, p2);
213  }
214  }
215 
216  return p;
217  }
218 
219  //*************************************************************************
221  //*************************************************************************
222  template <typename T, typename TP1, typename TP2, typename TP3>
223  T* create(const TP1& p1, const TP2& p2, const TP3& p3)
224  {
226 
227  T* p = ETL_NULLPTR;
228 
229  if (pool.full())
230  {
231  ETL_ASSERT(false, ETL_ERROR(etl::variant_pool_cannot_create));
232  }
233  else
234  {
235  p = pool.template allocate<T>();
236 
237  if (p != ETL_NULLPTR)
238  {
239  new (p) T(p1, p2, p3);
240  }
241  }
242 
243  return p;
244  }
245 
246  //*************************************************************************
248  //*************************************************************************
249  template <typename T, typename TP1, typename TP2, typename TP3, typename TP4>
250  T* create(const TP1& p1, const TP2& p2, const TP3& p3, const TP4& p4)
251  {
253 
254  T* p = ETL_NULLPTR;
255 
256  if (pool.full())
257  {
258  ETL_ASSERT(false, ETL_ERROR(etl::variant_pool_cannot_create));
259  }
260  else
261  {
262  p = pool.template allocate<T>();
263 
264  if (p != ETL_NULLPTR)
265  {
266  new (p) T(p1, p2, p3, p4);
267  }
268  }
269 
270  return p;
271  }
272 #else
273  //*************************************************************************
275  //*************************************************************************
276  template <typename T, typename... Args>
277  T* create(Args&&... args)
278  {
280 
281  T* p = ETL_NULLPTR;
282 
283  if (pool.full())
284  {
285  ETL_ASSERT(false, ETL_ERROR(etl::variant_pool_cannot_create));
286  }
287  else
288  {
289  p = pool.template allocate<T>();
290 
291  if (p != ETL_NULLPTR)
292  {
293  new (p) T(etl::forward<Args>(args)...);
294  }
295  }
296 
297  return p;
298  }
299 #endif
300 
301  //*************************************************************************
303  //*************************************************************************
304  template <typename T>
305  bool destroy(const T* const p)
306  {
323  etl::is_base_of<T, T16>::value), "Invalid type");
324 
325  p->~T();
326 
327  void* vp = reinterpret_cast<char*>(const_cast<T*>(p));
328 
329  if (pool.is_in_pool(vp))
330  {
331  pool.release(vp);
332  return true;
333  }
334  else
335  {
336  ETL_ASSERT(false, ETL_ERROR(variant_pool_did_not_create));
337  return false;
338  }
339  }
340 
341  //*************************************************************************
343  //*************************************************************************
344  size_t max_size() const
345  {
346  return MAX_SIZE;
347  }
348 
349  //*************************************************************************
351  //*************************************************************************
352  size_t available() const
353  {
354  return pool.available();
355  }
356 
357  //*************************************************************************
359  //*************************************************************************
360  size_t size() const
361  {
362  return pool.size();
363  }
364 
365  //*************************************************************************
368  //*************************************************************************
369  bool empty() const
370  {
371  return pool.empty();
372  }
373 
374  //*************************************************************************
377  //*************************************************************************
378  bool full() const
379  {
380  return pool.full();
381  }
382 
383  private:
384 
385  variant_pool(const variant_pool&);
386  variant_pool& operator =(const variant_pool&);
387 
388  // The pool.
391  MAX_SIZE> pool;
392  };
393 }
394 
395 #undef ETL_FILE
396 
397 #endif
Definition: variant_pool_generator.h:98
Definition: variant_pool_generator.h:109
Definition: variant_pool_generator.h:87
Definition: variant_pool_generator.h:132
size_t max_size() const
Returns the maximum number of items in the variant_pool.
Definition: variant_pool.h:344
bool destroy(const T *const p)
Destroys the object.
Definition: variant_pool.h:305
T * create(const TP1 &p1, const TP2 &p2, const TP3 &p3)
Creates the object. Three parameter constructor.
Definition: variant_pool.h:223
T * create(const TP1 &p1, const TP2 &p2)
Creates the object. Two parameter constructor.
Definition: variant_pool.h:196
T * create(const TP1 &p1, const TP2 &p2, const TP3 &p3, const TP4 &p4)
Creates the object. Four parameter constructor.
Definition: variant_pool.h:250
variant_pool()
Default constructor.
Definition: variant_pool.h:133
T * create(const TP1 &p1)
Creates the object. One parameter constructor.
Definition: variant_pool.h:169
size_t available() const
Returns the number of free items in the variant_pool.
Definition: variant_pool.h:352
size_t size() const
Returns the number of allocated items in the variant_pool.
Definition: variant_pool.h:360
bool empty() const
Definition: variant_pool.h:369
T * create()
Creates the object. Default constructor.
Definition: variant_pool.h:142
bool full() const
Definition: variant_pool.h:378
#define ETL_ASSERT(b, e)
Definition: error_handler.h:290
exception(string_type reason_, string_type file_, numeric_type line_)
Constructor.
Definition: exception.h:67
Definition: exception.h:47
Definition: largest.h:362
size_t size() const
Returns the number of allocated items in the pool.
Definition: pool.h:309
bool empty() const
Definition: pool.h:318
bool is_in_pool(const void *p_object) const
Definition: pool.h:276
bool full() const
Definition: pool.h:327
void release(const void *const p_object)
Definition: pool.h:255
size_t available() const
Returns the number of free items in the pool.
Definition: pool.h:301
Definition: pool.h:470
Definition: pool.h:617
is_base_of
Definition: type_traits_generator.h:1289
Definition: absolute.h:37
Definition: type_traits.h:1397