Flecs
v4.0
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
function_traits.hpp
Go to the documentation of this file.
1
8
namespace
flecs {
9
namespace
_ {
10
11
template
<
typename
... Args>
12
struct
arg_list
{ };
13
14
// Base type that contains the traits
15
template
<
typename
ReturnType,
typename
... Args>
16
struct
function_traits_defs
17
{
18
static
constexpr
bool
is_callable
=
true
;
19
static
constexpr
size_t
arity
=
sizeof
...(Args);
20
using
return_type = ReturnType;
21
using
args
=
arg_list
<Args ...>;
22
};
23
24
// Primary template for function_traits_impl
25
template
<
typename
T>
26
struct
function_traits_impl
{
27
static
constexpr
bool
is_callable
=
false
;
28
};
29
30
// Template specializations for the different kinds of function types (whew)
31
template
<
typename
ReturnType,
typename
... Args>
32
struct
function_traits_impl
<ReturnType(Args...)>
33
:
function_traits_defs
<ReturnType, Args...> {};
34
35
template
<
typename
ReturnType,
typename
... Args>
36
struct
function_traits_impl
<ReturnType(*)(Args...)>
37
:
function_traits_defs
<ReturnType, Args...> {};
38
39
template
<
typename
ClassType,
typename
ReturnType,
typename
... Args>
40
struct
function_traits_impl
<ReturnType(ClassType::*)(Args...)>
41
:
function_traits_defs
<ReturnType, Args...> {};
42
43
template
<
typename
ClassType,
typename
ReturnType,
typename
... Args>
44
struct
function_traits_impl
<ReturnType(ClassType::*)(Args...) const>
45
:
function_traits_defs
<ReturnType, Args...> {};
46
47
template
<
typename
ClassType,
typename
ReturnType,
typename
... Args>
48
struct
function_traits_impl
<ReturnType(ClassType::*)(Args...) const&>
49
:
function_traits_defs
<ReturnType, Args...> {};
50
51
template
<
typename
ClassType,
typename
ReturnType,
typename
... Args>
52
struct
function_traits_impl
<ReturnType(ClassType::*)(Args...) const&&>
53
:
function_traits_defs
<ReturnType, Args...> {};
54
55
template
<
typename
ClassType,
typename
ReturnType,
typename
... Args>
56
struct
function_traits_impl
<ReturnType(ClassType::*)(Args...) volatile>
57
:
function_traits_defs
<ReturnType, Args...> {};
58
59
template
<
typename
ClassType,
typename
ReturnType,
typename
... Args>
60
struct
function_traits_impl
<ReturnType(ClassType::*)(Args...) volatile&>
61
:
function_traits_defs
<ReturnType, Args...> {};
62
63
template
<
typename
ClassType,
typename
ReturnType,
typename
... Args>
64
struct
function_traits_impl
<ReturnType(ClassType::*)(Args...) volatile&&>
65
:
function_traits_defs
<ReturnType, Args...> {};
66
67
template
<
typename
ClassType,
typename
ReturnType,
typename
... Args>
68
struct
function_traits_impl
<ReturnType(ClassType::*)(Args...) const volatile>
69
:
function_traits_defs
<ReturnType, Args...> {};
70
71
template
<
typename
ClassType,
typename
ReturnType,
typename
... Args>
72
struct
function_traits_impl
<ReturnType(ClassType::*)(Args...) const volatile&>
73
:
function_traits_defs
<ReturnType, Args...> {};
74
75
template
<
typename
ClassType,
typename
ReturnType,
typename
... Args>
76
struct
function_traits_impl
<ReturnType(ClassType::*)(Args...) const volatile&&>
77
:
function_traits_defs
<ReturnType, Args...> {};
78
79
// Primary template for function_traits_no_cv. If T is not a function, the
80
// compiler will attempt to instantiate this template and fail, because its base
81
// is undefined.
82
template
<
typename
T,
typename
V =
void
>
83
struct
function_traits_no_cv
84
:
function_traits_impl
<T> {};
85
86
// Specialized template for function types
87
template
<
typename
T>
88
struct
function_traits_no_cv
<T, decltype((void)&T::operator())>
89
:
function_traits_impl
<decltype(&T::operator())> {};
90
91
// Front facing template that decays T before ripping it apart.
92
template
<
typename
T>
93
struct
function_traits
94
:
function_traits_no_cv
< decay_t<T> > {};
95
96
}
// _
97
98
99
template
<
typename
T>
100
struct
is_callable
{
101
static
constexpr
bool
value =
_::function_traits<T>::is_callable
;
102
};
103
104
template
<
typename
T>
105
struct
arity
{
106
static
constexpr
int
value =
_::function_traits<T>::arity
;
107
};
108
109
template
<
typename
T>
110
using
return_type_t =
typename
_::function_traits<T>::return_type
;
111
112
template
<
typename
T>
113
using
arg_list_t =
typename
_::function_traits<T>::args
;
114
115
// First arg
116
template
<
typename
Func,
typename
... Args>
117
struct
first_arg_impl
;
118
119
template
<
typename
Func,
typename
T,
typename
... Args>
120
struct
first_arg_impl
<Func, _::arg_list<T, Args ...> > {
121
using
type = T;
122
};
123
124
template
<
typename
Func>
125
struct
first_arg
{
126
using
type =
typename
first_arg_impl<Func, arg_list_t<Func>
>::type;
127
};
128
129
template
<
typename
Func>
130
using
first_arg_t =
typename
first_arg<Func>::type;
131
132
// Last arg
133
template
<
typename
Func,
typename
... Args>
134
struct
second_arg_impl
;
135
136
template
<
typename
Func,
typename
First,
typename
T,
typename
... Args>
137
struct
second_arg_impl
<Func, _::arg_list<First, T, Args ...> > {
138
using
type = T;
139
};
140
141
template
<
typename
Func>
142
struct
second_arg
{
143
using
type =
typename
second_arg_impl<Func, arg_list_t<Func>
>::type;
144
};
145
146
template
<
typename
Func>
147
using
second_arg_t =
typename
second_arg<Func>::type;
148
149
}
// flecs
flecs::_::arg_list
Definition
function_traits.hpp:12
flecs::_::function_traits_defs
Definition
function_traits.hpp:17
flecs::_::function_traits_impl
Definition
function_traits.hpp:26
flecs::_::function_traits_no_cv
Definition
function_traits.hpp:84
flecs::_::function_traits
Definition
function_traits.hpp:94
flecs::arity
Definition
function_traits.hpp:105
flecs::first_arg_impl
Definition
function_traits.hpp:117
flecs::first_arg
Definition
function_traits.hpp:125
flecs::is_callable
Definition
function_traits.hpp:100
flecs::second_arg_impl
Definition
function_traits.hpp:134
flecs::second_arg
Definition
function_traits.hpp:142