Bug 1601859 - Vendor cubeb-pulse-rs. r=kinetik
[gecko.git] / mfbt / FunctionTypeTraits.h
blob3a908bbec72f995fcfdc6c274e08c18c0d5ed947
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 <tuple>
14 namespace mozilla {
16 // Main FunctionTypeTraits declaration, taking one template argument.
18 // Given a function type, FunctionTypeTraits will expose the following members:
19 // - ReturnType: Return type.
20 // - arity: Number of parameters (size_t).
21 // - ParameterType<N>: Type of the Nth** parameter, 0-indexed.
23 // ** `ParameterType<N>` with `N` >= `arity` is allowed and gives `void`.
24 // This prevents compilation errors when trying to access a type outside of the
25 // function's parameters, which is useful for parameters checks, e.g.:
26 // template<typename F>
27 // auto foo(F&&)
28 // -> enable_if(FunctionTypeTraits<F>::arity == 1 &&
29 // is_same<FunctionTypeTraits<F>::template ParameterType<0>,
30 // int>::value,
31 // void)
32 // {
33 // // This function will only be enabled if `F` takes one `int`.
34 // // Without the permissive ParameterType<any N>, it wouldn't even compile.
36 // Note: FunctionTypeTraits does not work with generic lambdas `[](auto&) {}`,
37 // because parameter types cannot be known until an actual invocation when types
38 // are inferred from the given arguments.
39 template <typename T>
40 struct FunctionTypeTraits;
42 // Remove reference and pointer wrappers, if any.
43 template <typename T>
44 struct FunctionTypeTraits<T&> : public FunctionTypeTraits<T> {};
45 template <typename T>
46 struct FunctionTypeTraits<T&&> : public FunctionTypeTraits<T> {};
47 template <typename T>
48 struct FunctionTypeTraits<T*> : public FunctionTypeTraits<T> {};
50 // Extract `operator()` function from callables (e.g. lambdas, std::function).
51 template <typename T>
52 struct FunctionTypeTraits
53 : public FunctionTypeTraits<decltype(&T::operator())> {};
55 namespace detail {
57 // If `safe`, retrieve the `N`th type from `As`, otherwise `void`.
58 // See top description for reason.
59 template <bool safe, size_t N, typename... As>
60 struct TupleElementSafe;
61 template <size_t N, typename... As>
62 struct TupleElementSafe<true, N, As...> {
63 using Type = typename std::tuple_element<N, std::tuple<As...>>::type;
65 template <size_t N, typename... As>
66 struct TupleElementSafe<false, N, As...> {
67 using Type = void;
70 template <typename R, typename... As>
71 struct FunctionTypeTraitsHelper {
72 using ReturnType = R;
73 static constexpr size_t arity = sizeof...(As);
74 template <size_t N>
75 using ParameterType =
76 typename TupleElementSafe<(N < sizeof...(As)), N, As...>::Type;
79 } // namespace detail
81 // Specialization for free functions.
82 template <typename R, typename... As>
83 struct FunctionTypeTraits<R(As...)>
84 : detail::FunctionTypeTraitsHelper<R, As...> {};
86 // Specialization for non-const member functions.
87 template <typename C, typename R, typename... As>
88 struct FunctionTypeTraits<R (C::*)(As...)>
89 : detail::FunctionTypeTraitsHelper<R, As...> {};
91 // Specialization for const member functions.
92 template <typename C, typename R, typename... As>
93 struct FunctionTypeTraits<R (C::*)(As...) const>
94 : detail::FunctionTypeTraitsHelper<R, As...> {};
96 #ifdef NS_HAVE_STDCALL
97 // Specialization for __stdcall free functions.
98 template <typename R, typename... As>
99 struct FunctionTypeTraits<R NS_STDCALL(As...)>
100 : detail::FunctionTypeTraitsHelper<R, As...> {};
102 // Specialization for __stdcall non-const member functions.
103 template <typename C, typename R, typename... As>
104 struct FunctionTypeTraits<R (NS_STDCALL C::*)(As...)>
105 : detail::FunctionTypeTraitsHelper<R, As...> {};
107 // Specialization for __stdcall const member functions.
108 template <typename C, typename R, typename... As>
109 struct FunctionTypeTraits<R (NS_STDCALL C::*)(As...) const>
110 : detail::FunctionTypeTraitsHelper<R, As...> {};
111 #endif // NS_HAVE_STDCALL
113 } // namespace mozilla
115 #endif // mozilla_FunctionTypeTraits_h