Embedded Template Library 1.0
Loading...
Searching...
No Matches
variant_pool.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#ifndef ETL_VARIANT_POOL_INCLUDED
30#define ETL_VARIANT_POOL_INCLUDED
31
32#include "platform.h"
33#include "largest.h"
34#include "pool.h"
35#include "static_assert.h"
36#include "type_traits.h"
37
38#include <stdint.h>
39
40namespace etl
41{
42#if ETL_USING_CPP11 && !defined(ETL_VARIANT_POOL_FORCE_CPP03_IMPLEMENTATION)
43 //***************************************************************************
44 template <size_t MAX_SIZE_, typename... Ts>
45 class variant_pool : public etl::generic_pool<etl::largest<Ts...>::size, etl::largest<Ts...>::alignment, MAX_SIZE_>
46 {
47 public:
48
49 typedef etl::generic_pool<etl::largest<Ts...>::size, etl::largest<Ts...>::alignment, MAX_SIZE_> base_t;
50
51 static const size_t MAX_SIZE = MAX_SIZE_;
53 //*************************************************************************
55 //*************************************************************************
56 variant_pool() {}
57
58 //*************************************************************************
60 //*************************************************************************
61 template <typename T, typename... Args>
62 T* create(Args&&... args)
63 {
64 ETL_STATIC_ASSERT((etl::is_one_of<T, Ts...>::value), "Unsupported type");
65
66 return base_t::template create<T>(etl::forward<Args>(args)...);
67 }
69 //*************************************************************************
71 //*************************************************************************
72 template <typename T>
73 void destroy(const T* const p)
74 {
75 ETL_STATIC_ASSERT((etl::is_one_of<T, Ts...>::value || etl::is_base_of_any<T, Ts...>::value), "Invalid type");
76
77 base_t::destroy(p);
78 }
79
80 //*************************************************************************
82 //*************************************************************************
83 size_t max_size() const
84 {
85 return MAX_SIZE;
86 }
87
88 private:
89
90 variant_pool(const variant_pool&) ETL_DELETE;
91 variant_pool& operator=(const variant_pool&) ETL_DELETE;
92 };
93
94 //***************************************************************************
95 template <typename... Ts>
96 class variant_pool_ext : public etl::generic_pool_ext<etl::largest<Ts...>::size, etl::largest<Ts...>::alignment>
97 {
98 public:
100 typedef etl::generic_pool_ext<etl::largest<Ts...>::size, etl::largest<Ts...>::alignment> base_t;
101
102 //*************************************************************************
104 //*************************************************************************
105 variant_pool_ext(typename base_t::element* buffer, size_t size)
106 : base_t(buffer, size)
107 {
108 }
109
110 //*************************************************************************
112 //*************************************************************************
113 template <typename T, typename... Args>
114 T* create(Args&&... args)
115 {
116 ETL_STATIC_ASSERT((etl::is_one_of<T, Ts...>::value), "Unsupported type");
117
118 return base_t::template create<T>(etl::forward<Args>(args)...);
119 }
120
121 //*************************************************************************
123 //*************************************************************************
124 template <typename T>
125 void destroy(const T* const p)
126 {
127 ETL_STATIC_ASSERT((etl::is_one_of<T, Ts...>::value || etl::is_base_of_any<T, Ts...>::value), "Invalid type");
128
129 base_t::destroy(p);
130 }
131
132 //*************************************************************************
134 //*************************************************************************
135 size_t max_size() const
136 {
137 return base_t::max_size();
138 }
139
140 private:
141
142 variant_pool_ext(const variant_pool_ext&) ETL_DELETE;
143 variant_pool_ext& operator=(const variant_pool_ext&) ETL_DELETE;
144 };
145#else
146 #include "private/variant_pool_cpp03.h"
147#endif
148} // namespace etl
149
150#endif
Definition variant_pool.h:201
T * create()
Creates the object. Default constructor.
Definition variant_pool.h:220
void destroy(const T *const p)
Destroys the object.
Definition variant_pool.h:287
variant_pool_ext(typename base_t::element *buffer, size_t size)
Default constructor.
Definition variant_pool.h:210
size_t max_size() const
Returns the maximum number of items in the variant_pool.
Definition variant_pool.h:313
Definition variant_pool.h:56
size_t max_size() const
Returns the maximum number of items in the variant_pool.
Definition variant_pool.h:170
void destroy(const T *const p)
Destroys the object.
Definition variant_pool.h:144
T * create()
Creates the object. Default constructor.
Definition variant_pool.h:77
variant_pool()
Default constructor.
Definition variant_pool.h:68
Definition variant_pool_cpp03.h:200
Definition largest.h:45
size_t size() const
Returns the number of allocated items in the pool.
Definition ipool.h:522
Definition generic_pool.h:216
bitset_ext
Definition absolute.h:40