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") static constexpr size_t argument_count = arity;
70 };
71
72 //***************************************************************************
73 // Free function pointer
74 //***************************************************************************
75 template <typename TReturn, typename... TArgs>
76 struct function_traits<TReturn (*)(TArgs...), void> : function_traits<TReturn(TArgs...)>
77 {
78 };
79
80 //***************************************************************************
81 // Free function reference
82 //***************************************************************************
83 template <typename TReturn, typename... TArgs>
84 struct function_traits<TReturn (&)(TArgs...), void> : function_traits<TReturn(TArgs...)>
85 {
86 };
87
88 #if ETL_HAS_NOEXCEPT_FUNCTION_TYPE
89 //***************************************************************************
90 // Free noexcept function pointer
91 //***************************************************************************
92 template <typename TReturn, typename... TArgs>
93 struct function_traits<TReturn (*)(TArgs...) noexcept, void> : function_traits<TReturn(TArgs...)>
94 {
95 static constexpr bool is_noexcept = true;
96 };
97
98 //***************************************************************************
99 // Free noexcept function reference.
100 //***************************************************************************
101 template <typename TReturn, typename... TArgs>
102 struct function_traits<TReturn (&)(TArgs...) noexcept, void> : function_traits<TReturn(TArgs...)>
103 {
104 static constexpr bool is_noexcept = true;
105 };
106 #endif
107
108 //***************************************************************************
109 // Member function pointers
110 //***************************************************************************
111 template <typename TReturn, typename TObject, typename... TArgs>
112 struct function_traits<TReturn (TObject::*)(TArgs...), void> : function_traits<TReturn(TArgs...)>
113 {
114 using object_type = TObject;
115 static constexpr bool is_function = false;
116 static constexpr bool is_member_function = true;
117 };
118
119 //***************************************************************************
120 // Const member function pointers
121 //***************************************************************************
122 template <typename TReturn, typename TObject, typename... TArgs>
123 struct function_traits<TReturn (TObject::*)(TArgs...) const, void> : function_traits<TReturn (TObject::*)(TArgs...)>
124 {
125 static constexpr bool is_const = true;
126 };
127
128 //***************************************************************************
129 // Volatile member function pointers
130 //***************************************************************************
131 template <typename TReturn, typename TObject, typename... TArgs>
132 struct function_traits<TReturn (TObject::*)(TArgs...) volatile, void> : function_traits<TReturn (TObject::*)(TArgs...)>
133 {
134 static constexpr bool is_volatile = true;
135 };
136
137 //***************************************************************************
138 // Const volatile member function pointers
139 //***************************************************************************
140 template <typename TReturn, typename TObject, typename... TArgs>
141 struct function_traits<TReturn (TObject::*)(TArgs...) const volatile, void> : function_traits<TReturn (TObject::*)(TArgs...)>
142 {
143 static constexpr bool is_const = true;
144 static constexpr bool is_volatile = true;
145 };
146
147 #if ETL_HAS_NOEXCEPT_FUNCTION_TYPE
148 //***************************************************************************
149 // Noexcept member function pointers
150 //***************************************************************************
151 template <typename TReturn, typename TObject, typename... TArgs>
152 struct function_traits<TReturn (TObject::*)(TArgs...) noexcept, void> : function_traits<TReturn (TObject::*)(TArgs...)>
153 {
154 static constexpr bool is_noexcept = true;
155 };
156
157 //***************************************************************************
158 // Const noexcept member function pointers
159 //***************************************************************************
160 template <typename TReturn, typename TObject, typename... TArgs>
161 struct function_traits<TReturn (TObject::*)(TArgs...) const noexcept, void> : function_traits<TReturn (TObject::*)(TArgs...) const>
162 {
163 static constexpr bool is_noexcept = true;
164 };
165
166 //***************************************************************************
167 // Volatile noexcept member function pointers
168 //***************************************************************************
169 template <typename TReturn, typename TObject, typename... TArgs>
170 struct function_traits<TReturn (TObject::*)(TArgs...) volatile noexcept, void> : function_traits<TReturn (TObject::*)(TArgs...) volatile>
171 {
172 static constexpr bool is_noexcept = true;
173 };
174
175 //***************************************************************************
176 // Const volatile noexcept member function pointers
177 //***************************************************************************
178 template <typename TReturn, typename TObject, typename... TArgs>
179 struct function_traits<TReturn (TObject::*)(TArgs...) const volatile noexcept, void>
180 : function_traits<TReturn (TObject::*)(TArgs...) const volatile>
181 {
182 static constexpr bool is_noexcept = true;
183 };
184 #endif
185
186 //***************************************************************************
187 // Forward cv/ref on the whole type to the unqualified type.
188 //***************************************************************************
189 template <typename T>
190 struct function_traits< T, etl::enable_if_t<!etl::is_same<T, etl::remove_cvref_t<T>>::value && !etl::is_class<etl::decay_t<T>>::value>>
191 : function_traits<etl::remove_cvref_t<T>>
192 {
193 };
194
195 //***************************************************************************
196 // Functors / lambdas: enable only for class types that have a unique
197 // operator()
198 //***************************************************************************
199 namespace private_function_traits
200 {
201 //*********************************
202 // Helper to get pointer to call operator
203 //*********************************
204 template <typename U>
205 using call_operator_ptr_t = decltype(&U::operator());
206 } // namespace private_function_traits
207
208 //***************************************************************************
210 //***************************************************************************
211 template <typename T>
212 struct function_traits< T, etl::enable_if_t<etl::is_class<etl::decay_t<T>>::value && etl::has_unique_call_operator<T>::value>>
213 : function_traits< private_function_traits::call_operator_ptr_t<etl::decay_t<T>> >
214 {
215 static constexpr bool is_functor = true;
216 };
217
218 //***************************************************************************
219 // Out-of-class definitions for the function_traits static members
220 //***************************************************************************
221 // free/function primary template
222 template <typename TReturn, typename... TArgs>
223 constexpr bool function_traits<TReturn(TArgs...), void>::is_function;
224
225 template <typename TReturn, typename... TArgs>
226 constexpr bool function_traits<TReturn(TArgs...), void>::is_member_function;
227
228 template <typename TReturn, typename... TArgs>
229 constexpr bool function_traits<TReturn(TArgs...), void>::is_functor;
230
231 template <typename TReturn, typename... TArgs>
232 constexpr bool function_traits<TReturn(TArgs...), void>::is_const;
233
234 template <typename TReturn, typename... TArgs>
235 constexpr bool function_traits<TReturn(TArgs...), void>::is_volatile;
236
237 template <typename TReturn, typename... TArgs>
238 constexpr bool function_traits<TReturn(TArgs...), void>::is_noexcept;
239
240 template <typename TReturn, typename... TArgs>
241 constexpr size_t function_traits<TReturn(TArgs...), void>::arity;
242
243 // member-function-pointer specialization
244 template <typename TReturn, typename TObject, typename... TArgs>
245 constexpr bool function_traits<TReturn (TObject::*)(TArgs...), void>::is_function;
246
247 template <typename TReturn, typename TObject, typename... TArgs>
248 constexpr bool function_traits<TReturn (TObject::*)(TArgs...), void>::is_member_function;
249
250 // cv/ref-qualified member-function pointer flags
251 template <typename TReturn, typename TObject, typename... TArgs>
252 constexpr bool function_traits<TReturn (TObject::*)(TArgs...) const, void>::is_const;
253
254 template <typename TReturn, typename TObject, typename... TArgs>
255 constexpr bool function_traits<TReturn (TObject::*)(TArgs...) volatile, void>::is_volatile;
256
257 template <typename TReturn, typename TObject, typename... TArgs>
258 constexpr bool function_traits<TReturn (TObject::*)(TArgs...) const volatile, void>::is_const;
259
260 template <typename TReturn, typename TObject, typename... TArgs>
261 constexpr bool function_traits<TReturn (TObject::*)(TArgs...) const volatile, void>::is_volatile;
262
263 #if ETL_HAS_NOEXCEPT_FUNCTION_TYPE
264 template <typename TReturn, typename... TArgs>
265 constexpr bool function_traits<TReturn (*)(TArgs...) noexcept, void>::is_noexcept;
266
267 template <typename TReturn, typename TObject, typename... TArgs>
268 constexpr bool function_traits<TReturn (TObject::*)(TArgs...) noexcept, void>::is_noexcept;
269
270 template <typename TReturn, typename TObject, typename... TArgs>
271 constexpr bool function_traits<TReturn (TObject::*)(TArgs...) const noexcept, void>::is_noexcept;
272
273 template <typename TReturn, typename TObject, typename... TArgs>
274 constexpr bool function_traits< TReturn (TObject::*)(TArgs...) volatile noexcept, void>::is_noexcept;
275
276 template <typename TReturn, typename TObject, typename... TArgs>
277 constexpr bool function_traits< TReturn (TObject::*)(TArgs...) const volatile noexcept, void>::is_noexcept;
278 #endif
279
280 //***************************************************************************
281 // Functor / lambda specialisation: provide out-of-class definition for
282 // is_functor
283 //***************************************************************************
284 template <typename T>
285 constexpr bool function_traits< T, etl::enable_if_t<etl::is_class<etl::decay_t<T>>::value && etl::has_unique_call_operator<T>::value>>::is_functor;
286} // namespace etl
287
288#endif
289#endif
bitset_ext
Definition absolute.h:40