Debian Codegraph @

LspCpp/include/LibLsp/JsonRpc/traits.h

raw
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 #pragma once
16
17 #include <tuple>
18 #include <type_traits>
19
20 namespace lsp
21 {
22 namespace traits
23 {
24
25 // NthTypeOf returns the `N`th type in `Types`
26 template<int N, typename... Types>
27 using NthTypeOf = typename std::tuple_element<N, std::tuple<Types...>>::type;
28
29 // `IsTypeOrDerived<BASE, T>::value` is true iff `T` is of type `BASE`, or
30 // derives from `BASE`.
31 template<typename BASE, typename T>
32 using IsTypeOrDerived = std::integral_constant<
33 bool, 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 {
44 using base = typename std::tuple_element<N - 1, BASES>::type;
45 using type = typename std::tuple_element<N - 1, TYPES>::type;
46 using last_matches = IsTypeOrDerived<base, type>;
47 using others_match = EachIsTypeOrDerived<N - 1, BASES, TYPES>;
48 static constexpr bool value = last_matches::value && others_match::value;
49 };
50
51 // EachIsTypeOrDerived specialization for N = 1
52 template<typename BASES, typename TYPES>
53 struct EachIsTypeOrDerived<1, BASES, TYPES>
54 {
55 using base = typename std::tuple_element<0, BASES>::type;
56 using type = typename std::tuple_element<0, TYPES>::type;
57 static constexpr bool value = IsTypeOrDerived<base, type>::value;
58 };
59
60 // EachIsTypeOrDerived specialization for N = 0
61 template<typename BASES, typename TYPES>
62 struct EachIsTypeOrDerived<0, BASES, TYPES>
63 {
64 static constexpr bool value = true;
65 };
66
67 // Signature describes the signature of a function.
68 template<typename RETURN, typename... PARAMETERS>
69 struct Signature
70 {
71 // The return type of the function signature
72 using ret = RETURN;
73 // The parameters of the function signature held in a std::tuple
74 using parameters = std::tuple<PARAMETERS...>;
75 // The type of the Nth parameter of function signature
76 template<std::size_t N>
77 using parameter = NthTypeOf<N, PARAMETERS...>;
78 // The total number of parameters
79 static constexpr std::size_t parameter_count = sizeof...(PARAMETERS);
80 };
81
82 // SignatureOf is a traits helper that infers the signature of the function,
83 // method, static method, lambda, or function-like object `F`.
84 template<typename F>
85 struct SignatureOf
86 {
87 // The signature of the function-like object `F`
88 using type = typename SignatureOf<decltype(&F::operator())>::type;
89 };
90
91 // SignatureOf specialization for a regular function or static method.
92 template<typename R, typename... ARGS>
93 struct SignatureOf<R (*)(ARGS...)>
94 {
95 // The signature of the function-like object `F`
96 using type = Signature<typename std::decay<R>::type, typename std::decay<ARGS>::type...>;
97 };
98
99 // SignatureOf specialization for a non-static method.
100 template<typename R, typename C, typename... ARGS>
101 struct SignatureOf<R (C::*)(ARGS...)>
102 {
103 // The signature of the function-like object `F`
104 using type = Signature<typename std::decay<R>::type, typename std::decay<ARGS>::type...>;
105 };
106
107 // SignatureOf specialization for a non-static, const method.
108 template<typename R, typename C, typename... ARGS>
109 struct SignatureOf<R (C::*)(ARGS...) const>
110 {
111 // The signature of the function-like object `F`
112 using type = Signature<typename std::decay<R>::type, typename std::decay<ARGS>::type...>;
113 };
114
115 // SignatureOfT is an alias to `typename SignatureOf<F>::type`.
116 template<typename F>
117 using SignatureOfT = typename SignatureOf<F>::type;
118
119 // ParameterType is an alias to `typename SignatureOf<F>::type::parameter<N>`.
120 template<typename F, std::size_t N>
121 using ParameterType = typename SignatureOfT<F>::template parameter<N>;
122
123 // `HasSignature<F, S>::value` is true iff the function-like `F` has a matching
124 // signature to the function-like `S`.
125 template<typename F, typename S>
126 using HasSignature = std::integral_constant<bool, std::is_same<SignatureOfT<F>, SignatureOfT<S>>::value>;
127
128 // `Min<A, B>::value` resolves to the smaller value of A and B.
129 template<std::size_t A, std::size_t B>
130 using Min = std::integral_constant<std::size_t, (A < B ? A : B)>;
131
132 // `CompatibleWith<F, S>::value` is true iff the function-like `F`
133 // can be called with the argument types of the function-like `S`. Return type
134 // of the two functions are not considered.
135 template<typename F, typename S>
136 using CompatibleWith = std::integral_constant<
137 bool, (SignatureOfT<S>::parameter_count == SignatureOfT<F>::parameter_count)
138 && EachIsTypeOrDerived<
139 Min<SignatureOfT<S>::parameter_count, SignatureOfT<F>::parameter_count>::value,
140 typename SignatureOfT<S>::parameters, typename SignatureOfT<F>::parameters>::value>;
141
142 // If `CONDITION` is true then EnableIf resolves to type T, otherwise an
143 // invalid type.
144 template<bool CONDITION, typename T = void>
145 using EnableIf = typename std::enable_if<CONDITION, T>::type;
146
147 // If `BASE` is a base of `T` then EnableIfIsType resolves to type `TRUE`,
148 // otherwise an invalid type.
149 template<typename BASE, typename T, typename TRUE_ = void>
150 using EnableIfIsType = EnableIf<IsTypeOrDerived<BASE, T>::value, TRUE_>;
151
152 // If the function-like `F` has a matching signature to the function-like `S`
153 // then EnableIfHasSignature resolves to type `TRUE`, otherwise an invalid type.
154 template<typename F, typename S, typename TRUE_ = void>
155 using EnableIfHasSignature = EnableIf<HasSignature<F, S>::value, TRUE_>;
156
157 } // namespace traits
158 } // namespace lsp