-
Notifications
You must be signed in to change notification settings - Fork 1
/
utilities.hpp
213 lines (169 loc) · 6.12 KB
/
utilities.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#ifndef JSON_DESERIALISE_UTILITIES_H
#define JSON_DESERIALISE_UTILITIES_H
#include <type_traits>
namespace JsonDeserialise {
// unwrap a pack that has only one type
template <typename TypeTupleFront, typename...>
struct PackToType;
template <typename TypeTupleFront>
struct PackToType<TypeTupleFront> {
using Type = TypeTupleFront;
};
template <auto member_ptr, typename = decltype(member_ptr)>
struct MemberPtrToType;
template <typename T, typename ObjectType, auto member_ptr>
struct MemberPtrToType<member_ptr, T ObjectType::*> {
using Type = T;
};
template <typename Function>
struct ArgTypeDeduction : public ArgTypeDeduction<decltype(&Function::operator())> {};
template <typename R, typename C, typename Arg, typename... Args>
struct ArgTypeDeduction<R (C::*)(Arg, Args...) const> {
using Type = Arg;
using Return = R;
};
template <int... pack>
struct ConstexprArrayPack {
constexpr static int length = sizeof...(pack);
constexpr static int value[sizeof...(pack)] = {pack...};
};
template <>
struct ConstexprArrayPack<> {
constexpr static int length = 0;
};
template <int N, int current = 0, int... pack>
struct ConstexprIncArray : public ConstexprIncArray<N, (current + 1), pack..., current> {};
template <int N, int... pack>
struct ConstexprIncArray<N, N, pack...> : public ConstexprArrayPack<pack...> {
using Type = ConstexprArrayPack<pack...>;
};
template <int index, typename Tuple>
struct GetType : GetType<(index - 1), typename Tuple::Next> {};
template <typename Tuple>
struct GetType<0, Tuple> {
using Type = typename Tuple::CurrentType;
};
template <typename Tuple, typename T, int index = 0,
bool = std::is_same_v<T, typename GetType<index, Tuple>::Type>,
typename = std::enable_if_t<(index < Tuple::length)>>
struct FindType : public FindType<Tuple, T, (index + 1)> {};
template <typename Tuple, typename T, int Index>
struct FindType<Tuple, T, Index, true> {
static constexpr int index = Index;
};
template <typename Current = void, typename... Types>
struct TypeTuple {
using Next = TypeTuple<Types...>;
using CurrentType = Current;
static constexpr int length = sizeof...(Types) + 1;
};
template <typename Current>
struct TypeTuple<Current> {
using CurrentType = Current;
static constexpr int length = 1;
};
template <>
struct TypeTuple<void> {
static constexpr int length = 0;
};
template <typename Current, template <typename> typename F>
struct TypeTupleMap;
template <typename... Args, template <typename> typename F>
struct TypeTupleMap<TypeTuple<Args...>, F> {
using Type = TypeTuple<typename F<Args>::Type...>;
};
template <int n, typename Source,
typename Result = std::enable_if_t<(n >= 0 && n < Source::length), TypeTuple<void>>>
struct TypeTupleFront;
template <int n, typename T, typename... Args, typename... Last>
struct TypeTupleFront<n, TypeTuple<T, Args...>, TypeTuple<Last...>> {
using Right = TypeTuple<Args...>;
using Left = TypeTuple<Last..., T>;
using Type = typename TypeTupleFront<(n - 1), Right, Left>::Type;
};
template <int n, typename T, typename... Args>
struct TypeTupleFront<n, TypeTuple<T, Args...>, TypeTuple<void>> {
using Right = TypeTuple<Args...>;
using Left = TypeTuple<T>;
using Type = typename TypeTupleFront<(n - 1), Right, Left>::Type;
};
template <typename T, typename... Args, typename... Last>
struct TypeTupleFront<0, TypeTuple<T, Args...>, TypeTuple<Last...>> {
using Right = TypeTuple<Args...>;
using Left = TypeTuple<Last..., T>;
using Type = TypeTupleFront;
};
template <typename T, typename... Args>
struct TypeTupleFront<0, TypeTuple<T, Args...>, TypeTuple<void>> {
using Right = TypeTuple<Args...>;
using Left = TypeTuple<T>;
using Type = TypeTupleFront;
};
template <typename Left, typename Right>
struct TypeTupleMerge;
template <typename... Left, typename... Right>
struct TypeTupleMerge<TypeTuple<Left...>, TypeTuple<Right...>> {
using Type = TypeTuple<Left..., Right...>;
};
template <typename... Left>
struct TypeTupleMerge<TypeTuple<Left...>, TypeTuple<void>> {
using Type = TypeTuple<Left...>;
};
template <typename... Right>
struct TypeTupleMerge<TypeTuple<void>, TypeTuple<Right...>> {
using Type = TypeTuple<Right...>;
};
template <>
struct TypeTupleMerge<TypeTuple<void>, TypeTuple<void>> {
using Type = TypeTuple<void>;
};
template <int n, typename Source, typename Value,
typename = std::enable_if_t<(n < Source::length && n >= 0)>>
struct TypeTupleAlter;
template <int n, typename Value, typename... Args>
struct TypeTupleAlter<n, TypeTuple<Args...>, Value> {
using Loc = typename TypeTupleFront<(n - 1), TypeTuple<Args...>>::Type;
using Type =
typename TypeTupleMerge<typename Loc::Left,
typename TypeTupleAlter<0, typename Loc::Right, Value>::Type>::Type;
};
template <typename Value, typename T, typename... Args>
struct TypeTupleAlter<0, TypeTuple<T, Args...>, Value> {
using Type = TypeTuple<Value, Args...>;
};
template <typename Current = void, typename... Types>
struct ConstexprTuple {
Current value;
using Next = ConstexprTuple<Types...>;
Next next;
using CurrentType = std::decay_t<Current>;
constexpr ConstexprTuple(Current source, Types... pack) : value{source}, next{pack...} {}
template <int index>
constexpr inline const typename GetType<index, ConstexprTuple>::Type get() const {
if constexpr (index == 0)
return value;
else
return next.template get<index - 1>();
}
};
template <typename Current>
struct ConstexprTuple<Current> {
Current value;
using CurrentType = std::decay_t<Current>;
constexpr ConstexprTuple(Current source) : value{source} {}
template <int index, typename = std::enable_if_t<index == 0>>
constexpr inline const CurrentType get() const {
return value;
}
};
template <>
struct ConstexprTuple<void> {
template <int index, typename = std::enable_if_t<index == 0>>
constexpr inline decltype(auto) get() const {
return nullptr;
}
};
template <typename... Types>
ConstexprTuple(Types...) -> ConstexprTuple<Types...>;
} // namespace JsonDeserialise
#endif