Embedded Template Library 1.0
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 //***************************************************************************
77 /*[[[cog
78 import cog
79 cog.outl("template <size_t MAX_SIZE_,")
80 cog.outl(" typename T1,")
81 for n in range(2, int(NTypes)):
82 cog.outl(" typename T%s = void," % n)
83 cog.outl(" typename T%s = void>" % int(NTypes))
84 cog.outl("class variant_pool")
85 cog.out(" : public etl::generic_pool<")
86 cog.out("etl::largest<")
87 for n in range(1, int(NTypes)):
88 cog.out("T%s, " % n)
89 cog.outl("T%s>::size," % int(NTypes))
90 cog.out(" etl::largest<")
91 for n in range(1, int(NTypes)):
92 cog.out("T%s, " % n)
93 cog.outl("T%s>::alignment," % int(NTypes))
94 cog.outl(" MAX_SIZE_>")
95 ]]]*/
96 /*[[[end]]]*/
97 {
98 public:
99
100 /*[[[cog
101 import cog
102 cog.out("typedef etl::generic_pool<")
103 cog.out("etl::largest<")
104 for n in range(1, int(NTypes)):
105 cog.out("T%s, " % n)
106 cog.outl("T%s>::size," % int(NTypes))
107 cog.out(" etl::largest<")
108 for n in range(1, int(NTypes)):
109 cog.out("T%s, " % n)
110 cog.outl("T%s>::alignment," % int(NTypes))
111 cog.outl(" MAX_SIZE_> base_t;")
112 ]]]*/
113 /*[[[end]]]*/
114
115 static const size_t MAX_SIZE = MAX_SIZE_;
116
117 //*************************************************************************
119 //*************************************************************************
121 {
122 }
123
124#if ETL_CPP11_NOT_SUPPORTED || ETL_USING_STLPORT
125 //*************************************************************************
127 //*************************************************************************
128 template <typename T>
129 T* create()
130 {
131 /*[[[cog
132 import cog
133 cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
134 for n in range(1, int(NTypes)):
135 cog.out("T%s, " % n)
136 if n % 16 == 0:
137 cog.outl("")
138 cog.out(" ")
139 cog.outl("T%s>::value), \"Unsupported type\");" % int(NTypes))
140 ]]]*/
141 /*[[[end]]]*/
142
143 return base_t::template create<T>();
144 }
145
146 //*************************************************************************
148 //*************************************************************************
149 template <typename T, typename TP1>
150 T* create(const TP1& p1)
151 {
152 /*[[[cog
153 import cog
154 cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
155 for n in range(1, int(NTypes)):
156 cog.out("T%s, " % n)
157 if n % 16 == 0:
158 cog.outl("")
159 cog.out(" ")
160 cog.outl("T%s>::value), \"Unsupported type\");" % int(NTypes))
161 ]]]*/
162 /*[[[end]]]*/
163
164 return base_t::template create<T>(p1);
165 }
166
167 //*************************************************************************
169 //*************************************************************************
170 template <typename T, typename TP1, typename TP2>
171 T* create(const TP1& p1, const TP2& p2)
172 {
173 /*[[[cog
174 import cog
175 cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
176 for n in range(1, int(NTypes)):
177 cog.out("T%s, " % n)
178 if n % 16 == 0:
179 cog.outl("")
180 cog.out(" ")
181 cog.outl("T%s>::value), \"Unsupported type\");" % int(NTypes))
182 ]]]*/
183 /*[[[end]]]*/
184
185 return base_t::template create<T>(p1, p2);
186 }
187
188 //*************************************************************************
190 //*************************************************************************
191 template <typename T, typename TP1, typename TP2, typename TP3>
192 T* create(const TP1& p1, const TP2& p2, const TP3& p3)
193 {
194 /*[[[cog
195 import cog
196 cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
197 for n in range(1, int(NTypes)):
198 cog.out("T%s, " % n)
199 if n % 16 == 0:
200 cog.outl("")
201 cog.out(" ")
202 cog.outl("T%s>::value), \"Unsupported type\");" % int(NTypes))
203 ]]]*/
204 /*[[[end]]]*/
205
206 return base_t::template create<T>(p1, p2, p3);
207 }
208
209 //*************************************************************************
211 //*************************************************************************
212 template <typename T, typename TP1, typename TP2, typename TP3, typename TP4>
213 T* create(const TP1& p1, const TP2& p2, const TP3& p3, const TP4& p4)
214 {
215 /*[[[cog
216 import cog
217 cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
218 for n in range(1, int(NTypes)):
219 cog.out("T%s, " % n)
220 if n % 16 == 0:
221 cog.outl("")
222 cog.out(" ")
223 cog.outl("T%s>::value), \"Unsupported type\");" % int(NTypes))
224 ]]]*/
225 /*[[[end]]]*/
226
227 return base_t::template create<T>(p1, p2, p3, p4);
228 }
229#else
230 //*************************************************************************
232 //*************************************************************************
233 template <typename T, typename... Args>
234 T* create(Args&&... args)
235 {
236 /*[[[cog
237 import cog
238 cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
239 for n in range(1, int(NTypes)):
240 cog.out("T%s, " % n)
241 if n % 16 == 0:
242 cog.outl("")
243 cog.out(" ")
244 cog.outl("T%s>::value), \"Unsupported type\");" % int(NTypes))
245 ]]]*/
246 /*[[[end]]]*/
247
248 return base_t::template create<T>(etl::forward<Args>(args)...);
249 }
250#endif
251
252 //*************************************************************************
254 //*************************************************************************
255 template <typename T>
256 void destroy(const T* const p)
257 {
258 /*[[[cog
259 import cog
260 cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
261 for n in range(1, int(NTypes)):
262 cog.out("T%s, " % n)
263 if n % 16 == 0:
264 cog.outl("")
265 cog.out(" ")
266 cog.outl("T%s>::value ||" % int(NTypes))
267
268 for n in range(1, int(NTypes)):
269 cog.outl(" etl::is_base_of<T, T%s>::value ||" % n)
270 cog.outl(" etl::is_base_of<T, T%s>::value), \"Invalid type\");" % int(NTypes))
271
272 ]]]*/
273 /*[[[end]]]*/
274
276 }
277
278 //*************************************************************************
280 //*************************************************************************
281 size_t max_size() const
282 {
283 return MAX_SIZE;
284 }
285
286 private:
287
288 variant_pool(const variant_pool&) ETL_DELETE;
289 variant_pool& operator =(const variant_pool&) ETL_DELETE;
290 };
291
292 //***************************************************************************
293 /*[[[cog
294 import cog
295 cog.outl("template <typename T1,")
296 for n in range(2, int(NTypes)):
297 cog.outl(" typename T%s = void," % n)
298 cog.outl(" typename T%s = void>" % int(NTypes))
299 cog.outl("class variant_pool_ext")
300 cog.out(" : public etl::generic_pool_ext<")
301 cog.out("etl::largest<")
302 for n in range(1, int(NTypes)):
303 cog.out("T%s, " % n)
304 cog.outl("T%s>::size," % int(NTypes))
305 cog.out(" etl::largest<")
306 for n in range(1, int(NTypes)):
307 cog.out("T%s, " % n)
308 cog.outl("T%s>::alignment>" % int(NTypes))
309 ]]]*/
310 /*[[[end]]]*/
311 {
312 public:
313
314 /*[[[cog
315 import cog
316 cog.out("typedef etl::generic_pool_ext<")
317 cog.out("etl::largest<")
318 for n in range(1, int(NTypes)):
319 cog.out("T%s, " % n)
320 cog.outl("T%s>::size," % int(NTypes))
321 cog.out(" etl::largest<")
322 for n in range(1, int(NTypes)):
323 cog.out("T%s, " % n)
324 cog.outl("T%s>::alignment> base_t;" % int(NTypes))
325 ]]]*/
326 /*[[[end]]]*/
327
328 //*************************************************************************
330 //*************************************************************************
331 variant_pool_ext(typename base_t::element* buffer, size_t size)
332 : base_t(buffer, size)
333 {
334 }
335
336#if ETL_CPP11_NOT_SUPPORTED || ETL_USING_STLPORT
337 //*************************************************************************
339 //*************************************************************************
340 template <typename T>
341 T* create()
342 {
343 /*[[[cog
344 import cog
345 cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
346 for n in range(1, int(NTypes)):
347 cog.out("T%s, " % n)
348 if n % 16 == 0:
349 cog.outl("")
350 cog.out(" ")
351 cog.outl("T%s>::value), \"Unsupported type\");" % int(NTypes))
352 ]]]*/
353 /*[[[end]]]*/
354
355 return base_t::template create<T>();
356 }
357
358 //*************************************************************************
360 //*************************************************************************
361 template <typename T, typename TP1>
362 T* create(const TP1& p1)
363 {
364 /*[[[cog
365 import cog
366 cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
367 for n in range(1, int(NTypes)):
368 cog.out("T%s, " % n)
369 if n % 16 == 0:
370 cog.outl("")
371 cog.out(" ")
372 cog.outl("T%s>::value), \"Unsupported type\");" % int(NTypes))
373 ]]]*/
374 /*[[[end]]]*/
375
376 return base_t::template create<T>(p1);
377 }
378
379 //*************************************************************************
381 //*************************************************************************
382 template <typename T, typename TP1, typename TP2>
383 T* create(const TP1& p1, const TP2& p2)
384 {
385 /*[[[cog
386 import cog
387 cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
388 for n in range(1, int(NTypes)):
389 cog.out("T%s, " % n)
390 if n % 16 == 0:
391 cog.outl("")
392 cog.out(" ")
393 cog.outl("T%s>::value), \"Unsupported type\");" % int(NTypes))
394 ]]]*/
395 /*[[[end]]]*/
396
397 return base_t::template create<T>(p1, p2);
398 }
399
400 //*************************************************************************
402 //*************************************************************************
403 template <typename T, typename TP1, typename TP2, typename TP3>
404 T* create(const TP1& p1, const TP2& p2, const TP3& p3)
405 {
406 /*[[[cog
407 import cog
408 cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
409 for n in range(1, int(NTypes)):
410 cog.out("T%s, " % n)
411 if n % 16 == 0:
412 cog.outl("")
413 cog.out(" ")
414 cog.outl("T%s>::value), \"Unsupported type\");" % int(NTypes))
415 ]]]*/
416 /*[[[end]]]*/
417
418 return base_t::template create<T>(p1, p2, p3);
419 }
420
421 //*************************************************************************
423 //*************************************************************************
424 template <typename T, typename TP1, typename TP2, typename TP3, typename TP4>
425 T* create(const TP1& p1, const TP2& p2, const TP3& p3, const TP4& p4)
426 {
427 /*[[[cog
428 import cog
429 cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
430 for n in range(1, int(NTypes)):
431 cog.out("T%s, " % n)
432 if n % 16 == 0:
433 cog.outl("")
434 cog.out(" ")
435 cog.outl("T%s>::value), \"Unsupported type\");" % int(NTypes))
436 ]]]*/
437 /*[[[end]]]*/
438
439 return base_t::template create<T>(p1, p2, p3, p4);
440 }
441#else
442 //*************************************************************************
444 //*************************************************************************
445 template <typename T, typename... Args>
446 T* create(Args&&... args)
447 {
448 /*[[[cog
449 import cog
450 cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
451 for n in range(1, int(NTypes)):
452 cog.out("T%s, " % n)
453 if n % 16 == 0:
454 cog.outl("")
455 cog.out(" ")
456 cog.outl("T%s>::value), \"Unsupported type\");" % int(NTypes))
457 ]]]*/
458 /*[[[end]]]*/
459
460 return base_t::template create<T>(etl::forward<Args>(args)...);
461 }
462#endif
463
464 //*************************************************************************
466 //*************************************************************************
467 template <typename T>
468 void destroy(const T* const p)
469 {
470 /*[[[cog
471 import cog
472 cog.out("ETL_STATIC_ASSERT((etl::is_one_of<T, ")
473 for n in range(1, int(NTypes)):
474 cog.out("T%s, " % n)
475 if n % 16 == 0:
476 cog.outl("")
477 cog.out(" ")
478 cog.outl("T%s>::value ||" % int(NTypes))
479
480 for n in range(1, int(NTypes)):
481 cog.outl(" etl::is_base_of<T, T%s>::value ||" % n)
482 cog.outl(" etl::is_base_of<T, T%s>::value), \"Invalid type\");" % int(NTypes))
483
484 ]]]*/
485 /*[[[end]]]*/
486
488 }
489
490 //*************************************************************************
492 //*************************************************************************
493 size_t max_size() const
494 {
495 return base_t::max_size();
496 }
497
498 private:
499
500 variant_pool_ext(const variant_pool_ext&) ETL_DELETE;
501 variant_pool_ext& operator =(const variant_pool_ext&) ETL_DELETE;
502 };
503}
504
505#endif
etl::enable_if< etl::is_trivially_destructible< typenameetl::iterator_traits< TIterator >::value_type >::value, void >::type destroy(TIterator, TIterator)
Definition: memory.h:1083
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:281
T * create(Args &&... args)
Creates the object from a type. Variadic parameter constructor.
Definition: variant_pool_generator.h:234
variant_pool()
Default constructor.
Definition: variant_pool_generator.h:120
void destroy(const T *const p)
Destroys the object.
Definition: variant_pool_generator.h:256