-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
type_list.hpp
187 lines (136 loc) · 4.96 KB
/
type_list.hpp
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#pragma once
namespace hfsm2 {
namespace detail {
////////////////////////////////////////////////////////////////////////////////
template <typename... Ts>
struct TL_ {
static constexpr LongIndex SIZE = sizeof...(Ts);
};
//------------------------------------------------------------------------------
template <typename...>
struct PrependT;
template <typename T, typename... Ts>
struct PrependT<T, TL_<Ts...>> {
using Type = TL_<T, Ts...>;
};
template <typename... Ts>
using Prepend = typename PrependT<Ts...>::Type;
//------------------------------------------------------------------------------
template <LongIndex, LongIndex, typename...>
struct LesserT;
template <LongIndex H, LongIndex I, typename TFirst, typename... TRest>
struct LesserT<H, I, TFirst, TRest...> {
using Type = typename std::conditional<(I < H),
Prepend<TFirst, typename LesserT<H, I + 1, TRest...>::Type>,
typename LesserT<H, I + 1, TRest...>::Type>::type;
};
template <LongIndex H, LongIndex I>
struct LesserT<H, I> {
using Type = TL_<>;
};
template <typename... Ts>
using SplitL = typename LesserT<sizeof...(Ts) / 2, 0, Ts...>::Type;
//------------------------------------------------------------------------------
template <LongIndex, LongIndex, typename...>
struct GreaterT;
template <LongIndex H, LongIndex I, typename TFirst, typename... TRest>
struct GreaterT<H, I, TFirst, TRest...> {
using Type = typename std::conditional<(I < H),
typename GreaterT<H, I + 1, TRest...>::Type,
TL_<TFirst, TRest...>>::type;
};
template <LongIndex H, LongIndex I>
struct GreaterT<H, I> {
using Type = TL_<>;
};
template <typename... Ts>
using SplitR = typename GreaterT<sizeof...(Ts) / 2, 0, Ts...>::Type;
////////////////////////////////////////////////////////////////////////////////
template<LongIndex N>
struct IndexConstant {};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template<LongIndex... Ns>
struct IndexSequence {
using Type = IndexSequence<Ns...>;
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template<typename, typename>
struct MakeIndexSequence_Impl {};
template<LongIndex N, LongIndex... Ns>
struct MakeIndexSequence_Impl<IndexConstant<N>,
IndexSequence<Ns...>>
: MakeIndexSequence_Impl<IndexConstant<N - 1>,
IndexSequence<N - 1, Ns...>>
{};
template<LongIndex... Ns>
struct MakeIndexSequence_Impl<IndexConstant<0>,
IndexSequence<Ns...>>
: IndexSequence<Ns...>
{};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template<LongIndex N>
using MakeIndexSequence = typename MakeIndexSequence_Impl<IndexConstant<N>,
IndexSequence<>>::Type;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template<class... Ts>
using IndexSequenceFor = MakeIndexSequence<sizeof...(Ts)>;
////////////////////////////////////////////////////////////////////////////////
template <typename T>
struct IndexedTypeList_EntryT {};
template <typename T, LongIndex N>
struct IndexedTypeList_EntryN
: IndexedTypeList_EntryT<T>
{};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template <typename...>
struct IndexedTypeList_Impl;
template <LongIndex... Ns, typename... Ts>
struct IndexedTypeList_Impl<IndexSequence<Ns...>, Ts...>
: IndexedTypeList_EntryN<Ts, Ns>...
{
template <typename T, LongIndex N>
static constexpr LongIndex select(IndexedTypeList_EntryN<T, N>) { return (LongIndex) N; }
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template <typename... Ts>
struct ITL_
: private IndexedTypeList_Impl<IndexSequenceFor<Ts...>, Ts...>
{
using Base = IndexedTypeList_Impl<IndexSequenceFor<Ts...>, Ts...>;
static constexpr LongIndex SIZE = sizeof...(Ts);
template <typename T>
static constexpr bool contains() { return std::is_base_of<IndexedTypeList_EntryT<T>, ITL_>::value; }
template <typename T>
static constexpr
typename std::enable_if< std::is_base_of<IndexedTypeList_EntryT<T>, ITL_>::value, LongIndex>::type
index() {
return Base::template select<T>(ITL_{});
}
template <typename T>
static constexpr
typename std::enable_if<!std::is_base_of<IndexedTypeList_EntryT<T>, ITL_>::value, LongIndex>::type
index() {
return INVALID_LONG_INDEX;
}
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template <typename...>
struct IndexedT;
template <typename... Ts>
struct IndexedT<TL_<Ts...>> {
using Type = ITL_<Ts...>;
};
template <typename T>
using Indexed = typename IndexedT<T>::Type;
//------------------------------------------------------------------------------
template <typename...>
struct MergeT;
template <typename... Ts1, typename... Ts2>
struct MergeT<TL_<Ts1...>, TL_<Ts2...>> {
using TypeList = TL_<Ts1..., Ts2...>;
};
template <typename... Ts>
using Merge = typename MergeT<Ts...>::TypeList;
////////////////////////////////////////////////////////////////////////////////
}
}