-
Notifications
You must be signed in to change notification settings - Fork 1
/
xx_typetraits.h
342 lines (285 loc) · 14.7 KB
/
xx_typetraits.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#pragma once
#include "xx_includes.h"
namespace xx {
/************************************************************************************/
// template is same checkers
template< template<typename...>typename T> struct Template_t {};
template<typename T> struct TemplateExt_t {
static constexpr bool isTemplate{ false };
};
template< template<typename...> typename T, typename...Args> struct TemplateExt_t<T<Args...>> {
static constexpr bool isTemplate{ true };
using Type = Template_t<T>;
};
template<typename T, typename U> constexpr bool TemplateIsSame() {
if constexpr (TemplateExt_t<T>::isTemplate != TemplateExt_t<U>::isTemplate) return false;
else return std::is_same_v<typename TemplateExt_t<T>::Type, typename TemplateExt_t<U>::Type>;
}
template<typename T, typename U> constexpr bool TemplateIsSame_v = TemplateIsSame<T, U>();
template <template <typename, auto> typename T> struct TemplateS_t {};
template <template <typename, auto> typename T, typename E, auto S> struct TemplateExt_t<T<E, S>> {
static constexpr bool isTemplate = true;
using Type = TemplateS_t<T>;
};
struct AnyType {}; // helper type
template<typename T> constexpr bool IsStdOptional_v = TemplateIsSame_v<std::remove_cvref_t<T>, std::optional<AnyType>>;
template<typename T> constexpr bool IsStdPair_v = TemplateIsSame_v<std::remove_cvref_t<T>, std::pair<AnyType, AnyType>>;
template<typename T> constexpr bool IsStdTuple_v = TemplateIsSame_v<std::remove_cvref_t<T>, std::tuple<AnyType>>;
template<typename T> constexpr bool IsStdVariant_v = TemplateIsSame_v<std::remove_cvref_t<T>, std::variant<AnyType>>;
template<typename T> constexpr bool IsStdUniquePtr_v = TemplateIsSame_v<std::remove_cvref_t<T>, std::unique_ptr<AnyType>>;
template<typename T> constexpr bool IsStdVector_v = TemplateIsSame_v<std::remove_cvref_t<T>, std::vector<AnyType>>;
template<typename T> constexpr bool IsStdList_v = TemplateIsSame_v<std::remove_cvref_t<T>, std::list<AnyType>>;
template<typename T> constexpr bool IsStdSet_v = TemplateIsSame_v<std::remove_cvref_t<T>, std::set<AnyType>>;
template<typename T> constexpr bool IsStdUnorderedSet_v = TemplateIsSame_v<std::remove_cvref_t<T>, std::unordered_set<AnyType>>;
template<typename T> constexpr bool IsStdMap_v = TemplateIsSame_v<std::remove_cvref_t<T>, std::map<AnyType, AnyType>>;
template<typename T> constexpr bool IsStdUnorderedMap_v = TemplateIsSame_v<std::remove_cvref_t<T>, std::unordered_map<AnyType, AnyType>>;
template<typename T> constexpr bool IsStdQueue_v = TemplateIsSame_v<std::remove_cvref_t<T>, std::queue<AnyType>>;
template<typename T> constexpr bool IsStdDeque_v = TemplateIsSame_v<std::remove_cvref_t<T>, std::deque<AnyType>>;
template<typename T> constexpr bool IsStdTimepoint_v = TemplateIsSame_v<std::remove_cvref_t<T>, std::chrono::time_point<AnyType, AnyType>>;
template<typename T> constexpr bool IsStdArray_v = TemplateIsSame_v<std::remove_cvref_t<T>, std::array<AnyType, 1>>;
// ...
template<typename T> constexpr bool IsStdSetLike_v = IsStdSet_v<T> || IsStdUnorderedSet_v<T>;
template<typename T> constexpr bool IsStdMapLike_v = IsStdMap_v<T> || IsStdUnorderedMap_v<T>;
template<typename T> constexpr bool IsStdQueueLike_v = IsStdQueue_v<T> || IsStdDeque_v<T>;
// ...
template<typename T> constexpr bool IsFromTo_v = TemplateIsSame_v<std::remove_cvref_t<T>, FromTo<AnyType>>;
template<typename T> constexpr bool IsCurrentMax_v = TemplateIsSame_v<std::remove_cvref_t<T>, CurrentMax<AnyType>>;
// ...
/************************************************************************************/
// std::is_pod like, flag container can memcpy or memmove
template<typename T, typename ENABLED = void>
struct IsPod : std::false_type {};
template<typename T>
struct IsPod<T, std::enable_if_t<std::is_standard_layout_v<T>&& std::is_trivial_v<T>>> : std::true_type {};
template<typename T> constexpr bool IsPod_v = IsPod<std::remove_cvref_t<T>>::value;
/************************************************************************************/
// check T is literal "xxxxx" strings
template<typename T, typename = void> struct IsLiteral : std::false_type {};
template<size_t L> struct IsLiteral<char[L], void> : std::true_type {
static const size_t len = L;
};
template<size_t L> struct IsLiteral<char const [L], void> : std::true_type {
static const size_t len = L;
};
template<size_t L> struct IsLiteral<char const (&)[L], void> : std::true_type {
static const size_t len = L;
};
template<typename T> constexpr bool IsLiteral_v = IsLiteral<T>::value;
template<typename T> constexpr size_t LiteralLen = IsLiteral<T>::len;
/************************************************************************************/
// check T has member funcs: data() size()
template<typename, typename = void> struct IsContainer : std::false_type {};
template<typename T> struct IsContainer<T, std::void_t<decltype(std::declval<T>().data()), decltype(std::declval<T>().size())>> : std::true_type {};
template<typename T>
constexpr bool IsContainer_v = IsContainer<T>::value;
/************************************************************************************/
// check T has member func: operator()
template<typename T, typename = void> struct IsCallable : std::is_function<T> {};
template<typename T> struct IsCallable<T, typename std::enable_if<std::is_same<decltype(void(&T::operator())), void>::value>::type> : std::true_type {};
template<typename T> constexpr bool IsCallable_v = IsCallable<T>::value;
/************************************************************************************/
// check T is std::function
template<typename> struct IsFunction : std::false_type {};
template<typename T> struct IsFunction<std::function<T>> : std::true_type {
using FT = T;
};
template<typename T> constexpr bool IsFunction_v = IsFunction<std::remove_cvref_t<T>>::value;
/************************************************************************************/
// check tuple contains T
template<typename T, typename Tuple> struct HasType;
template<typename T> struct HasType<T, std::tuple<>> : std::false_type {};
template<typename T, typename U, typename... Ts> struct HasType<T, std::tuple<U, Ts...>> : HasType<T, std::tuple<Ts...>> {};
template<typename T, typename... Ts> struct HasType<T, std::tuple<T, Ts...>> : std::true_type {};
template<typename T, typename Tuple> using TupleContainsType = typename HasType<T, Tuple>::type;
template<typename T, typename Tuple> constexpr bool TupleContainsType_v = TupleContainsType<T, Tuple>::value;
/************************************************************************************/
// return T in tuple's index
template<typename T, typename Tuple> struct TupleTypeIndex;
template<typename T, typename...TS> struct TupleTypeIndex<T, std::tuple<T, TS...>> {
static const size_t value = 0;
};
template<typename T, typename U, typename... TS> struct TupleTypeIndex<T, std::tuple<U, TS...>> {
static const size_t value = 1 + TupleTypeIndex<T, std::tuple<TS...>>::value;
};
template<typename T, typename Tuple> constexpr size_t TupleTypeIndex_v = TupleTypeIndex<T, Tuple>::value;
/************************************************************************************/
// foreach tuple elements
template <typename Tuple, typename F, std::size_t ...Indices>
void ForEachCore(Tuple&& tuple, F&& f, std::index_sequence<Indices...>) {
using swallow = int[];
(void)swallow { 1, (f(std::get<Indices>(std::forward<Tuple>(tuple))), void(), int{})... };
}
template <typename Tuple, typename F>
void ForEach(Tuple&& tuple, F&& f) {
constexpr std::size_t N = std::tuple_size<std::remove_reference_t<Tuple>>::value;
ForEachCore(std::forward<Tuple>(tuple), std::forward<F>(f), std::make_index_sequence<N>{});
}
/************************************************************************************/
// foreach tuple types
/*
xx::ForEachType<Tup>([&]<typename T>() {
// ...
});
*/
template<typename T, typename F>
static inline constexpr void ForEachType(F&& func) {
auto h = []<typename TT, typename FF, size_t... I>(FF && func, std::index_sequence<I...>) {
(func.template operator()<std::tuple_element_t<I, TT>>(), ...);
};
h.template operator()<T> (
std::forward<F>(func),
std::make_index_sequence<std::tuple_size_v<T>>{}
);
}
/************************************************************************************/
// simple tuple memory like array<T, siz>
template<typename ...TS> struct SimpleTuple;
template<typename T> struct SimpleTuple<T> {
T value;
};
template<typename T, typename ...TS> struct SimpleTuple<T, TS...> {
T value;
SimpleTuple<TS...> others;
};
template<typename T, typename...TS>
auto&& Get(xx::SimpleTuple<TS...>& t) {
if constexpr (std::is_same_v< decltype(t.value), T>) {
return t.value;
} else {
return Get<T>(t.others);
}
}
/************************************************************************************/
// ref args[index]
template <int I, typename...Args>
decltype(auto) GetAt(Args&&...args) {
return std::get<I>(std::forward_as_tuple(std::forward<Args>(args)...));
}
/************************************************************************************/
// static comparer func type define
template<typename A, typename B>
using ABComparer = bool (*) (A const& a, B const& b);
/************************************************************************************/
// mapping operator(): return T, container, args, mutable
template<typename T, typename = void>
struct FuncTraits;
template<typename Rtv, typename...Args>
struct FuncTraits<Rtv(*)(Args ...)> {
using R = Rtv;
using A = std::tuple<Args...>;
using A2 = std::tuple<std::decay_t<Args>...>;
using C = void;
static const bool isMutable = true;
};
template<typename Rtv, typename...Args>
struct FuncTraits<Rtv(Args ...)> {
using R = Rtv;
using A = std::tuple<Args...>;
using A2 = std::tuple<std::decay_t<Args>...>;
using C = void;
static const bool isMutable = true;
};
template<typename Rtv, typename CT, typename... Args>
struct FuncTraits<Rtv(CT::*)(Args ...)> {
using R = Rtv;
using A = std::tuple<Args...>;
using A2 = std::tuple<std::decay_t<Args>...>;
using C = CT;
static const bool isMutable = true;
};
template<typename Rtv, typename CT, typename... Args>
struct FuncTraits<Rtv(CT::*)(Args ...) const> {
using R = Rtv;
using A = std::tuple<Args...>;
using A2 = std::tuple<std::decay_t<Args>...>;
using C = CT;
static const bool isMutable = false;
};
template<typename T>
struct FuncTraits<T, std::void_t<decltype(&T::operator())> >
: public FuncTraits<decltype(&T::operator())> {};
template<typename T> using FuncR_t = typename FuncTraits<T>::R;
template<typename T> using FuncA_t = typename FuncTraits<T>::A;
template<typename T> using FuncA2_t = typename FuncTraits<T>::A2;
template<typename T> using FuncC_t = typename FuncTraits<T>::C;
template<typename T> constexpr bool isMutable_v = FuncTraits<T>::isMutable;
/************************************************************************************/
// check T is baseof Template. usage: XX_IsTemplateOf(BT, T)::value
struct IsTemplateOf {
template <template <typename> typename TM, typename T> static std::true_type checkfunc(TM<T>);
template <template <typename> typename TM> static std::false_type checkfunc(...);
template <template <int> typename TM, int N> static std::true_type checkfunc(TM<N>);
template <template <int> typename TM> static std::false_type checkfunc(...);
};
#define XX_IsTemplateOf(TM, ...) decltype(::xx::IsTemplateOf::checkfunc<TM>(std::declval<__VA_ARGS__>()))
/************************************************************************************/
// ctor call helper
template<typename U>
struct TCtor {
U* p;
template<typename...Args>
U& operator()(Args&&...args) {
return *new (p) U(std::forward<Args>(args)...);
}
};
/************************************************************************************/
// literal string's template container
template<size_t n>
struct Str {
char v[n];
constexpr Str(const char(&s)[n]) {
std::copy_n(s, n, v);
}
constexpr std::string_view ToSV() const {
return { v, n - 1 };
}
};
/***********************************************************************************/
// find max size in multi types
template<typename T, typename... Args>
struct MaxSizeof {
static const size_t value = sizeof(T) > MaxSizeof<Args...>::value
? sizeof(T)
: MaxSizeof<Args...>::value;
};
template<typename T>
struct MaxSizeof<T> {
static const size_t value = sizeof(T);
};
template<typename T, typename... Args>
constexpr size_t MaxSizeof_v = MaxSizeof<T, Args...>::value;
/************************************************************************************/
// check T is []{} likely (lambda)( depend on compiler impl )
// unsafe
namespace Detail {
template<Str s, typename T>
inline static constexpr bool LambdaChecker() {
if constexpr (sizeof(s.v) > 8) {
for (std::size_t i = 0; i < sizeof(s.v) - 8; i++) {
if ((s.v[i + 0] == '(' || s.v[i + 0] == '<')
&& s.v[i + 1] == 'l'
&& s.v[i + 2] == 'a'
&& s.v[i + 3] == 'm'
&& s.v[i + 4] == 'b'
&& s.v[i + 5] == 'd'
&& s.v[i + 6] == 'a'
&& (s.v[i + 7] == ' ' || s.v[i + 7] == '(' || s.v[i + 7] == '_')
) return true;
}
}
return false;
}
}
template<typename...T>
constexpr bool IsLambda() {
return ::xx::Detail::LambdaChecker<
#ifdef _MSC_VER
__FUNCSIG__
#else
__PRETTY_FUNCTION__
#endif
, T...>();
}
template<typename ...T>
constexpr bool IsLambda_v = IsLambda<T...>();
}