Embedded Template Library
1.0
Toggle main menu visibility
Loading...
Searching...
No Matches
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 John Wellbelove
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 "
platform.h
"
33
#include "
integral_limits.h
"
34
#include "null_type.h"
35
#include "
type_traits.h
"
36
37
#include <stddef.h>
38
39
namespace
etl
40
{
41
//***************************************************************************
43
//***************************************************************************
44
template
<
typename
T,
int
ID_>
45
struct
type_id_pair
46
{
47
typedef
T type;
48
49
enum
50
{
51
Id = ID_
52
};
53
};
54
55
//***************************************************************************
57
//***************************************************************************
58
template
<
typename
T1,
typename
T2>
59
struct
type_type_pair
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
etl::integral_limits
Definition
integral_limits.h:517
type
integral_limits.h
etl
Definition
absolute.h:40
platform.h
etl::type
etl::type_id_lookup::type_from_id
Definition
type_lookup.h:60
etl::type_id_lookup
Definition
type_lookup.h:54
etl::type_id_pair
The type/id pair type to use for type/id lookup template parameters.
Definition
type_lookup.h:46
etl::type_type_lookup::type_from_type
Definition
type_lookup.h:161
etl::type_type_lookup
Definition
type_lookup.h:155
etl::type_type_pair
The type/type pair type to use for type/type lookup template parameters.
Definition
type_lookup.h:60
type_traits.h
include
etl
type_lookup.h
Generated on
for Embedded Template Library by
1.17.0