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 "integral_limits.h"
34#include "null_type.h"
35#include "static_assert.h"
36#include "type_traits.h"
37
38#include <limits.h>
39namespace etl
40{
41 //***************************************************************************
43 //***************************************************************************
44 template <typename T, int ID_>
46 {
47 typedef T type;
48
49 enum
50 {
51 Id = ID_
52 };
53 };
54
55 //***************************************************************************
57 //***************************************************************************
58 template <typename T1, typename T2>
60 {
61 typedef T1 type1;
62 typedef T2 type2;
63 };
64
65#if ETL_USING_CPP11 && !defined(ETL_TYPE_SELECT_FORCE_CPP03_IMPLEMENTATION)
66 //***************************************************************************
67 // type_id_lookup
68 //***************************************************************************
69 template <typename... TTypes>
70 struct type_id_lookup
71 {
72 private:
73
74 // The type for no match.
75 struct nulltype
76 {
77 };
78
79 // For N type pairs.
80 template <int Id, typename T1, typename... TRest>
81 struct type_from_id_helper
82 {
83 using type = typename etl::conditional< Id == T1::Id, typename T1::type, typename type_from_id_helper<Id, TRest...>::type>::type;
84 };
85
86 // Specialisation for 1 type pair.
87 template <int Id, typename T1>
88 struct type_from_id_helper<Id, T1>
89 {
90 using type = typename etl::conditional<Id == T1::Id, typename T1::type, nulltype>::type;
91 };
92
93 public:
94
95 //************************************
96 // type_from_id
97 //************************************
98 template <int Id>
99 struct type_from_id
100 {
101 using type = typename type_from_id_helper<Id, TTypes...>::type;
102
103 static_assert(!(etl::is_same<nulltype, type>::value), "Invalid id");
104 };
105
106 template <int Id>
107 using type_from_id_t = typename type_from_id<Id>::type;
108
109 private:
110
111 static constexpr size_t UNKNOWN = etl::integral_limits<size_t>::max;
112
113 // For N type pairs.
114 template <typename T, typename T1, typename... TRest>
115 struct id_from_type_helper
116 {
117 static constexpr size_t value = etl::is_same<T, typename T1::type>::value ? size_t(T1::Id) : id_from_type_helper<T, TRest...>::value;
118 };
119
120 // Specialisation for 1 type pair.
121 template <typename T, typename T1>
122 struct id_from_type_helper<T, T1>
123 {
124 static constexpr size_t value = etl::is_same<T, typename T1::type>::value ? size_t(T1::Id) : UNKNOWN;
125 };
126
127 public:
128
129 //************************************
130 // id_from_type
131 //************************************
132 template <typename T>
133 struct id_from_type
134 {
135 static constexpr size_t value = id_from_type_helper<T, TTypes...>::value;
136
137 static_assert(value != UNKNOWN, "Invalid type");
138 };
139
140 #if ETL_USING_CPP17
141 template <typename T>
142 static constexpr size_t id_from_type_v = id_from_type<T>::value;
143 #endif
144
145 //************************************
146 template <typename T>
147 static unsigned int get_id_from_type(const T&)
148 {
149 return get_id_from_type<T>();
150 }
151
152 //************************************
153 template <typename T>
154 static unsigned int get_id_from_type()
155 {
156 return id_from_type<T>::value;
157 }
158 };
159
160 //***************************************************************************
161 // type_type_lookup
162 //***************************************************************************
163 template <typename... TTypes>
164 class type_type_lookup
165 {
166 private:
167
168 // The type for no match.
169 struct nulltype
170 {
171 };
172
173 template <typename T, typename T1, typename... TRest>
174 struct type_from_type_helper
175 {
176 using type = typename etl::conditional< etl::is_same<T, typename T1::type1>::value, typename T1::type2,
177 typename type_from_type_helper<T, TRest...>::type>::type;
178 };
179
180 template <typename T, typename T1>
181 struct type_from_type_helper<T, T1>
182 {
183 using type = typename etl::conditional<etl::is_same<T, typename T1::type1>::value, typename T1::type2, nulltype>::type;
184 };
185
186 public:
187
188 template <typename T>
189 class type_from_type
190 {
191 public:
192
193 // The matched type or nulltype
194 using type = typename type_from_type_helper<T, TTypes...>::type;
195
196 static_assert(!etl::is_same<type, nulltype>::value, "Type match not found");
197 };
198
199 // Template alias.
200 template <typename T>
201 using type_from_type_t = typename type_from_type<T>::type;
202 };
203
204#else
205 #include "private/type_lookup_cpp03.h"
206#endif
207} // namespace etl
208
209#endif
Definition integral_limits.h:518
bitset_ext
Definition absolute.h:40
Definition type_lookup.h:60
Definition type_lookup.h:54
The type/id pair type to use for type/id lookup template parameters.
Definition type_lookup.h:46
Definition type_lookup.h:161
Definition type_lookup.h:155
The type/type pair type to use for type/type lookup template parameters.
Definition type_lookup.h:60