-
Notifications
You must be signed in to change notification settings - Fork 0
/
func_alias.h
92 lines (75 loc) · 4.72 KB
/
func_alias.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
//
// Created by overseven on 13.03.2021.
//
#ifndef TEST_META_UTILS_FUNC_ALIAS_H
#define TEST_META_UTILS_FUNC_ALIAS_H
#include "common.h"
#define UTIL_ALIAS_SIGNATURE_PACK(PackName, ReturnType, ...) \
using Return_##PackName = ReturnType; \
using Args_##PackName = TypeList<__VA_ARGS__>;
#define ALIAS_HAS_FUNC_STRUCT(FuncName) \
template <typename Class, typename Return, typename...T> \
class alias_has_##FuncName; \
\
template <typename Class, typename Return, typename ...Args> \
class alias_has_##FuncName<Class, Return, TypeList<Args...>> \
{ \
typedef char one; \
struct two { char x[2]; }; \
\
template<typename U, Return (U::*f)(Args...)> \
struct SFINAE_NO_CONST {}; \
\
template<typename U, Return (U::*f)(Args...) const> \
struct SFINAE_CONST {}; \
\
template<typename U> \
static one test(SFINAE_NO_CONST<U, &U::FuncName>*){ return one(); };\
\
template<typename U> \
static one test(SFINAE_CONST<U, &U::FuncName>*){ return one(); }; \
\
template <typename C> \
static two test(...){ return two(); } \
\
public: \
enum { value = sizeof(test<Class>(0)) == sizeof(one) }; \
};
#define UTIL_ALIAS(Alias, PackName, ...) \
FOR_EACH(ALIAS_HAS_FUNC_STRUCT, __VA_ARGS__) \
\
template<class Class, typename Return, typename...Args> \
struct Internal_##Alias; \
\
\
\
template<class Class, typename...Args> \
struct Internal_##Alias<Class, Return_##PackName, TypeList<Args...>>{ \
template<class ...> \
static constexpr std::false_type always_false{}; \
static Return_##PackName call(Class &c, Args&&...args) { \
ALIAS_IF_BRANCH(PackName, GET_HEAD(__VA_ARGS__)) \
FOR_EACH_2(ALIAS_ELSE_BRANCH, PackName, 0, GET_TAIL(__VA_ARGS__) ) \
else{ \
static_assert(always_false<Class>, "No one suitable " \
"function is found!"); \
} \
} \
}; \
\
template<class Class, typename ...Args> \
Return_##PackName Alias(Class &c, Args&&...args) { \
return Internal_##Alias<Class, Return_##PackName, \
Args_##PackName>::call(c, std::forward<Args>(args)...); \
}
#define ALIAS_IF_BRANCH(PackName, FuncName) \
if constexpr (ALIAS_HAS_FUNC(PackName, FuncName)){ \
return c.FuncName(std::forward<Args>(args)...); \
}
#define ALIAS_ELSE_BRANCH(PackName, Unused, FuncName) \
else if constexpr (ALIAS_HAS_FUNC(PackName, FuncName)){ \
return c.FuncName(std::forward<Args>(args)...); \
}
#define ALIAS_HAS_FUNC(PackName, FuncName) \
alias_has_##FuncName <Class, Return_##PackName, Args_##PackName>::value
#endif //TEST_META_UTILS_FUNC_ALIAS_H