Embedded Template Library  1.0
type_lookup.h
1 /******************************************************************************
2 The MIT License(MIT)
3 
4 Embedded Template Library.
5 https://github.com/ETLCPP/etl
6 https://www.etlcpp.com
7 
8 Copyright(c) 2017 jwellbelove
9 
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files(the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions :
16 
17 The above copyright notice and this permission notice shall be included in all
18 copies or substantial portions of the Software.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 SOFTWARE.
27 ******************************************************************************/
28 
29 #ifndef ETL_TYPE_LOOKUP_INCLUDED
30 #define ETL_TYPE_LOOKUP_INCLUDED
31 
32 #include <limits.h>
33 
34 #include "platform.h"
35 #include "type_traits.h"
36 #include "static_assert.h"
37 #include "integral_limits.h"
38 #include "null_type.h"
39 
40 #undef ETL_FILE
41 #define ETL_FILE "45"
42 
43 #if 0
44 #error THIS HEADER IS A GENERATOR. DO NOT INCLUDE.
45 #endif
46 
47 //***************************************************************************
48 // THIS FILE HAS BEEN AUTO GENERATED. DO NOT EDIT THIS FILE.
49 //***************************************************************************
50 
51 namespace etl
52 {
53  //***************************************************************************
55  //***************************************************************************
56  template <typename T, int ID_>
57  struct type_id_pair
58  {
59  typedef T type;
60 
61  enum
62  {
63  ID = ID_
64  };
65  };
66 
67  //***************************************************************************
69  //***************************************************************************
70  template <typename T1, typename T2>
71  struct type_type_pair
72  {
73  typedef T1 type1;
74  typedef T2 type2;
75  };
76 
77 #if ETL_CPP11_SUPPORTED && !defined(ETL_TYPE_SELECT_FORCE_CPP03)
78  //***************************************************************************
79  // type_id_lookup
80  //***************************************************************************
81  template <typename... TTypes>
82  struct type_id_lookup
83  {
84  private:
85 
86  // The type for no match.
87  struct nulltype {};
88 
89  // For N type pairs.
90  template <size_t ID, typename T1, typename... TRest>
91  struct type_from_id_helper
92  {
93  using type = typename etl::conditional<ID == T1::ID,
94  typename T1::type,
95  typename type_from_id_helper<ID, TRest...>::type>::type;
96  };
97 
98  // Specialisation for 1 type pair.
99  template <size_t ID, typename T1>
100  struct type_from_id_helper<ID, T1>
101  {
102  using type = typename etl::conditional<ID == T1::ID,
103  typename T1::type,
104  nulltype>::type;
105  };
106 
107  public:
108 
109  //************************************
110  // type_from_id
111  //************************************
112  template <int ID>
113  struct type_from_id
114  {
115  using type = typename type_from_id_helper<ID, TTypes...>::type;
116 
117  static_assert(!(etl::is_same<nulltype, type>::value), "Invalid id");
118  };
119 
120 #if ETL_CPP14_SUPPORTED
121  template <int ID>
122  using type_from_id_t = typename type_from_id<ID>::type;
123 #endif
124 
125  private:
126 
127  static constexpr size_t UNKNOWN = etl::integral_limits<size_t>::max;
128 
129  // For N type pairs.
130  template <typename T, typename T1, typename... TRest>
131  struct id_from_type_helper
132  {
133  static constexpr size_t value = etl::is_same<T, typename T1::type>::value ? T1::ID : id_from_type_helper<T, TRest...>::value;
134  };
135 
136  // Specialisation for 1 type pair.
137  template <typename T, typename T1>
138  struct id_from_type_helper<T, T1>
139  {
140  static constexpr size_t value = etl::is_same<T, typename T1::type>::value ? T1::ID : UNKNOWN;
141  };
142 
143  public:
144 
145  //************************************
146  // id_from_type
147  //************************************
148  template <typename T>
149  struct id_from_type
150  {
151  static constexpr size_t value = id_from_type_helper<T, TTypes...>::value;
152 
153  static_assert(value != UNKNOWN, "Invalid type");
154  };
155 
156 #if ETL_CPP17_SUPPORTED
157  template <typename T>
158  static constexpr size_t id_from_type_v = id_from_type<T>::value;
159 #endif
160 
161  //************************************
162  template <typename T>
163  static unsigned int get_id_from_type(const T&)
164  {
165  return get_id_from_type<T>();
166  }
167 
168  //************************************
169  template <typename T>
170  static unsigned int get_id_from_type()
171  {
172  return id_from_type<T>::value;
173  }
174  };
175 
176  //***************************************************************************
177  // type_type_lookup
178  //***************************************************************************
179  template <typename... TTypes>
180  class type_type_lookup
181  {
182  private:
183 
184  // The type for no match.
185  struct nulltype {};
186 
187  template <typename T, typename T1, typename... TRest>
188  struct type_from_type_helper
189  {
191  typename T1::type2,
192  typename type_from_type_helper<T, TRest...>::type>::type;
193  };
194 
195  template <typename T, typename T1>
196  struct type_from_type_helper<T, T1>
197  {
199  typename T1::type2,
200  nulltype>::type;
201  };
202 
203  public:
204 
205  template <typename T>
206  class type_from_type
207  {
208  public:
209 
210  // The matched type or nulltype
211  using type = typename type_from_type_helper<T, TTypes...>::type;
212 
213  static_assert(!etl::is_same<type, nulltype>::value, "Type match not found");
214  };
215 
216 #if ETL_CPP14_SUPPORTED
217  // Template alias.
218  template <typename T>
219  using type_from_type_t = typename type_from_type<T>::type;
220 #endif
221  };
222 
223 #else
224 
225  //***************************************************************************
226  // For 16 types.
227  //***************************************************************************
228  template <typename T1,
229  typename T2 = etl::type_id_pair<etl::null_type<0>, -2>,
230  typename T3 = etl::type_id_pair<etl::null_type<0>, -3>,
231  typename T4 = etl::type_id_pair<etl::null_type<0>, -4>,
232  typename T5 = etl::type_id_pair<etl::null_type<0>, -5>,
233  typename T6 = etl::type_id_pair<etl::null_type<0>, -6>,
234  typename T7 = etl::type_id_pair<etl::null_type<0>, -7>,
235  typename T8 = etl::type_id_pair<etl::null_type<0>, -8>,
236  typename T9 = etl::type_id_pair<etl::null_type<0>, -9>,
237  typename T10 = etl::type_id_pair<etl::null_type<0>, -10>,
238  typename T11 = etl::type_id_pair<etl::null_type<0>, -11>,
239  typename T12 = etl::type_id_pair<etl::null_type<0>, -12>,
240  typename T13 = etl::type_id_pair<etl::null_type<0>, -13>,
241  typename T14 = etl::type_id_pair<etl::null_type<0>, -14>,
242  typename T15 = etl::type_id_pair<etl::null_type<0>, -15>,
243  typename T16 = etl::type_id_pair<etl::null_type<0>, -16> >
245  {
246  public:
247 
248  //************************************
249  template <int ID>
251  {
252  typedef
253  typename etl::conditional<ID == T1::ID, typename T1::type,
254  typename etl::conditional<ID == T2::ID, typename T2::type,
255  typename etl::conditional<ID == T3::ID, typename T3::type,
256  typename etl::conditional<ID == T4::ID, typename T4::type,
257  typename etl::conditional<ID == T5::ID, typename T5::type,
258  typename etl::conditional<ID == T6::ID, typename T6::type,
259  typename etl::conditional<ID == T7::ID, typename T7::type,
260  typename etl::conditional<ID == T8::ID, typename T8::type,
261  typename etl::conditional<ID == T9::ID, typename T9::type,
262  typename etl::conditional<ID == T10::ID, typename T10::type,
263  typename etl::conditional<ID == T11::ID, typename T11::type,
264  typename etl::conditional<ID == T12::ID, typename T12::type,
265  typename etl::conditional<ID == T13::ID, typename T13::type,
266  typename etl::conditional<ID == T14::ID, typename T14::type,
267  typename etl::conditional<ID == T15::ID, typename T15::type,
268  typename etl::conditional<ID == T16::ID, typename T16::type,
273 
274  ETL_STATIC_ASSERT(!(etl::is_same<etl::null_type<0>, type>::value), "Invalid id");
275  };
276 
277  //************************************
278  enum
279  {
280  UNKNOWN = UINT_MAX
281  };
282 
283  template <typename T>
285  {
286  enum
287  {
288  value =
289  (unsigned int) etl::is_same<T, typename T1::type>::value ? T1::ID :
290  (unsigned int) etl::is_same<T, typename T2::type>::value ? T2::ID :
291  (unsigned int) etl::is_same<T, typename T3::type>::value ? T3::ID :
292  (unsigned int) etl::is_same<T, typename T4::type>::value ? T4::ID :
293  (unsigned int) etl::is_same<T, typename T5::type>::value ? T5::ID :
294  (unsigned int) etl::is_same<T, typename T6::type>::value ? T6::ID :
295  (unsigned int) etl::is_same<T, typename T7::type>::value ? T7::ID :
296  (unsigned int) etl::is_same<T, typename T8::type>::value ? T8::ID :
297  (unsigned int) etl::is_same<T, typename T9::type>::value ? T9::ID :
298  (unsigned int) etl::is_same<T, typename T10::type>::value ? T10::ID :
299  (unsigned int) etl::is_same<T, typename T11::type>::value ? T11::ID :
300  (unsigned int) etl::is_same<T, typename T12::type>::value ? T12::ID :
301  (unsigned int) etl::is_same<T, typename T13::type>::value ? T13::ID :
302  (unsigned int) etl::is_same<T, typename T14::type>::value ? T14::ID :
303  (unsigned int) etl::is_same<T, typename T15::type>::value ? T15::ID :
304  (unsigned int) etl::is_same<T, typename T16::type>::value ? T16::ID :
305  (unsigned int) UNKNOWN
306  };
307 
308  ETL_STATIC_ASSERT(((unsigned int)value != (unsigned int)UNKNOWN), "Invalid type");
309  };
310 
311  //************************************
312  template <typename T>
313  static unsigned int get_id_from_type(const T&)
314  {
315  return get_id_from_type<T>();
316  }
317 
318  //************************************
319  template <typename T>
320  static unsigned int get_id_from_type()
321  {
322  return id_from_type<T>::value;
323  }
324  };
325 
326  //***************************************************************************
327  // For 16 types.
328  //***************************************************************************
329  template <typename T1,
346  {
347  public:
348 
349  //************************************
350  template <typename T>
352  {
353  typedef
372 
373  ETL_STATIC_ASSERT(!(etl::is_same<etl::null_type<0>, type>::value), "Invalid type");
374  };
375  };
376 
377 #endif
378 }
379 
380 #undef ETL_FILE
381 
382 #endif
Definition: constant.h:45
Definition: null_type.h:39
Definition: integral_limits.h:54
conditional
Definition: type_traits_generator.h:1202
is_same
Definition: type_traits_generator.h:981
Definition: absolute.h:37
Definition: type_lookup.h:285
Definition: type_lookup.h:251
Definition: type_lookup.h:245
The type/id pair type to use for type/id lookup template parameters.
Definition: type_lookup_generator.h:70
Definition: type_lookup.h:352
Definition: type_lookup.h:346
The type/type pair type to use for type/type lookup template parameters.
Definition: type_lookup_generator.h:84