Embedded Template Library 1.0
Loading...
Searching...
No Matches
function_traits.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2025 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR TArgs PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_FUNCTION_TRAITS_INCLUDED
32#define ETL_FUNCTION_TRAITS_INCLUDED
33
34#include "platform.h"
35#include "type_list.h"
36#include "type_traits.h"
37
38#if ETL_USING_CPP11
39
40namespace etl
41{
42 //***************************************************************************
43 // Primary template (unspecialized)
44 //***************************************************************************
45 template <typename T, typename Enable = void>
46 struct function_traits;
47
48 //***************************************************************************
49 // Base for plain function type TReturn(TArgs...)
50 //***************************************************************************
51 template <typename TReturn, typename... TArgs>
52 struct function_traits<TReturn(TArgs...), void>
53 {
54 public:
55
56 using function_type = TReturn(TArgs...);
57 using return_type = TReturn;
58 using object_type = void;
59 using argument_types = etl::type_list<TArgs...>;
60
61 static constexpr bool is_function = true;
62 static constexpr bool is_member_function = false;
63 static constexpr bool is_functor = false;
64 static constexpr bool is_const = false;
65 static constexpr bool is_volatile = false;
66 static constexpr bool is_noexcept = false;
67 static constexpr size_t arity = sizeof...(TArgs);
68
69 ETL_DEPRECATED_REASON("Use etl::function_traits::arity instead")
70 static constexpr size_t argument_count = arity;
71 };
72
73 //***************************************************************************
74 // Free function pointer
75 //***************************************************************************
76 template <typename TReturn, typename... TArgs>
77 struct function_traits<TReturn(*)(TArgs...), void> : function_traits<TReturn(TArgs...)> {};
78
79 //***************************************************************************
80 // Free function reference
81 //***************************************************************************
82 template <typename TReturn, typename... TArgs>
83 struct function_traits<TReturn(&)(TArgs...), void> : function_traits<TReturn(TArgs...)> {};
84
85#if ETL_HAS_NOEXCEPT_FUNCTION_TYPE
86 //***************************************************************************
87 // Free noexcept function pointer
88 //***************************************************************************
89 template <typename TReturn, typename... TArgs>
90 struct function_traits<TReturn(*)(TArgs...) noexcept, void> : function_traits<TReturn(TArgs...)>
91 {
92 static constexpr bool is_noexcept = true;
93 };
94
95 //***************************************************************************
96 // Free noexcept function reference.
97 //***************************************************************************
98 template <typename TReturn, typename... TArgs>
99 struct function_traits<TReturn(&)(TArgs...) noexcept, void> : function_traits<TReturn(TArgs...)>
100 {
101 static constexpr bool is_noexcept = true;
102 };
103#endif
104
105 //***************************************************************************
106 // Member function pointers
107 //***************************************************************************
108 template <typename TReturn, typename TObject, typename... TArgs>
109 struct function_traits<TReturn (TObject::*)(TArgs...), void> : function_traits<TReturn(TArgs...)>
110 {
111 using object_type = TObject;
112 static constexpr bool is_function = false;
113 static constexpr bool is_member_function = true;
114 };
115
116 //***************************************************************************
117 // Const member function pointers
118 //***************************************************************************
119 template <typename TReturn, typename TObject, typename... TArgs>
120 struct function_traits<TReturn (TObject::*)(TArgs...) const, void> : function_traits<TReturn(TObject::*)(TArgs...)>
121 {
122 static constexpr bool is_const = true;
123 };
124
125 //***************************************************************************
126 // Volatile member function pointers
127 //***************************************************************************
128 template <typename TReturn, typename TObject, typename... TArgs>
129 struct function_traits<TReturn (TObject::*)(TArgs...) volatile, void> : function_traits<TReturn(TObject::*)(TArgs...)>
130 {
131 static constexpr bool is_volatile = true;
132 };
133
134 //***************************************************************************
135 // Const volatile member function pointers
136 //***************************************************************************
137 template <typename TReturn, typename TObject, typename... TArgs>
138 struct function_traits<TReturn (TObject::*)(TArgs...) const volatile, void> : function_traits<TReturn(TObject::*)(TArgs...)>
139 {
140 static constexpr bool is_const = true;
141 static constexpr bool is_volatile = true;
142 };
143
144#if ETL_HAS_NOEXCEPT_FUNCTION_TYPE
145 //***************************************************************************
146 // Noexcept member function pointers
147 //***************************************************************************
148 template <typename TReturn, typename TObject, typename... TArgs>
149 struct function_traits<TReturn (TObject::*)(TArgs...) noexcept, void> : function_traits<TReturn(TObject::*)(TArgs...)>
150 {
151 static constexpr bool is_noexcept = true;
152 };
153
154 //***************************************************************************
155 // Const noexcept member function pointers
156 //***************************************************************************
157 template <typename TReturn, typename TObject, typename... TArgs>
158 struct function_traits<TReturn (TObject::*)(TArgs...) const noexcept, void> : function_traits<TReturn(TObject::*)(TArgs...) const>
159 {
160 static constexpr bool is_noexcept = true;
161 };
162
163 //***************************************************************************
164 // Volatile noexcept member function pointers
165 //***************************************************************************
166 template <typename TReturn, typename TObject, typename... TArgs>
167 struct function_traits<TReturn (TObject::*)(TArgs...) volatile noexcept, void> : function_traits<TReturn(TObject::*)(TArgs...) volatile>
168 {
169 static constexpr bool is_noexcept = true;
170 };
171
172 //***************************************************************************
173 // Const volatile noexcept member function pointers
174 //***************************************************************************
175 template <typename TReturn, typename TObject, typename... TArgs>
176 struct function_traits<TReturn (TObject::*)(TArgs...) const volatile noexcept, void> : function_traits<TReturn(TObject::*)(TArgs...) const volatile>
177 {
178 static constexpr bool is_noexcept = true;
179 };
180#endif
181
182 //***************************************************************************
183 // Forward cv/ref on the whole type to the unqualified type.
184 //***************************************************************************
185 template <typename T>
186 struct function_traits<T, etl::enable_if_t<!etl::is_same<T, etl::remove_cvref_t<T>>::value &&
187 !etl::is_class<etl::decay_t<T>>::value>>
188 : function_traits<etl::remove_cvref_t<T>>
189 {
190 };
191
192 //***************************************************************************
193 // Functors / lambdas: enable only for class types that have a unique operator()
194 //***************************************************************************
195 namespace private_function_traits
196 {
197 //*********************************
198 // Helper to get pointer to call operator
199 //*********************************
200 template <typename U>
201 using call_operator_ptr_t = decltype(&U::operator());
202 }
203
204 //***************************************************************************
206 //***************************************************************************
207 template <typename T>
208 struct function_traits<T, etl::enable_if_t<etl::is_class<etl::decay_t<T>>::value&&
209 etl::has_unique_call_operator<T>::value>>
210 : function_traits<private_function_traits::call_operator_ptr_t<etl::decay_t<T>> >
211 {
212 static constexpr bool is_functor = true;
213 };
214
215 //***************************************************************************
216 // Out-of-class definitions for the function_traits static members
217 //***************************************************************************
218 // free/function primary template
219 template <typename TReturn, typename... TArgs>
220 constexpr bool function_traits<TReturn(TArgs...), void>::is_function;
221
222 template <typename TReturn, typename... TArgs>
223 constexpr bool function_traits<TReturn(TArgs...), void>::is_member_function;
224
225 template <typename TReturn, typename... TArgs>
226 constexpr bool function_traits<TReturn(TArgs...), void>::is_functor;
227
228 template <typename TReturn, typename... TArgs>
229 constexpr bool function_traits<TReturn(TArgs...), void>::is_const;
230
231 template <typename TReturn, typename... TArgs>
232 constexpr bool function_traits<TReturn(TArgs...), void>::is_volatile;
233
234 template <typename TReturn, typename... TArgs>
235 constexpr bool function_traits<TReturn(TArgs...), void>::is_noexcept;
236
237 template <typename TReturn, typename... TArgs>
238 constexpr size_t function_traits<TReturn(TArgs...), void>::arity;
239
240 // member-function-pointer specialization
241 template <typename TReturn, typename TObject, typename... TArgs>
242 constexpr bool function_traits<TReturn (TObject::*)(TArgs...), void>::is_function;
243
244 template <typename TReturn, typename TObject, typename... TArgs>
245 constexpr bool function_traits<TReturn (TObject::*)(TArgs...), void>::is_member_function;
246
247 // cv/ref-qualified member-function pointer flags
248 template <typename TReturn, typename TObject, typename... TArgs>
249 constexpr bool function_traits<TReturn (TObject::*)(TArgs...) const, void>::is_const;
250
251 template <typename TReturn, typename TObject, typename... TArgs>
252 constexpr bool function_traits<TReturn (TObject::*)(TArgs...) volatile, void>::is_volatile;
253
254 template <typename TReturn, typename TObject, typename... TArgs>
255 constexpr bool function_traits<TReturn (TObject::*)(TArgs...) const volatile, void>::is_const;
256
257 template <typename TReturn, typename TObject, typename... TArgs>
258 constexpr bool function_traits<TReturn (TObject::*)(TArgs...) const volatile, void>::is_volatile;
259
260#if ETL_HAS_NOEXCEPT_FUNCTION_TYPE
261 template <typename TReturn, typename... TArgs>
262 constexpr bool function_traits<TReturn(*)(TArgs...) noexcept, void>::is_noexcept;
263
264 template <typename TReturn, typename TObject, typename... TArgs>
265 constexpr bool function_traits<TReturn (TObject::*)(TArgs...) noexcept, void>::is_noexcept;
266
267 template <typename TReturn, typename TObject, typename... TArgs>
268 constexpr bool function_traits<TReturn (TObject::*)(TArgs...) const noexcept, void>::is_noexcept;
269
270 template <typename TReturn, typename TObject, typename... TArgs>
271 constexpr bool function_traits<TReturn (TObject::*)(TArgs...) volatile noexcept, void>::is_noexcept;
272
273 template <typename TReturn, typename TObject, typename... TArgs>
274 constexpr bool function_traits<TReturn (TObject::*)(TArgs...) const volatile noexcept, void>::is_noexcept;
275#endif
276
277 //***************************************************************************
278 // Functor / lambda specialisation: provide out-of-class definition for is_functor
279 //***************************************************************************
280 template <typename T>
281 constexpr bool function_traits<T, etl::enable_if_t<etl::is_class<etl::decay_t<T>>::value &&
282 etl::has_unique_call_operator<T>::value>>::is_functor;
283}
284
285#endif
286#endif
bitset_ext
Definition absolute.h:39