-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypeList.h
56 lines (42 loc) · 1.49 KB
/
TypeList.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <cstddef>
#include <type_traits>
using id_type = int;
template <typename... Ts> class TypeList;
template <typename... Ts> class HasRepeatedParameter;
template <typename T, typename... Ts> class HasRepeatedParameter<T, T, Ts...> {
public:
static constexpr bool value = true;
};
template <typename T1, typename T2, typename... Ts> class HasRepeatedParameter<T1, T2, Ts...> {
public:
static constexpr bool value = HasRepeatedParameter<T1, Ts...>::value || HasRepeatedParameter<T2, Ts...>::value;
};
template <typename T> class HasRepeatedParameter<T> {
public:
static constexpr bool value = false;
};
template <> class TypeList<> {
public:
//This will be useful later
static constexpr id_type id = -1;
template <typename T> static constexpr id_type index_of() { return id; }
};
template <typename T, typename... Ts> class TypeList<T, Ts...> {
// Here we have our check
static_assert(!HasRepeatedParameter<T, Ts...>::value, "TypeList can't have repeated parameters");
using remaining_types = TypeList<Ts...>;
static constexpr id_type id = sizeof...(Ts);
template<typename U, bool dummy>
struct Helper {
static constexpr id_type value = remaining_types::template index_of<U>();
};
template<bool dummy>
struct Helper<T, dummy> {
static constexpr id_type value = id;
};
public:
// Returns -1 when the type is not found
template <typename F> static constexpr id_type index_of() {
return Helper<F, true>::value;
};
};