Embedded Template Library  1.0
variant_pool_generator.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 /*[[[cog
30 import cog
31 cog.outl("#if 0")
32 ]]]*/
33 /*[[[end]]]*/
34 #error THIS HEADER IS A GENERATOR. DO NOT INCLUDE.
35 /*[[[cog
36 import cog
37 cog.outl("#endif")
38 ]]]*/
39 /*[[[end]]]*/
40 
41 /*[[[cog
42 import cog
43 cog.outl("//***************************************************************************")
44 cog.outl("// THIS FILE HAS BEEN AUTO GENERATED. DO NOT EDIT THIS FILE.")
45 cog.outl("//***************************************************************************")
46 ]]]*/
47 /*[[[end]]]*/
48 
49 //***************************************************************************
50 // To generate to header file, run this at the command line.
51 // Note: You will need Python and COG installed.
52 //
53 // python -m cogapp -d -e -ovariant_pool.h -DNTypes=<n> variant_pool_generator.h
54 // Where <n> is the number of types to support.
55 //
56 // e.g.
57 // To generate handlers for up to 16 types...
58 // python -m cogapp -d -e -ovariant_pool.h -DNTypes=16 variant_pool_generator.h
59 //
60 // See generate.bat
61 //***************************************************************************
62 
63 #ifndef ETL_VARIANT_POOL_INCLUDED
64 #define ETL_VARIANT_POOL_INCLUDED
65 
66 #include <stdint.h>
67 
68 #include "platform.h"
69 #include "error_handler.h"
70 #include "exception.h"
71 #include "largest.h"
72 #include "type_traits.h"
73 #include "alignment.h"
74 #include "static_assert.h"
75 #include "type_lookup.h"
76 #include "pool.h"
77 
78 #include "utility.h"
79 
80 #undef ETL_FILE
81 #define ETL_FILE "40"
82 
83 namespace etl
84 {
85  //***************************************************************************
87  {
88  public:
89 
90  variant_pool_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
91  : exception(reason_, file_name_, line_number_)
92  {
93  }
94  };
95 
96  //***************************************************************************
98  {
99  public:
100 
101  variant_pool_cannot_create(string_type file_name_, numeric_type line_number_)
102  : variant_pool_exception(ETL_ERROR_TEXT("variant_pool:cannot create", ETL_FILE"A"), file_name_, line_number_)
103  {
104  }
105  };
106 
107  //***************************************************************************
109  {
110  public:
111 
112  variant_pool_did_not_create(string_type file_name_, numeric_type line_number_)
113  : variant_pool_exception(ETL_ERROR_TEXT("variant_pool:did not create", ETL_FILE"B"), file_name_, line_number_)
114  {
115  }
116  };
117 
118  //***************************************************************************
119  /*[[[cog
120  import cog
121  cog.outl("template <const size_t MAX_SIZE_,")
122  cog.outl(" typename T1, ")
123  for n in range(2, int(NTypes)):
124  cog.outl(" typename T%s = void, " % n)
125  if n % 16 == 0:
126  cog.outl("")
127  cog.out(" ")
128  cog.outl(" typename T%s = void>" % int(NTypes))
129  ]]]*/
130  /*[[[end]]]*/
132  {
133  public:
134 
135  static const size_t MAX_SIZE = MAX_SIZE_;
136 
137  //*************************************************************************
139  //*************************************************************************
141  {
142  }
143 
144 #if ETL_CPP11_NOT_SUPPORTED || ETL_USING_STLPORT
145  //*************************************************************************
147  //*************************************************************************
148  template <typename T>
149  T* create()
150  {
151  /*[[[cog
152  import cog
153  cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
154  for n in range(1, int(NTypes)):
155  cog.out("T%s, " % n)
156  if n % 16 == 0:
157  cog.outl("")
158  cog.out(" ")
159  cog.outl("T%s>::value), \"Unsupported type\");" % int(NTypes))
160  ]]]*/
161  /*[[[end]]]*/
162 
163  T* p = ETL_NULLPTR;
164 
165  if (pool.full())
166  {
167  ETL_ASSERT(false, ETL_ERROR(etl::variant_pool_cannot_create));
168  }
169  else
170  {
171  p = pool.template allocate<T>();
172 
173  if (p != ETL_NULLPTR)
174  {
175  new (p) T();
176  }
177  }
178 
179  return p;
180  }
181 
182  //*************************************************************************
184  //*************************************************************************
185  template <typename T, typename TP1>
186  T* create(const TP1& p1)
187  {
188  /*[[[cog
189  import cog
190  cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
191  for n in range(1, int(NTypes)):
192  cog.out("T%s, " % n)
193  if n % 16 == 0:
194  cog.outl("")
195  cog.out(" ")
196  cog.outl("T%s>::value), \"Unsupported type\");" % int(NTypes))
197  ]]]*/
198  /*[[[end]]]*/
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);
213  }
214  }
215 
216  return p;
217  }
218 
219  //*************************************************************************
221  //*************************************************************************
222  template <typename T, typename TP1, typename TP2>
223  T* create(const TP1& p1, const TP2& p2)
224  {
225  /*[[[cog
226  import cog
227  cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
228  for n in range(1, int(NTypes)):
229  cog.out("T%s, " % n)
230  if n % 16 == 0:
231  cog.outl("")
232  cog.out(" ")
233  cog.outl("T%s>::value), \"Unsupported type\");" % int(NTypes))
234  ]]]*/
235  /*[[[end]]]*/
236 
237  T* p = ETL_NULLPTR;
238 
239  if (pool.full())
240  {
241  ETL_ASSERT(false, ETL_ERROR(etl::variant_pool_cannot_create));
242  }
243  else
244  {
245  p = pool.template allocate<T>();
246 
247  if (p != ETL_NULLPTR)
248  {
249  new (p) T(p1, p2);
250  }
251  }
252 
253  return p;
254  }
255 
256  //*************************************************************************
258  //*************************************************************************
259  template <typename T, typename TP1, typename TP2, typename TP3>
260  T* create(const TP1& p1, const TP2& p2, const TP3& p3)
261  {
262  /*[[[cog
263  import cog
264  cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
265  for n in range(1, int(NTypes)):
266  cog.out("T%s, " % n)
267  if n % 16 == 0:
268  cog.outl("")
269  cog.out(" ")
270  cog.outl("T%s>::value), \"Unsupported type\");" % int(NTypes))
271  ]]]*/
272  /*[[[end]]]*/
273 
274  T* p = ETL_NULLPTR;
275 
276  if (pool.full())
277  {
278  ETL_ASSERT(false, ETL_ERROR(etl::variant_pool_cannot_create));
279  }
280  else
281  {
282  p = pool.template allocate<T>();
283 
284  if (p != ETL_NULLPTR)
285  {
286  new (p) T(p1, p2, p3);
287  }
288  }
289 
290  return p;
291  }
292 
293  //*************************************************************************
295  //*************************************************************************
296  template <typename T, typename TP1, typename TP2, typename TP3, typename TP4>
297  T* create(const TP1& p1, const TP2& p2, const TP3& p3, const TP4& p4)
298  {
299  /*[[[cog
300  import cog
301  cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
302  for n in range(1, int(NTypes)):
303  cog.out("T%s, " % n)
304  if n % 16 == 0:
305  cog.outl("")
306  cog.out(" ")
307  cog.outl("T%s>::value), \"Unsupported type\");" % int(NTypes))
308  ]]]*/
309  /*[[[end]]]*/
310 
311  T* p = ETL_NULLPTR;
312 
313  if (pool.full())
314  {
315  ETL_ASSERT(false, ETL_ERROR(etl::variant_pool_cannot_create));
316  }
317  else
318  {
319  p = pool.template allocate<T>();
320 
321  if (p != ETL_NULLPTR)
322  {
323  new (p) T(p1, p2, p3, p4);
324  }
325  }
326 
327  return p;
328  }
329 #else
330  //*************************************************************************
332  //*************************************************************************
333  template <typename T, typename... Args>
334  T* create(Args&&... args)
335  {
336  /*[[[cog
337  import cog
338  cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
339  for n in range(1, int(NTypes)):
340  cog.out("T%s, " % n)
341  if n % 16 == 0:
342  cog.outl("")
343  cog.out(" ")
344  cog.outl("T%s>::value), \"Unsupported type\");" % int(NTypes))
345  ]]]*/
346  /*[[[end]]]*/
347 
348  T* p = ETL_NULLPTR;
349 
350  if (pool.full())
351  {
352  ETL_ASSERT(false, ETL_ERROR(etl::variant_pool_cannot_create));
353  }
354  else
355  {
356  p = pool.template allocate<T>();
357 
358  if (p != ETL_NULLPTR)
359  {
360  new (p) T(ETL_OR_STD::forward<Args>(args)...);
361  }
362  }
363 
364  return p;
365  }
366 #endif
367 
368  //*************************************************************************
370  //*************************************************************************
371  template <typename T>
372  bool destroy(const T* const p)
373  {
374  /*[[[cog
375  import cog
376  cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
377  for n in range(1, int(NTypes)):
378  cog.out("T%s, " % n)
379  if n % 16 == 0:
380  cog.outl("")
381  cog.out(" ")
382  cog.outl("T%s>::value ||" % int(NTypes))
383 
384  for n in range(1, int(NTypes)):
385  cog.outl(" etl::is_base_of<T, T%s>::value ||" % n)
386  cog.outl(" etl::is_base_of<T, T%s>::value), \"Invalid type\");" % int(NTypes))
387 
388  ]]]*/
389  /*[[[end]]]*/
390 
391  p->~T();
392 
393  void* vp = reinterpret_cast<char*>(const_cast<T*>(p));
394 
395  if (pool.is_in_pool(vp))
396  {
397  pool.release(vp);
398  return true;
399  }
400  else
401  {
402  ETL_ASSERT(false, ETL_ERROR(variant_pool_did_not_create));
403  return false;
404  }
405  }
406 
407  //*************************************************************************
409  //*************************************************************************
410  size_t max_size() const
411  {
412  return MAX_SIZE;
413  }
414 
415  //*************************************************************************
417  //*************************************************************************
418  size_t available() const
419  {
420  return pool.available();
421  }
422 
423  //*************************************************************************
425  //*************************************************************************
426  size_t size() const
427  {
428  return pool.size();
429  }
430 
431  //*************************************************************************
434  //*************************************************************************
435  bool empty() const
436  {
437  return pool.empty();
438  }
439 
440  //*************************************************************************
443  //*************************************************************************
444  bool full() const
445  {
446  return pool.full();
447  }
448 
449  private:
450 
451  variant_pool(const variant_pool&);
452  variant_pool& operator =(const variant_pool&);
453 
454  // The pool.
455  /*[[[cog
456  import cog
457  cog.out("etl::generic_pool<etl::largest<")
458  for n in range(1, int(NTypes)):
459  cog.out("T%s, " % n)
460  if n % 16 == 0:
461  cog.outl("")
462  cog.out(" ")
463  cog.outl("T%s>::size," % int(NTypes))
464 
465  cog.out(" etl::largest<")
466  for n in range(1, int(NTypes)):
467  cog.out("T%s, " % n)
468  if n % 16 == 0:
469  cog.outl("")
470  cog.out(" ")
471  cog.outl("T%s>::alignment," % int(NTypes))
472  cog.outl(" MAX_SIZE> pool;")
473  ]]]*/
474  /*[[[end]]]*/
475  };
476 }
477 
478 #undef ETL_FILE
479 
480 #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_generator.h:410
bool destroy(const T *const p)
Destroys the object.
Definition: variant_pool_generator.h:372
variant_pool()
Default constructor.
Definition: variant_pool_generator.h:140
T * create(Args &&... args)
Creates the object from a type. Variadic parameter constructor.
Definition: variant_pool_generator.h:334
size_t available() const
Returns the number of free items in the variant_pool.
Definition: variant_pool_generator.h:418
size_t size() const
Returns the number of allocated items in the variant_pool.
Definition: variant_pool_generator.h:426
bool empty() const
Checks to see if there are no allocated items in the variant_pool.
Definition: variant_pool_generator.h:435
T * create()
Creates the object. Default constructor.
Definition: variant_pool.h:142
bool full() const
Checks to see if there are no free items in the variant_pool.
Definition: variant_pool_generator.h:444
#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
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:617
Definition: absolute.h:37