Embedded Template Library 1.0
Loading...
Searching...
No Matches
variant_pool_generator.h
1/******************************************************************************
2The MIT License(MIT)
3
4Embedded Template Library.
5https://github.com/ETLCPP/etl
6https://www.etlcpp.com
7
8Copyright(c) 2017 John Wellbelove
9
10Permission is hereby granted, free of charge, to any person obtaining a copy
11of this software and associated documentation files(the "Software"), to deal
12in the Software without restriction, including without limitation the rights
13to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14copies of the Software, and to permit persons to whom the Software is
15furnished to do so, subject to the following conditions :
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26SOFTWARE.
27******************************************************************************/
28
29/*[[[cog
30import cog
31cog.outl("#if 0")
32]]]*/
33/*[[[end]]]*/
34#error THIS HEADER IS A GENERATOR. DO NOT INCLUDE.
35/*[[[cog
36import cog
37cog.outl("#endif")
38]]]*/
39/*[[[end]]]*/
40
41/*[[[cog
42import cog
43cog.outl("//***************************************************************************")
44cog.outl("// THIS FILE HAS BEEN AUTO GENERATED. DO NOT EDIT THIS FILE.")
45cog.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 "platform.h"
67#include "pool.h"
68#include "type_traits.h"
69#include "static_assert.h"
70#include "largest.h"
71
72#include <stdint.h>
73
74namespace etl
75{
76#if ETL_USING_CPP11 && !defined(ETL_VARIANT_POOL_FORCE_CPP03_IMPLEMENTATION)
77 //***************************************************************************
78 template <size_t MAX_SIZE_, typename ... Ts>
79 class variant_pool
80 : public etl::generic_pool<etl::largest<Ts...>::size,
81 etl::largest<Ts...>::alignment,
82 MAX_SIZE_>
83 {
84 public:
85
87 etl::largest<Ts...>::alignment,
88 MAX_SIZE_> base_t;
89
90 static const size_t MAX_SIZE = MAX_SIZE_;
91
92 //*************************************************************************
94 //*************************************************************************
96 {
97 }
98
99 //*************************************************************************
101 //*************************************************************************
102 template <typename T, typename... Args>
103 T* create(Args&&... args)
104 {
105 ETL_STATIC_ASSERT((etl::is_one_of<T, Ts...>::value), "Unsupported type");
106
107 return base_t::template create<T>(etl::forward<Args>(args)...);
108 }
109
110 //*************************************************************************
112 //*************************************************************************
113 template <typename T>
114 void destroy(const T* const p)
115 {
116 ETL_STATIC_ASSERT((etl::is_one_of<T, Ts...>::value || etl::is_base_of_any<T, Ts...>::value), "Invalid type");
117
118 base_t::destroy(p);
119 }
120
121 //*************************************************************************
123 //*************************************************************************
124 size_t max_size() const
125 {
126 return MAX_SIZE;
127 }
128
129 private:
130
131 variant_pool(const variant_pool&) ETL_DELETE;
132 variant_pool& operator =(const variant_pool&) ETL_DELETE;
133 };
134
135 //***************************************************************************
136 template <typename ... Ts>
137 class variant_pool_ext
138 : public etl::generic_pool_ext<etl::largest<Ts...>::size,
139 etl::largest<Ts...>::alignment>
140 {
141 public:
142
144 etl::largest<Ts...>::alignment> base_t;
145
146 //*************************************************************************
148 //*************************************************************************
149 variant_pool_ext(typename base_t::element* buffer, size_t size)
150 : base_t(buffer, size)
151 {
152 }
153
154 //*************************************************************************
156 //*************************************************************************
157 template <typename T, typename... Args>
158 T* create(Args&&... args)
159 {
160 ETL_STATIC_ASSERT((etl::is_one_of<T, Ts...>::value), "Unsupported type");
161
162 return base_t::template create<T>(etl::forward<Args>(args)...);
163 }
164
165 //*************************************************************************
167 //*************************************************************************
168 template <typename T>
169 void destroy(const T* const p)
170 {
171 ETL_STATIC_ASSERT((etl::is_one_of<T, Ts...>::value || etl::is_base_of_any<T, Ts...>::value), "Invalid type");
172
173 base_t::destroy(p);
174 }
175
176 //*************************************************************************
178 //*************************************************************************
179 size_t max_size() const
180 {
181 return base_t::max_size();
182 }
183
184 private:
185
186 variant_pool_ext(const variant_pool_ext&) ETL_DELETE;
187 variant_pool_ext& operator =(const variant_pool_ext&) ETL_DELETE;
188 };
189#else
190 //***************************************************************************
191 /*[[[cog
192 import cog
193 cog.outl("template <size_t MAX_SIZE_,")
194 cog.outl(" typename T1,")
195 for n in range(2, int(NTypes)):
196 cog.outl(" typename T%s = void," % n)
197 cog.outl(" typename T%s = void>" % int(NTypes))
198 cog.outl("class variant_pool")
199 cog.out(" : public etl::generic_pool<")
200 cog.out("etl::largest<")
201 for n in range(1, int(NTypes)):
202 cog.out("T%s, " % n)
203 cog.outl("T%s>::size," % int(NTypes))
204 cog.out(" etl::largest<")
205 for n in range(1, int(NTypes)):
206 cog.out("T%s, " % n)
207 cog.outl("T%s>::alignment," % int(NTypes))
208 cog.outl(" MAX_SIZE_>")
209 ]]]*/
210 /*[[[end]]]*/
211 {
212 public:
213
214 /*[[[cog
215 import cog
216 cog.out("typedef etl::generic_pool<")
217 cog.out("etl::largest<")
218 for n in range(1, int(NTypes)):
219 cog.out("T%s, " % n)
220 cog.outl("T%s>::size," % int(NTypes))
221 cog.out(" etl::largest<")
222 for n in range(1, int(NTypes)):
223 cog.out("T%s, " % n)
224 cog.outl("T%s>::alignment," % int(NTypes))
225 cog.outl(" MAX_SIZE_> base_t;")
226 ]]]*/
227 /*[[[end]]]*/
228
229 static const size_t MAX_SIZE = MAX_SIZE_;
230
231 //*************************************************************************
233 //*************************************************************************
235 {
236 }
237
238#if ETL_CPP11_NOT_SUPPORTED || ETL_USING_STLPORT
239 //*************************************************************************
241 //*************************************************************************
242 template <typename T>
243 T* create()
244 {
245 /*[[[cog
246 import cog
247 cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
248 for n in range(1, int(NTypes)):
249 cog.out("T%s, " % n)
250 if n % 16 == 0:
251 cog.outl("")
252 cog.out(" ")
253 cog.outl("T%s>::value), \"Unsupported type\");" % int(NTypes))
254 ]]]*/
255 /*[[[end]]]*/
256
257 return base_t::template create<T>();
258 }
259
260 //*************************************************************************
262 //*************************************************************************
263 template <typename T, typename TP1>
264 T* create(const TP1& p1)
265 {
266 /*[[[cog
267 import cog
268 cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
269 for n in range(1, int(NTypes)):
270 cog.out("T%s, " % n)
271 if n % 16 == 0:
272 cog.outl("")
273 cog.out(" ")
274 cog.outl("T%s>::value), \"Unsupported type\");" % int(NTypes))
275 ]]]*/
276 /*[[[end]]]*/
277
278 return base_t::template create<T>(p1);
279 }
280
281 //*************************************************************************
283 //*************************************************************************
284 template <typename T, typename TP1, typename TP2>
285 T* create(const TP1& p1, const TP2& p2)
286 {
287 /*[[[cog
288 import cog
289 cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
290 for n in range(1, int(NTypes)):
291 cog.out("T%s, " % n)
292 if n % 16 == 0:
293 cog.outl("")
294 cog.out(" ")
295 cog.outl("T%s>::value), \"Unsupported type\");" % int(NTypes))
296 ]]]*/
297 /*[[[end]]]*/
298
299 return base_t::template create<T>(p1, p2);
300 }
301
302 //*************************************************************************
304 //*************************************************************************
305 template <typename T, typename TP1, typename TP2, typename TP3>
306 T* create(const TP1& p1, const TP2& p2, const TP3& p3)
307 {
308 /*[[[cog
309 import cog
310 cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
311 for n in range(1, int(NTypes)):
312 cog.out("T%s, " % n)
313 if n % 16 == 0:
314 cog.outl("")
315 cog.out(" ")
316 cog.outl("T%s>::value), \"Unsupported type\");" % int(NTypes))
317 ]]]*/
318 /*[[[end]]]*/
319
320 return base_t::template create<T>(p1, p2, p3);
321 }
322
323 //*************************************************************************
325 //*************************************************************************
326 template <typename T, typename TP1, typename TP2, typename TP3, typename TP4>
327 T* create(const TP1& p1, const TP2& p2, const TP3& p3, const TP4& p4)
328 {
329 /*[[[cog
330 import cog
331 cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
332 for n in range(1, int(NTypes)):
333 cog.out("T%s, " % n)
334 if n % 16 == 0:
335 cog.outl("")
336 cog.out(" ")
337 cog.outl("T%s>::value), \"Unsupported type\");" % int(NTypes))
338 ]]]*/
339 /*[[[end]]]*/
340
341 return base_t::template create<T>(p1, p2, p3, p4);
342 }
343#else
344 //*************************************************************************
346 //*************************************************************************
347 template <typename T, typename... Args>
349 {
350 /*[[[cog
351 import cog
352 cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
353 for n in range(1, int(NTypes)):
354 cog.out("T%s, " % n)
355 if n % 16 == 0:
356 cog.outl("")
357 cog.out(" ")
358 cog.outl("T%s>::value), \"Unsupported type\");" % int(NTypes))
359 ]]]*/
360 /*[[[end]]]*/
361
362 return base_t::template create<T>(etl::forward<Args>(args)...);
363 }
364#endif
365
366 //*************************************************************************
368 //*************************************************************************
369 template <typename T>
370 void destroy(const T* const p)
371 {
372 /*[[[cog
373 import cog
374 cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
375 for n in range(1, int(NTypes)):
376 cog.out("T%s, " % n)
377 if n % 16 == 0:
378 cog.outl("")
379 cog.out(" ")
380 cog.outl("T%s>::value ||" % int(NTypes))
381
382 for n in range(1, int(NTypes)):
383 cog.outl(" etl::is_base_of<T, T%s>::value ||" % n)
384 cog.outl(" etl::is_base_of<T, T%s>::value), \"Invalid type\");" % int(NTypes))
385
386 ]]]*/
387 /*[[[end]]]*/
388
389 base_t::destroy(p);
390 }
391
392 //*************************************************************************
394 //*************************************************************************
395 size_t max_size() const
396 {
397 return MAX_SIZE;
398 }
399
400 private:
401
402 variant_pool(const variant_pool&) ETL_DELETE;
403 variant_pool& operator =(const variant_pool&) ETL_DELETE;
404 };
405
406 //***************************************************************************
407 /*[[[cog
408 import cog
409 cog.outl("template <typename T1,")
410 for n in range(2, int(NTypes)):
411 cog.outl(" typename T%s = void," % n)
412 cog.outl(" typename T%s = void>" % int(NTypes))
413 cog.outl("class variant_pool_ext")
414 cog.out(" : public etl::generic_pool_ext<")
415 cog.out("etl::largest<")
416 for n in range(1, int(NTypes)):
417 cog.out("T%s, " % n)
418 cog.outl("T%s>::size," % int(NTypes))
419 cog.out(" etl::largest<")
420 for n in range(1, int(NTypes)):
421 cog.out("T%s, " % n)
422 cog.outl("T%s>::alignment>" % int(NTypes))
423 ]]]*/
424 /*[[[end]]]*/
425 {
426 public:
427
428 /*[[[cog
429 import cog
430 cog.out("typedef etl::generic_pool_ext<")
431 cog.out("etl::largest<")
432 for n in range(1, int(NTypes)):
433 cog.out("T%s, " % n)
434 cog.outl("T%s>::size," % int(NTypes))
435 cog.out(" etl::largest<")
436 for n in range(1, int(NTypes)):
437 cog.out("T%s, " % n)
438 cog.outl("T%s>::alignment> base_t;" % int(NTypes))
439 ]]]*/
440 /*[[[end]]]*/
441
442 //*************************************************************************
444 //*************************************************************************
445 variant_pool_ext(typename base_t::element* buffer, size_t size)
446 : base_t(buffer, size)
447 {
448 }
449
450#if ETL_CPP11_NOT_SUPPORTED || ETL_USING_STLPORT
451 //*************************************************************************
453 //*************************************************************************
454 template <typename T>
455 T* create()
456 {
457 /*[[[cog
458 import cog
459 cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
460 for n in range(1, int(NTypes)):
461 cog.out("T%s, " % n)
462 if n % 16 == 0:
463 cog.outl("")
464 cog.out(" ")
465 cog.outl("T%s>::value), \"Unsupported type\");" % int(NTypes))
466 ]]]*/
467 /*[[[end]]]*/
468
469 return base_t::template create<T>();
470 }
471
472 //*************************************************************************
474 //*************************************************************************
475 template <typename T, typename TP1>
476 T* create(const TP1& p1)
477 {
478 /*[[[cog
479 import cog
480 cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
481 for n in range(1, int(NTypes)):
482 cog.out("T%s, " % n)
483 if n % 16 == 0:
484 cog.outl("")
485 cog.out(" ")
486 cog.outl("T%s>::value), \"Unsupported type\");" % int(NTypes))
487 ]]]*/
488 /*[[[end]]]*/
489
490 return base_t::template create<T>(p1);
491 }
492
493 //*************************************************************************
495 //*************************************************************************
496 template <typename T, typename TP1, typename TP2>
497 T* create(const TP1& p1, const TP2& p2)
498 {
499 /*[[[cog
500 import cog
501 cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
502 for n in range(1, int(NTypes)):
503 cog.out("T%s, " % n)
504 if n % 16 == 0:
505 cog.outl("")
506 cog.out(" ")
507 cog.outl("T%s>::value), \"Unsupported type\");" % int(NTypes))
508 ]]]*/
509 /*[[[end]]]*/
510
511 return base_t::template create<T>(p1, p2);
512 }
513
514 //*************************************************************************
516 //*************************************************************************
517 template <typename T, typename TP1, typename TP2, typename TP3>
518 T* create(const TP1& p1, const TP2& p2, const TP3& p3)
519 {
520 /*[[[cog
521 import cog
522 cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
523 for n in range(1, int(NTypes)):
524 cog.out("T%s, " % n)
525 if n % 16 == 0:
526 cog.outl("")
527 cog.out(" ")
528 cog.outl("T%s>::value), \"Unsupported type\");" % int(NTypes))
529 ]]]*/
530 /*[[[end]]]*/
531
532 return base_t::template create<T>(p1, p2, p3);
533 }
534
535 //*************************************************************************
537 //*************************************************************************
538 template <typename T, typename TP1, typename TP2, typename TP3, typename TP4>
539 T* create(const TP1& p1, const TP2& p2, const TP3& p3, const TP4& p4)
540 {
541 /*[[[cog
542 import cog
543 cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
544 for n in range(1, int(NTypes)):
545 cog.out("T%s, " % n)
546 if n % 16 == 0:
547 cog.outl("")
548 cog.out(" ")
549 cog.outl("T%s>::value), \"Unsupported type\");" % int(NTypes))
550 ]]]*/
551 /*[[[end]]]*/
552
553 return base_t::template create<T>(p1, p2, p3, p4);
554 }
555#else
556 //*************************************************************************
558 //*************************************************************************
559 template <typename T, typename... Args>
560 T* create(Args&&... args)
561 {
562 /*[[[cog
563 import cog
564 cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
565 for n in range(1, int(NTypes)):
566 cog.out("T%s, " % n)
567 if n % 16 == 0:
568 cog.outl("")
569 cog.out(" ")
570 cog.outl("T%s>::value), \"Unsupported type\");" % int(NTypes))
571 ]]]*/
572 /*[[[end]]]*/
573
574 return base_t::template create<T>(etl::forward<Args>(args)...);
575 }
576#endif
577
578 //*************************************************************************
580 //*************************************************************************
581 template <typename T>
582 void destroy(const T* const p)
583 {
584 /*[[[cog
585 import cog
586 cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
587 for n in range(1, int(NTypes)):
588 cog.out("T%s, " % n)
589 if n % 16 == 0:
590 cog.outl("")
591 cog.out(" ")
592 cog.outl("T%s>::value ||" % int(NTypes))
593
594 for n in range(1, int(NTypes)):
595 cog.outl(" etl::is_base_of<T, T%s>::value ||" % n)
596 cog.outl(" etl::is_base_of<T, T%s>::value), \"Invalid type\");" % int(NTypes))
597
598 ]]]*/
599 /*[[[end]]]*/
600
601 base_t::destroy(p);
602 }
603
604 //*************************************************************************
606 //*************************************************************************
607 size_t max_size() const
608 {
609 return base_t::max_size();
610 }
611
612 private:
613
614 variant_pool_ext(const variant_pool_ext&) ETL_DELETE;
615 variant_pool_ext& operator =(const variant_pool_ext&) ETL_DELETE;
616 };
617#endif
618}
619
620#endif
T * create()
Creates the object. Default constructor.
Definition variant_pool.h:364
void destroy(const T *const p)
Destroys the object.
Definition variant_pool.h:431
variant_pool_ext(typename base_t::element *buffer, size_t size)
Default constructor.
Definition variant_pool.h:354
size_t max_size() const
Returns the maximum number of items in the variant_pool.
Definition variant_pool.h:457
Definition variant_pool.h:200
size_t max_size() const
Returns the maximum number of items in the variant_pool.
Definition variant_pool.h:314
void destroy(const T *const p)
Destroys the object.
Definition variant_pool.h:288
T * create()
Creates the object. Default constructor.
Definition variant_pool.h:221
variant_pool()
Default constructor.
Definition variant_pool.h:212
Definition largest.h:367
size_t size() const
Returns the number of allocated items in the pool.
Definition ipool.h:301
Definition generic_pool.h:56
Definition generic_pool.h:213
bitset_ext
Definition absolute.h:38
size_t max_size() const
Returns the maximum number of items in the variant_pool.
Definition variant_pool_generator.h:395
T * create(Args &&... args)
Creates the object from a type. Variadic parameter constructor.
Definition variant_pool_generator.h:348
variant_pool()
Default constructor.
Definition variant_pool_generator.h:234
ETL_CONSTEXPR TContainer::size_type size(const TContainer &container)
Definition iterator.h:1187
void destroy(const T *const p)
Destroys the object.
Definition variant_pool_generator.h:370
pair holds two objects of arbitrary type
Definition utility.h:164
ETL_CONSTEXPR pair()
Default constructor.
Definition utility.h:176