traits.h 6.3 KB

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