1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* Helpers to manipulate function types that don't fit in TypeTraits.h */
9 #ifndef mozilla_FunctionTypeTraits_h
10 #define mozilla_FunctionTypeTraits_h
12 #include <cstddef> /* for size_t */
17 // Main FunctionTypeTraits declaration, taking one template argument.
19 // Given a function type, FunctionTypeTraits will expose the following members:
20 // - ReturnType: Return type.
21 // - arity: Number of parameters (size_t).
22 // - ParameterType<N>: Type of the Nth** parameter, 0-indexed.
24 // ** `ParameterType<N>` with `N` >= `arity` is allowed and gives `void`.
25 // This prevents compilation errors when trying to access a type outside of the
26 // function's parameters, which is useful for parameters checks, e.g.:
27 // template<typename F>
29 // -> enable_if(FunctionTypeTraits<F>::arity == 1 &&
30 // is_same<FunctionTypeTraits<F>::template ParameterType<0>,
34 // // This function will only be enabled if `F` takes one `int`.
35 // // Without the permissive ParameterType<any N>, it wouldn't even compile.
37 // Note: FunctionTypeTraits does not work with generic lambdas `[](auto&) {}`,
38 // because parameter types cannot be known until an actual invocation when types
39 // are inferred from the given arguments.
41 struct FunctionTypeTraits
;
43 // Remove reference and pointer wrappers, if any.
45 struct FunctionTypeTraits
<T
&> : public FunctionTypeTraits
<T
> {};
47 struct FunctionTypeTraits
<T
&&> : public FunctionTypeTraits
<T
> {};
49 struct FunctionTypeTraits
<T
*> : public FunctionTypeTraits
<T
> {};
51 // Extract `operator()` function from callables (e.g. lambdas, std::function).
53 struct FunctionTypeTraits
54 : public FunctionTypeTraits
<decltype(&T::operator())> {};
58 // If `safe`, retrieve the `N`th type from `As`, otherwise `void`.
59 // See top description for reason.
60 template <bool safe
, size_t N
, typename
... As
>
61 struct TupleElementSafe
;
62 template <size_t N
, typename
... As
>
63 struct TupleElementSafe
<true, N
, As
...> {
64 using Type
= typename
std::tuple_element
<N
, std::tuple
<As
...>>::type
;
66 template <size_t N
, typename
... As
>
67 struct TupleElementSafe
<false, N
, As
...> {
71 template <typename R
, typename
... As
>
72 struct FunctionTypeTraitsHelper
{
74 static constexpr size_t arity
= sizeof...(As
);
77 typename TupleElementSafe
<(N
< sizeof...(As
)), N
, As
...>::Type
;
82 // Specialization for free functions.
83 template <typename R
, typename
... As
>
84 struct FunctionTypeTraits
<R(As
...)>
85 : detail::FunctionTypeTraitsHelper
<R
, As
...> {};
87 // Specialization for non-const member functions.
88 template <typename C
, typename R
, typename
... As
>
89 struct FunctionTypeTraits
<R (C::*)(As
...)>
90 : detail::FunctionTypeTraitsHelper
<R
, As
...> {};
92 // Specialization for const member functions.
93 template <typename C
, typename R
, typename
... As
>
94 struct FunctionTypeTraits
<R (C::*)(As
...) const>
95 : detail::FunctionTypeTraitsHelper
<R
, As
...> {};
97 #ifdef NS_HAVE_STDCALL
98 // Specialization for __stdcall free functions.
99 template <typename R
, typename
... As
>
100 struct FunctionTypeTraits
<R
NS_STDCALL(As
...)>
101 : detail::FunctionTypeTraitsHelper
<R
, As
...> {};
103 // Specialization for __stdcall non-const member functions.
104 template <typename C
, typename R
, typename
... As
>
105 struct FunctionTypeTraits
<R (NS_STDCALL
C::*)(As
...)>
106 : detail::FunctionTypeTraitsHelper
<R
, As
...> {};
108 // Specialization for __stdcall const member functions.
109 template <typename C
, typename R
, typename
... As
>
110 struct FunctionTypeTraits
<R (NS_STDCALL
C::*)(As
...) const>
111 : detail::FunctionTypeTraitsHelper
<R
, As
...> {};
112 #endif // NS_HAVE_STDCALL
114 } // namespace mozilla
116 #endif // mozilla_FunctionTypeTraits_h