Embedded Template Library 1.0
Loading...
Searching...
No Matches
type_lookup.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_TYPE_LOOKUP_INCLUDED
30#define ETL_TYPE_LOOKUP_INCLUDED
31
32#include "platform.h"
33#include "type_traits.h"
34#include "static_assert.h"
35#include "integral_limits.h"
36#include "null_type.h"
37
38#include <limits.h>
39
40#if 0
41#error THIS HEADER IS A GENERATOR. DO NOT INCLUDE.
42#endif
43
44//***************************************************************************
45// THIS FILE HAS BEEN AUTO GENERATED. DO NOT EDIT THIS FILE.
46//***************************************************************************
47
48namespace etl
49{
50 //***************************************************************************
52 //***************************************************************************
53 template <typename T, int ID_>
54 struct type_id_pair
55 {
56 typedef T type;
57
58 enum
59 {
60 ID = ID_
61 };
62 };
63
64 //***************************************************************************
66 //***************************************************************************
67 template <typename T1, typename T2>
68 struct type_type_pair
69 {
70 typedef T1 type1;
71 typedef T2 type2;
72 };
73
74#if ETL_USING_CPP11 && !defined(ETL_TYPE_SELECT_FORCE_CPP03_IMPLEMENTATION)
75 //***************************************************************************
76 // type_id_lookup
77 //***************************************************************************
78 template <typename... TTypes>
79 struct type_id_lookup
80 {
81 private:
82
83 // The type for no match.
84 struct nulltype {};
85
86 // For N type pairs.
87 template <size_t ID, typename T1, typename... TRest>
88 struct type_from_id_helper
89 {
90 using type = typename etl::conditional<ID == T1::ID,
91 typename T1::type,
92 typename type_from_id_helper<ID, TRest...>::type>::type;
93 };
94
95 // Specialisation for 1 type pair.
96 template <size_t ID, typename T1>
97 struct type_from_id_helper<ID, T1>
98 {
99 using type = typename etl::conditional<ID == T1::ID,
100 typename T1::type,
101 nulltype>::type;
102 };
103
104 public:
105
106 //************************************
107 // type_from_id
108 //************************************
109 template <int ID>
110 struct type_from_id
111 {
112 using type = typename type_from_id_helper<ID, TTypes...>::type;
113
114 static_assert(!(etl::is_same<nulltype, type>::value), "Invalid id");
115 };
116
117 template <int ID>
118 using type_from_id_t = typename type_from_id<ID>::type;
119
120 private:
121
122 static constexpr size_t UNKNOWN = etl::integral_limits<size_t>::max;
123
124 // For N type pairs.
125 template <typename T, typename T1, typename... TRest>
126 struct id_from_type_helper
127 {
128 static constexpr size_t value = etl::is_same<T, typename T1::type>::value ? size_t(T1::ID) : id_from_type_helper<T, TRest...>::value;
129 };
130
131 // Specialisation for 1 type pair.
132 template <typename T, typename T1>
133 struct id_from_type_helper<T, T1>
134 {
135 static constexpr size_t value = etl::is_same<T, typename T1::type>::value ? size_t(T1::ID) : UNKNOWN;
136 };
137
138 public:
139
140 //************************************
141 // id_from_type
142 //************************************
143 template <typename T>
144 struct id_from_type
145 {
146 static constexpr size_t value = id_from_type_helper<T, TTypes...>::value;
147
148 static_assert(value != UNKNOWN, "Invalid type");
149 };
150
151#if ETL_USING_CPP17
152 template <typename T>
153 static constexpr size_t id_from_type_v = id_from_type<T>::value;
154#endif
155
156 //************************************
157 template <typename T>
158 static unsigned int get_id_from_type(const T&)
159 {
160 return get_id_from_type<T>();
161 }
162
163 //************************************
164 template <typename T>
165 static unsigned int get_id_from_type()
166 {
167 return id_from_type<T>::value;
168 }
169 };
170
171 //***************************************************************************
172 // type_type_lookup
173 //***************************************************************************
174 template <typename... TTypes>
175 class type_type_lookup
176 {
177 private:
178
179 // The type for no match.
180 struct nulltype {};
181
182 template <typename T, typename T1, typename... TRest>
183 struct type_from_type_helper
184 {
186 typename T1::type2,
187 typename type_from_type_helper<T, TRest...>::type>::type;
188 };
189
190 template <typename T, typename T1>
191 struct type_from_type_helper<T, T1>
192 {
194 typename T1::type2,
195 nulltype>::type;
196 };
197
198 public:
199
200 template <typename T>
201 class type_from_type
202 {
203 public:
204
205 // The matched type or nulltype
206 using type = typename type_from_type_helper<T, TTypes...>::type;
207
208 static_assert(!etl::is_same<type, nulltype>::value, "Type match not found");
209 };
210
211 // Template alias.
212 template <typename T>
213 using type_from_type_t = typename type_from_type<T>::type;
214 };
215
216#else
217
218 //***************************************************************************
219 // For 16 types.
220 //***************************************************************************
221 template <typename T1,
222 typename T2 = etl::type_id_pair<etl::null_type<0>, -2>,
223 typename T3 = etl::type_id_pair<etl::null_type<0>, -3>,
224 typename T4 = etl::type_id_pair<etl::null_type<0>, -4>,
225 typename T5 = etl::type_id_pair<etl::null_type<0>, -5>,
226 typename T6 = etl::type_id_pair<etl::null_type<0>, -6>,
227 typename T7 = etl::type_id_pair<etl::null_type<0>, -7>,
228 typename T8 = etl::type_id_pair<etl::null_type<0>, -8>,
229 typename T9 = etl::type_id_pair<etl::null_type<0>, -9>,
230 typename T10 = etl::type_id_pair<etl::null_type<0>, -10>,
231 typename T11 = etl::type_id_pair<etl::null_type<0>, -11>,
232 typename T12 = etl::type_id_pair<etl::null_type<0>, -12>,
233 typename T13 = etl::type_id_pair<etl::null_type<0>, -13>,
234 typename T14 = etl::type_id_pair<etl::null_type<0>, -14>,
235 typename T15 = etl::type_id_pair<etl::null_type<0>, -15>,
236 typename T16 = etl::type_id_pair<etl::null_type<0>, -16> >
238 {
239 public:
240
241 //************************************
242 template <int ID>
244 {
245 typedef
246 typename etl::conditional<ID == T1::ID, typename T1::type,
247 typename etl::conditional<ID == T2::ID, typename T2::type,
248 typename etl::conditional<ID == T3::ID, typename T3::type,
249 typename etl::conditional<ID == T4::ID, typename T4::type,
250 typename etl::conditional<ID == T5::ID, typename T5::type,
251 typename etl::conditional<ID == T6::ID, typename T6::type,
252 typename etl::conditional<ID == T7::ID, typename T7::type,
253 typename etl::conditional<ID == T8::ID, typename T8::type,
254 typename etl::conditional<ID == T9::ID, typename T9::type,
255 typename etl::conditional<ID == T10::ID, typename T10::type,
256 typename etl::conditional<ID == T11::ID, typename T11::type,
257 typename etl::conditional<ID == T12::ID, typename T12::type,
258 typename etl::conditional<ID == T13::ID, typename T13::type,
259 typename etl::conditional<ID == T14::ID, typename T14::type,
260 typename etl::conditional<ID == T15::ID, typename T15::type,
261 typename etl::conditional<ID == T16::ID, typename T16::type,
262 etl::null_type<0> >::type>::type>::type>::type>
263 ::type>::type>::type>::type>
264 ::type>::type>::type>::type>
265 ::type>::type>::type>::type type;
266
267 ETL_STATIC_ASSERT(!(etl::is_same<etl::null_type<0>, type>::value), "Invalid id");
268 };
269
270 //************************************
271 enum
272 {
273 UNKNOWN = UINT_MAX
274 };
275
276 template <typename T>
278 {
279 enum
280 {
281 value =
282 (unsigned int) etl::is_same<T, typename T1::type>::value ? (unsigned int)T1::ID :
283 (unsigned int) etl::is_same<T, typename T2::type>::value ? (unsigned int)T2::ID :
284 (unsigned int) etl::is_same<T, typename T3::type>::value ? (unsigned int)T3::ID :
285 (unsigned int) etl::is_same<T, typename T4::type>::value ? (unsigned int)T4::ID :
286 (unsigned int) etl::is_same<T, typename T5::type>::value ? (unsigned int)T5::ID :
287 (unsigned int) etl::is_same<T, typename T6::type>::value ? (unsigned int)T6::ID :
288 (unsigned int) etl::is_same<T, typename T7::type>::value ? (unsigned int)T7::ID :
289 (unsigned int) etl::is_same<T, typename T8::type>::value ? (unsigned int)T8::ID :
290 (unsigned int) etl::is_same<T, typename T9::type>::value ? (unsigned int)T9::ID :
291 (unsigned int) etl::is_same<T, typename T10::type>::value ? (unsigned int)T10::ID :
292 (unsigned int) etl::is_same<T, typename T11::type>::value ? (unsigned int)T11::ID :
293 (unsigned int) etl::is_same<T, typename T12::type>::value ? (unsigned int)T12::ID :
294 (unsigned int) etl::is_same<T, typename T13::type>::value ? (unsigned int)T13::ID :
295 (unsigned int) etl::is_same<T, typename T14::type>::value ? (unsigned int)T14::ID :
296 (unsigned int) etl::is_same<T, typename T15::type>::value ? (unsigned int)T15::ID :
297 (unsigned int) etl::is_same<T, typename T16::type>::value ? (unsigned int)T16::ID :
298 (unsigned int) UNKNOWN
299 };
300
301 ETL_STATIC_ASSERT(((unsigned int)value != (unsigned int)UNKNOWN), "Invalid type");
302 };
303
304 //************************************
305 template <typename T>
306 static unsigned int get_id_from_type(const T&)
307 {
308 return get_id_from_type<T>();
309 }
310
311 //************************************
312 template <typename T>
313 static unsigned int get_id_from_type()
314 {
315 return id_from_type<T>::value;
316 }
317 };
318
319 //***************************************************************************
320 // For 16 types.
321 //***************************************************************************
322 template <typename T1,
339 {
340 public:
341
342 //************************************
343 template <typename T>
345 {
346 typedef
363 etl::null_type<0> >::type>::type>::type>::type>::type>::type>::type>::type>
364 ::type>::type>::type>::type>::type>::type>::type>::type type;
365
366 ETL_STATIC_ASSERT(!(etl::is_same<etl::null_type<0>, type>::value), "Invalid type");
367 };
368 };
369
370#endif
371}
372
373#endif
Definition integral_limits.h:516
conditional
Definition type_traits_generator.h:1155
is_same
Definition type_traits_generator.h:1036
bitset_ext
Definition absolute.h:38
pair holds two objects of arbitrary type
Definition utility.h:164
Definition type_lookup.h:278
Definition type_lookup.h:244
Definition type_lookup.h:238
Definition type_lookup.h:345
Definition type_lookup.h:339