Flecs: include/flecs/addons/cpp/utils/function_traits.hpp Source File
Description: A fast entity component system (ECS) for C & C++

| Flecs v4.1 A fast entity component system (ECS) for C & C++

Loading...
Searching...
No Matches
function_traits.hpp
Go to the documentation of this file.
1
7
8#pragma once
9
10namespace flecs {
11namespace _ {
12
14template <typename ... Args>
15struct arg_list { };
16
18template <typename ReturnType, typename... Args>
19struct function_traits_defs
20{
21 static constexpr bool is_callable = true;
22 static constexpr size_t arity = sizeof...(Args);
23 using return_type = ReturnType;
24 using args = arg_list<Args ...>;
25};
26
28template <typename T>
29struct function_traits_impl {
30 static constexpr bool is_callable = false;
31};
32
34template <typename ReturnType, typename... Args>
35struct function_traits_impl<ReturnType(Args...)>
36 : function_traits_defs<ReturnType, Args...> {};
37
39template <typename ReturnType, typename... Args>
40struct function_traits_impl<ReturnType(*)(Args...)>
41 : function_traits_defs<ReturnType, Args...> {};
42
44template <typename ClassType, typename ReturnType, typename... Args>
45struct function_traits_impl<ReturnType(ClassType::*)(Args...)>
46 : function_traits_defs<ReturnType, Args...> {};
47
49template <typename ClassType, typename ReturnType, typename... Args>
50struct function_traits_impl<ReturnType(ClassType::*)(Args...) const>
51 : function_traits_defs<ReturnType, Args...> {};
52
54template <typename ClassType, typename ReturnType, typename... Args>
55struct function_traits_impl<ReturnType(ClassType::*)(Args...) const&>
56 : function_traits_defs<ReturnType, Args...> {};
57
59template <typename ClassType, typename ReturnType, typename... Args>
60struct function_traits_impl<ReturnType(ClassType::*)(Args...) const&&>
61 : function_traits_defs<ReturnType, Args...> {};
62
64template <typename ClassType, typename ReturnType, typename... Args>
65struct function_traits_impl<ReturnType(ClassType::*)(Args...) volatile>
66 : function_traits_defs<ReturnType, Args...> {};
67
69template <typename ClassType, typename ReturnType, typename... Args>
70struct function_traits_impl<ReturnType(ClassType::*)(Args...) volatile&>
71 : function_traits_defs<ReturnType, Args...> {};
72
74template <typename ClassType, typename ReturnType, typename... Args>
75struct function_traits_impl<ReturnType(ClassType::*)(Args...) volatile&&>
76 : function_traits_defs<ReturnType, Args...> {};
77
79template <typename ClassType, typename ReturnType, typename... Args>
80struct function_traits_impl<ReturnType(ClassType::*)(Args...) const volatile>
81 : function_traits_defs<ReturnType, Args...> {};
82
84template <typename ClassType, typename ReturnType, typename... Args>
85struct function_traits_impl<ReturnType(ClassType::*)(Args...) const volatile&>
86 : function_traits_defs<ReturnType, Args...> {};
87
89template <typename ClassType, typename ReturnType, typename... Args>
90struct function_traits_impl<ReturnType(ClassType::*)(Args...) const volatile&&>
91 : function_traits_defs<ReturnType, Args...> {};
92
94template <typename T, typename V = void>
95struct function_traits_no_cv
96 : function_traits_impl<T> {};
97
99template <typename T>
100struct function_traits_no_cv<T, decltype((void)&T::operator())>
101 : function_traits_impl<decltype(&T::operator())> {};
102
104template <typename T>
105struct function_traits
106 : function_traits_no_cv< decay_t<T> > {};
107
108} // namespace _
109
110
112template <typename T>
113 struct is_callable {
114 static constexpr bool value = _::function_traits<T>::is_callable;
115};
116
118template <typename T>
119 struct arity {
120 static constexpr int value = _::function_traits<T>::arity;
121};
122
124template <typename T>
125 using return_type_t = typename _::function_traits<T>::return_type;
126
128template <typename T>
129 using arg_list_t = typename _::function_traits<T>::args;
130
132template<typename Func, typename ... Args>
133 struct first_arg_impl;
134
136template<typename Func, typename T, typename ... Args>
137 struct first_arg_impl<Func, _::arg_list<T, Args ...> > {
138 using type = T;
139};
140
142template<typename Func>
143 struct first_arg {
144 using type = typename first_arg_impl<Func, arg_list_t<Func>>::type;
145};
146
148template <typename Func>
149 using first_arg_t = typename first_arg<Func>::type;
150
152template<typename Func, typename ... Args>
153 struct second_arg_impl;
154
156template<typename Func, typename First, typename T, typename ... Args>
157 struct second_arg_impl<Func, _::arg_list<First, T, Args ...> > {
158 using type = T;
159};
160
162template<typename Func>
163 struct second_arg {
164 using type = typename second_arg_impl<Func, arg_list_t<Func>>::type;
165};
166
168template <typename Func>
169 using second_arg_t = typename second_arg<Func>::type;
170
171} // namespace flecs
flecs::arg_list_t
typename _::function_traits< T >::args arg_list_t
Get the argument list type of a callable.
Definition function_traits.hpp:129
flecs::return_type_t
typename _::function_traits< T >::return_type return_type_t
Get the return type of a callable.
Definition function_traits.hpp:125
flecs::first_arg_t
typename first_arg< Func >::type first_arg_t
Convenience alias for the first argument type of a callable.
Definition function_traits.hpp:149
flecs::second_arg_t
typename second_arg< Func >::type second_arg_t
Convenience alias for the second argument type of a callable.
Definition function_traits.hpp:169
flecs::_
Int to enum.
Definition component.hpp:18
flecs::arity
Trait to get the number of arguments of a callable.
Definition function_traits.hpp:119
flecs::first_arg_impl
Extract the first argument type from a callable (implementation).
Definition function_traits.hpp:133
flecs::first_arg
Get the first argument type of a callable.
Definition function_traits.hpp:143
flecs::is_callable
Trait to check if a type is callable.
Definition function_traits.hpp:113
flecs::second_arg_impl
Extract the second argument type from a callable (implementation).
Definition function_traits.hpp:153
flecs::second_arg
Get the second argument type of a callable.
Definition function_traits.hpp:163