1 | // Copyright 2021 Google LLC |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | #ifndef dap_traits_h |
16 | #define dap_traits_h |
17 | |
18 | #include <tuple> |
19 | #include <type_traits> |
20 | |
21 | namespace dap { |
22 | namespace traits { |
23 | |
24 | // NthTypeOf returns the `N`th type in `Types` |
25 | template <int N, typename... Types> |
26 | using NthTypeOf = typename std::tuple_element<N, std::tuple<Types...>>::type; |
27 | |
28 | // `IsTypeOrDerived<BASE, T>::value` is true iff `T` is of type `BASE`, or |
29 | // derives from `BASE`. |
30 | template <typename BASE, typename T> |
31 | using IsTypeOrDerived = std::integral_constant< |
32 | bool, |
33 | std::is_base_of<BASE, typename std::decay<T>::type>::value || |
34 | std::is_same<BASE, typename std::decay<T>::type>::value>; |
35 | |
36 | // `EachIsTypeOrDerived<N, BASES, TYPES>::value` is true iff all of the types in |
37 | // the std::tuple `TYPES` is of, or derives from the corresponding indexed type |
38 | // in the std::tuple `BASES`. |
39 | // `N` must be equal to the number of types in both the std::tuple `BASES` and |
40 | // `TYPES`. |
41 | template <int N, typename BASES, typename TYPES> |
42 | struct EachIsTypeOrDerived { |
43 | using base = typename std::tuple_element<N - 1, BASES>::type; |
44 | using type = typename std::tuple_element<N - 1, TYPES>::type; |
45 | using last_matches = IsTypeOrDerived<base, type>; |
46 | using others_match = EachIsTypeOrDerived<N - 1, BASES, TYPES>; |
47 | static constexpr bool value = last_matches::value && others_match::value; |
48 | }; |
49 | |
50 | // EachIsTypeOrDerived specialization for N = 1 |
51 | template <typename BASES, typename TYPES> |
52 | struct EachIsTypeOrDerived<1, BASES, TYPES> { |
53 | using base = typename std::tuple_element<0, BASES>::type; |
54 | using type = typename std::tuple_element<0, TYPES>::type; |
55 | static constexpr bool value = IsTypeOrDerived<base, type>::value; |
56 | }; |
57 | |
58 | // EachIsTypeOrDerived specialization for N = 0 |
59 | template <typename BASES, typename TYPES> |
60 | struct EachIsTypeOrDerived<0, BASES, TYPES> { |
61 | static constexpr bool value = true; |
62 | }; |
63 | |
64 | // Signature describes the signature of a function. |
65 | template <typename RETURN, typename... PARAMETERS> |
66 | struct Signature { |
67 | // The return type of the function signature |
68 | using ret = RETURN; |
69 | // The parameters of the function signature held in a std::tuple |
70 | using parameters = std::tuple<PARAMETERS...>; |
71 | // The type of the Nth parameter of function signature |
72 | template <std::size_t N> |
73 | using parameter = NthTypeOf<N, PARAMETERS...>; |
74 | // The total number of parameters |
75 | static constexpr std::size_t parameter_count = sizeof...(PARAMETERS); |
76 | }; |
77 | |
78 | // SignatureOf is a traits helper that infers the signature of the function, |
79 | // method, static method, lambda, or function-like object `F`. |
80 | template <typename F> |
81 | struct SignatureOf { |
82 | // The signature of the function-like object `F` |
83 | using type = typename SignatureOf<decltype(&F::operator())>::type; |
84 | }; |
85 | |
86 | // SignatureOf specialization for a regular function or static method. |
87 | template <typename R, typename... ARGS> |
88 | struct SignatureOf<R (*)(ARGS...)> { |
89 | // The signature of the function-like object `F` |
90 | using type = Signature<typename std::decay<R>::type, |
91 | typename std::decay<ARGS>::type...>; |
92 | }; |
93 | |
94 | // SignatureOf specialization for a non-static method. |
95 | template <typename R, typename C, typename... ARGS> |
96 | struct SignatureOf<R (C::*)(ARGS...)> { |
97 | // The signature of the function-like object `F` |
98 | using type = Signature<typename std::decay<R>::type, |
99 | typename std::decay<ARGS>::type...>; |
100 | }; |
101 | |
102 | // SignatureOf specialization for a non-static, const method. |
103 | template <typename R, typename C, typename... ARGS> |
104 | struct SignatureOf<R (C::*)(ARGS...) const> { |
105 | // The signature of the function-like object `F` |
106 | using type = Signature<typename std::decay<R>::type, |
107 | typename std::decay<ARGS>::type...>; |
108 | }; |
109 | |
110 | // SignatureOfT is an alias to `typename SignatureOf<F>::type`. |
111 | template <typename F> |
112 | using SignatureOfT = typename SignatureOf<F>::type; |
113 | |
114 | // ParameterType is an alias to `typename SignatureOf<F>::type::parameter<N>`. |
115 | template <typename F, std::size_t N> |
116 | using ParameterType = typename SignatureOfT<F>::template parameter<N>; |
117 | |
118 | // `HasSignature<F, S>::value` is true iff the function-like `F` has a matching |
119 | // signature to the function-like `S`. |
120 | template <typename F, typename S> |
121 | using HasSignature = std::integral_constant< |
122 | bool, |
123 | std::is_same<SignatureOfT<F>, SignatureOfT<S>>::value>; |
124 | |
125 | // `Min<A, B>::value` resolves to the smaller value of A and B. |
126 | template <std::size_t A, std::size_t B> |
127 | using Min = std::integral_constant<std::size_t, (A < B ? A : B)>; |
128 | |
129 | // `CompatibleWith<F, S>::value` is true iff the function-like `F` |
130 | // can be called with the argument types of the function-like `S`. Return type |
131 | // of the two functions are not considered. |
132 | template <typename F, typename S> |
133 | using CompatibleWith = std::integral_constant< |
134 | bool, |
135 | (SignatureOfT<S>::parameter_count == SignatureOfT<F>::parameter_count) && |
136 | EachIsTypeOrDerived<Min<SignatureOfT<S>::parameter_count, |
137 | SignatureOfT<F>::parameter_count>::value, |
138 | typename SignatureOfT<S>::parameters, |
139 | typename SignatureOfT<F>::parameters>::value>; |
140 | |
141 | // If `CONDITION` is true then EnableIf resolves to type T, otherwise an |
142 | // invalid type. |
143 | template <bool CONDITION, typename T = void> |
144 | using EnableIf = typename std::enable_if<CONDITION, T>::type; |
145 | |
146 | // If `BASE` is a base of `T` then EnableIfIsType resolves to type `TRUE_TY`, |
147 | // otherwise an invalid type. |
148 | template <typename BASE, typename T, typename TRUE_TY = void> |
149 | using EnableIfIsType = EnableIf<IsTypeOrDerived<BASE, T>::value, TRUE_TY>; |
150 | |
151 | // If the function-like `F` has a matching signature to the function-like `S` |
152 | // then EnableIfHasSignature resolves to type `TRUE_TY`, otherwise an invalid type. |
153 | template <typename F, typename S, typename TRUE_TY = void> |
154 | using EnableIfHasSignature = EnableIf<HasSignature<F, S>::value, TRUE_TY>; |
155 | |
156 | } // namespace traits |
157 | } // namespace dap |
158 | |
159 | #endif // dap_traits_h |
160 | |