Embedded Template Library 1.0
Loading...
Searching...
No Matches
type_lookup_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#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/*[[[cog
41import cog
42cog.outl("#if 0")
43]]]*/
44/*[[[end]]]*/
45#error THIS HEADER IS A GENERATOR. DO NOT INCLUDE.
46/*[[[cog
47import cog
48cog.outl("#endif")
49]]]*/
50/*[[[end]]]*/
51
52/*[[[cog
53import cog
54cog.outl("//***************************************************************************")
55cog.outl("// THIS FILE HAS BEEN AUTO GENERATED. DO NOT EDIT THIS FILE.")
56cog.outl("//***************************************************************************")
57]]]*/
58/*[[[end]]]*/
59
60namespace etl
61{
62 //***************************************************************************
64 //***************************************************************************
65 template <typename T, int ID_>
67 {
68 typedef T type;
69
70 enum
71 {
72 ID = ID_
73 };
74 };
75
76 //***************************************************************************
78 //***************************************************************************
79 template <typename T1, typename T2>
81 {
82 typedef T1 type1;
83 typedef T2 type2;
84 };
85
86#if ETL_USING_CPP11 && !defined(ETL_TYPE_SELECT_FORCE_CPP03_IMPLEMENTATION)
87 //***************************************************************************
88 // type_id_lookup
89 //***************************************************************************
90 template <typename... TTypes>
91 struct type_id_lookup
92 {
93 private:
94
95 // The type for no match.
96 struct nulltype {};
97
98 // For N type pairs.
99 template <size_t ID, typename T1, typename... TRest>
100 struct type_from_id_helper
101 {
102 using type = typename etl::conditional<ID == T1::ID,
103 typename T1::type,
104 typename type_from_id_helper<ID, TRest...>::type>::type;
105 };
106
107 // Specialisation for 1 type pair.
108 template <size_t ID, typename T1>
109 struct type_from_id_helper<ID, T1>
110 {
111 using type = typename etl::conditional<ID == T1::ID,
112 typename T1::type,
113 nulltype>::type;
114 };
115
116 public:
117
118 //************************************
119 // type_from_id
120 //************************************
121 template <int ID>
122 struct type_from_id
123 {
124 using type = typename type_from_id_helper<ID, TTypes...>::type;
125
126 static_assert(!(etl::is_same<nulltype, type>::value), "Invalid id");
127 };
128
129 template <int ID>
130 using type_from_id_t = typename type_from_id<ID>::type;
131
132 private:
133
134 static constexpr size_t UNKNOWN = etl::integral_limits<size_t>::max;
135
136 // For N type pairs.
137 template <typename T, typename T1, typename... TRest>
138 struct id_from_type_helper
139 {
140 static constexpr size_t value = etl::is_same<T, typename T1::type>::value ? size_t(T1::ID) : id_from_type_helper<T, TRest...>::value;
141 };
142
143 // Specialisation for 1 type pair.
144 template <typename T, typename T1>
145 struct id_from_type_helper<T, T1>
146 {
147 static constexpr size_t value = etl::is_same<T, typename T1::type>::value ? size_t(T1::ID) : UNKNOWN;
148 };
149
150 public:
151
152 //************************************
153 // id_from_type
154 //************************************
155 template <typename T>
156 struct id_from_type
157 {
158 static constexpr size_t value = id_from_type_helper<T, TTypes...>::value;
159
160 static_assert(value != UNKNOWN, "Invalid type");
161 };
162
163#if ETL_USING_CPP17
164 template <typename T>
165 static constexpr size_t id_from_type_v = id_from_type<T>::value;
166#endif
167
168 //************************************
169 template <typename T>
170 static unsigned int get_id_from_type(const T&)
171 {
172 return get_id_from_type<T>();
173 }
174
175 //************************************
176 template <typename T>
177 static unsigned int get_id_from_type()
178 {
179 return id_from_type<T>::value;
180 }
181 };
182
183 //***************************************************************************
184 // type_type_lookup
185 //***************************************************************************
186 template <typename... TTypes>
187 class type_type_lookup
188 {
189 private:
190
191 // The type for no match.
192 struct nulltype {};
193
194 template <typename T, typename T1, typename... TRest>
195 struct type_from_type_helper
196 {
198 typename T1::type2,
199 typename type_from_type_helper<T, TRest...>::type>::type;
200 };
201
202 template <typename T, typename T1>
203 struct type_from_type_helper<T, T1>
204 {
206 typename T1::type2,
207 nulltype>::type;
208 };
209
210 public:
211
212 template <typename T>
213 class type_from_type
214 {
215 public:
216
217 // The matched type or nulltype
218 using type = typename type_from_type_helper<T, TTypes...>::type;
219
220 static_assert(!etl::is_same<type, nulltype>::value, "Type match not found");
221 };
222
223 // Template alias.
224 template <typename T>
225 using type_from_type_t = typename type_from_type<T>::type;
226 };
227
228#else
229
230 /*[[[cog
231 import cog
232 cog.outl("//***************************************************************************")
233 cog.outl("// For %s types." % int(NTypes))
234 cog.outl("//***************************************************************************")
235 cog.outl("template <typename T1,")
236 for n in range(2, int(NTypes)):
237 cog.outl(" typename T%s = etl::type_id_pair<etl::null_type<0>, -%s>," %(n, n))
238 cog.outl(" typename T%s = etl::type_id_pair<etl::null_type<0>, -%s> >" %(NTypes, NTypes))
239 cog.outl("struct type_id_lookup")
240 cog.outl("{")
241 cog.outl("public:")
242 cog.outl("")
243 cog.outl(" //************************************")
244 cog.outl(" template <int ID>")
245 cog.outl(" struct type_from_id")
246 cog.outl(" {")
247 cog.outl(" typedef ")
248 for n in range(1, int(NTypes) + 1):
249 cog.outl(" typename etl::conditional<ID == T%s::ID, typename T%s::type," %(n, n))
250 cog.out(" etl::null_type<0> >")
251 for n in range(1, int(NTypes) + 1):
252 if n == int(NTypes):
253 cog.outl("::type type;")
254 else:
255 cog.out("::type>")
256 if n % 4 == 0:
257 if n != int(NTypes):
258 cog.outl("")
259 cog.out(" ")
260 cog.outl("")
261 cog.outl(" ETL_STATIC_ASSERT(!(etl::is_same<etl::null_type<0>, type>::value), \"Invalid id\");")
262 cog.outl(" };")
263 cog.outl("")
264 cog.outl(" //************************************")
265 cog.outl(" enum")
266 cog.outl(" {")
267 cog.outl(" UNKNOWN = UINT_MAX")
268 cog.outl(" };")
269 cog.outl("")
270 cog.outl(" template <typename T>")
271 cog.outl(" struct id_from_type")
272 cog.outl(" {")
273 cog.outl(" enum")
274 cog.outl(" {")
275 cog.outl(" value =")
276 for n in range(1, int(NTypes) + 1) :
277 cog.outl(" (unsigned int) etl::is_same<T, typename T%s::type>::value ? (unsigned int)T%s::ID :" % (n, n))
278 cog.outl(" (unsigned int) UNKNOWN")
279 cog.outl(" };")
280 cog.outl("")
281 cog.outl(" ETL_STATIC_ASSERT(((unsigned int)value != (unsigned int)UNKNOWN), \"Invalid type\");")
282 cog.outl(" };")
283 cog.outl("")
284 cog.outl(" //************************************")
285 cog.outl(" template <typename T>")
286 cog.outl(" static unsigned int get_id_from_type(const T&)")
287 cog.outl(" {")
288 cog.outl(" return get_id_from_type<T>();")
289 cog.outl(" }")
290 cog.outl("")
291 cog.outl(" //************************************")
292 cog.outl(" template <typename T>")
293 cog.outl(" static unsigned int get_id_from_type()")
294 cog.outl(" {")
295 cog.outl(" return id_from_type<T>::value;")
296 cog.outl(" }")
297 cog.outl("};")
298 cog.outl("")
299 cog.outl("//***************************************************************************")
300 cog.outl("// For %s types." % int(NTypes))
301 cog.outl("//***************************************************************************")
302 cog.outl("template <typename T1,")
303 for n in range(2, int(NTypes)):
304 cog.outl(" typename T%s = etl::type_type_pair<etl::null_type<0>, etl::null_type<0> >," %n)
305 cog.outl(" typename T%s = etl::type_type_pair<etl::null_type<0>, etl::null_type<0> > >" %NTypes)
306 cog.outl("struct type_type_lookup")
307 cog.outl("{")
308 cog.outl("public:")
309 cog.outl("")
310 cog.outl(" //************************************")
311 cog.outl(" template <typename T>")
312 cog.outl(" struct type_from_type")
313 cog.outl(" {")
314 cog.outl(" typedef ")
315 for n in range(1, int(NTypes) + 1):
316 cog.outl(" typename etl::conditional<etl::is_same<T, typename T%s::type1>::value, typename T%s::type2," %(n, n))
317 cog.out(" etl::null_type<0> >")
318 for n in range(1, int(NTypes) + 1):
319 if n == int(NTypes):
320 cog.outl("::type type;")
321 else:
322 cog.out("::type>")
323 if n % 8 == 0:
324 if n != int(NTypes):
325 cog.outl("")
326 cog.out(" ")
327 cog.outl("")
328 cog.outl(" ETL_STATIC_ASSERT(!(etl::is_same<etl::null_type<0>, type>::value), \"Invalid type\");")
329 cog.outl(" };")
330 cog.outl("};")
331 ]]]*/
332 /*[[[end]]]*/
333
334#endif
335}
336
337#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:238
The type/id pair type to use for type/id lookup template parameters.
Definition type_lookup_generator.h:67
The type/type pair type to use for type/type lookup template parameters.
Definition type_lookup_generator.h:81