Bug 1788332 [wpt PR 35694] - Only fire togglechange events when value changes., a...
[gecko.git] / mfbt / FunctionTypeTraits.h
blobc41829a30f9d139b6d8db69dcffeac1ec32bf9b1
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 */
13 #include <tuple>
15 namespace mozilla {
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>
28 // auto foo(F&&)
29 // -> enable_if(FunctionTypeTraits<F>::arity == 1 &&
30 // is_same<FunctionTypeTraits<F>::template ParameterType<0>,
31 // int>::value,
32 // void)
33 // {
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.
40 template <typename T>
41 struct FunctionTypeTraits;
43 // Remove reference and pointer wrappers, if any.
44 template <typename T>
45 struct FunctionTypeTraits<T&> : public FunctionTypeTraits<T> {};
46 template <typename T>
47 struct FunctionTypeTraits<T&&> : public FunctionTypeTraits<T> {};
48 template <typename T>
49 struct FunctionTypeTraits<T*> : public FunctionTypeTraits<T> {};
51 // Extract `operator()` function from callables (e.g. lambdas, std::function).
52 template <typename T>
53 struct FunctionTypeTraits
54 : public FunctionTypeTraits<decltype(&T::operator())> {};
56 namespace detail {
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...> {
68 using Type = void;
71 template <typename R, typename... As>
72 struct FunctionTypeTraitsHelper {
73 using ReturnType = R;
74 static constexpr size_t arity = sizeof...(As);
75 template <size_t N>
76 using ParameterType =
77 typename TupleElementSafe<(N < sizeof...(As)), N, As...>::Type;
80 } // namespace detail
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