tuple_traits.hpp
Go to the documentation of this file.
1 // Copyright 2023-2024 Ekumen, Inc.
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 // http://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 BELUGA_TYPE_TRAITS_TUPLE_TRAITS_HPP
16 #define BELUGA_TYPE_TRAITS_TUPLE_TRAITS_HPP
17 
18 #include <tuple>
19 #include <type_traits>
20 #include <utility>
21 
27 namespace beluga {
28 
29 namespace detail {
30 
32 
33 template <typename, typename = void>
34 struct is_complete : std::false_type {};
35 
36 template <typename T>
37 struct is_complete<T, std::void_t<decltype(sizeof(T))>> : std::true_type {};
38 
40 
41 } // namespace detail
42 
44 
48 template <typename T>
49 struct is_tuple_like : detail::is_complete<std::tuple_size<std::decay_t<T>>> {};
50 
52 template <class T>
53 inline constexpr bool is_tuple_like_v = is_tuple_like<T>::value;
54 
56 
60 template <
61  typename T,
62  typename U,
63  typename Is = std::enable_if_t<
64  std::tuple_size_v<std::decay_t<T>> == std::tuple_size_v<std::decay_t<U>>,
65  std::make_index_sequence<std::tuple_size_v<std::decay_t<T>>>>>
67 
69 template <typename T, typename U, std::size_t... I>
70 struct common_tuple_type<T, U, std::index_sequence<I...>> {
72  using type = std::tuple<
73  std::common_type_t<std::tuple_element_t<I, std::decay_t<T>>, std::tuple_element_t<I, std::decay_t<U>>>...>;
74 };
75 
77 template <typename T, typename U>
79 
81 template <typename, typename, typename = void>
82 struct has_common_tuple_type : std::false_type {};
83 
85 template <typename T, typename U>
86 struct has_common_tuple_type<T, U, std::void_t<common_tuple_type_t<T, U>>> : std::true_type {};
87 
89 template <typename T, typename U>
91 
92 namespace detail {
93 
95 static constexpr std::size_t kTupleIndexNotFound = static_cast<std::size_t>(-1);
96 
98 static constexpr std::size_t kTupleIndexAmbiguous = static_cast<std::size_t>(-2);
99 
101 
105 template <class T, class... Args>
106 constexpr std::size_t tuple_index_helper() noexcept {
107  constexpr bool kMatches[sizeof...(Args)] = // NOLINT(modernize-avoid-c-arrays)
108  {std::is_same_v<T, std::decay_t<Args>>...};
109  std::size_t selected = kTupleIndexNotFound;
110  for (std::size_t i = 0; i < sizeof...(Args); ++i) {
111  if (kMatches[i]) {
112  if (selected == kTupleIndexNotFound) {
113  selected = i;
114  } else {
115  return kTupleIndexAmbiguous;
116  }
117  }
118  }
119  return selected;
120 }
121 
123 template <class T, class... Args>
124 constexpr bool tuple_index_found() noexcept {
125  constexpr std::size_t kIndex = tuple_index_helper<T, Args...>();
126  return kIndex != kTupleIndexNotFound && kIndex != kTupleIndexAmbiguous;
127 }
128 
129 } // namespace detail
130 
132 template <class T, class TupleLike, class = void>
133 struct tuple_index;
134 
136 template <class T, template <class...> class TupleLike, class... Args>
137 struct tuple_index<
138  T,
139  TupleLike<Args...>,
140  std::enable_if_t<is_tuple_like_v<std::decay_t<TupleLike<Args...>>> && detail::tuple_index_found<T, Args...>()>>
141  : std::integral_constant<std::size_t, detail::tuple_index_helper<T, Args...>()> {};
142 
144 template <class T, class TupleLike>
145 inline constexpr std::size_t tuple_index_v = tuple_index<T, TupleLike>::value;
146 
148 template <class T, class TupleLike>
150 
152 template <class T, class TupleLike, class = void>
153 struct has_single_element : std::false_type {};
154 
156 template <class T, template <class...> class TupleLike, class... Args>
158  T,
159  TupleLike<Args...>,
160  std::enable_if_t<is_tuple_like_v<std::decay_t<TupleLike<Args...>>> && detail::tuple_index_found<T, Args...>()>>
161  : std::true_type {};
162 
164 template <class T, class TupleLike>
166 
168 template <class T, class TupleLike>
169 constexpr decltype(auto) element(TupleLike&& tuple) noexcept {
170  constexpr std::size_t kIndex = tuple_index_v<T, std::decay_t<TupleLike>>;
171  return std::get<kIndex>(std::forward<TupleLike>(tuple));
172 }
173 
175 template <class T, class = void>
177 
179 template <template <class...> class TupleLike, class... Args>
180 struct decay_tuple_like<TupleLike<Args...>, std::enable_if_t<is_tuple_like_v<std::decay_t<TupleLike<Args...>>>>> {
182  using type = std::decay_t<TupleLike<std::decay_t<Args>...>>;
183 };
184 
186 template <class T>
188 
189 } // namespace beluga
190 
191 #endif
beluga::tuple_index
Meta-function that returns the tuple index of the element whose type is T.
Definition: tuple_traits.hpp:133
beluga::decay_tuple_like< TupleLike< Args... >, std::enable_if_t< is_tuple_like_v< std::decay_t< TupleLike< Args... > > > > >::type
std::decay_t< TupleLike< std::decay_t< Args >... > > type
Return type.
Definition: tuple_traits.hpp:182
beluga::has_single_element_v
constexpr bool has_single_element_v
Convenience template variable for has_single_element.
Definition: tuple_traits.hpp:165
beluga::element
constexpr decltype(auto) element(TupleLike &&tuple) noexcept
Returns element of a tuple like object whose type is T (or a possibly const reference to T).
Definition: tuple_traits.hpp:169
beluga::tuple_index_t
typename tuple_index< T, TupleLike >::type tuple_index_t
Convenience template type alias for tuple_index.
Definition: tuple_traits.hpp:149
enable_if_t
typename std::enable_if< B, T >::type enable_if_t
beluga::decay_tuple_like
Meta-function that decays a tuple like type and its members.
Definition: tuple_traits.hpp:176
beluga::common_tuple_type
Meta-function that computes a common tuple type given two tuple-like types T and U.
Definition: tuple_traits.hpp:66
beluga::is_tuple_like_v
constexpr bool is_tuple_like_v
Convenience template variable for is_tuple_like.
Definition: tuple_traits.hpp:53
beluga::is_tuple_like
Meta-function that returns true if T is a tuple-like type.
Definition: tuple_traits.hpp:49
beluga::detail::tuple_index_found
constexpr bool tuple_index_found() noexcept
Help method that returns true if a tuple element index that matches an input type is found.
Definition: tuple_traits.hpp:124
beluga::detail::kTupleIndexAmbiguous
static constexpr std::size_t kTupleIndexAmbiguous
Constant value to return when there are multiple indices that match the input type.
Definition: tuple_traits.hpp:98
beluga::common_tuple_type_t
typename common_tuple_type< T, U >::type common_tuple_type_t
Convenience template type alias for common_tuple_type.
Definition: tuple_traits.hpp:78
beluga::tuple_index_v
constexpr std::size_t tuple_index_v
Convenience template variable for tuple_index.
Definition: tuple_traits.hpp:145
beluga::has_single_element
Meta-function that returns true if there is a single element of type T in the tuple-like type.
Definition: tuple_traits.hpp:153
beluga::decay_tuple_like_t
typename decay_tuple_like< T >::type decay_tuple_like_t
Convenience template type alias for decay_tuple_like.
Definition: tuple_traits.hpp:187
std
Definition: circular_array.hpp:529
beluga::has_common_tuple_type_v
constexpr bool has_common_tuple_type_v
Convenience template variable for has_common_tuple_type.
Definition: tuple_traits.hpp:90
beluga::detail::tuple_index_helper
constexpr std::size_t tuple_index_helper() noexcept
Help method that finds a tuple element index that matches an input type.
Definition: tuple_traits.hpp:106
beluga::detail::kTupleIndexNotFound
static constexpr std::size_t kTupleIndexNotFound
Constant value to return when the index was not found.
Definition: tuple_traits.hpp:95
beluga::has_common_tuple_type
Meta-function that checks for the existence of a common tuple type.
Definition: tuple_traits.hpp:82
beluga::common_tuple_type< T, U, std::index_sequence< I... > >::type
std::tuple< std::common_type_t< std::tuple_element_t< I, std::decay_t< T > >, std::tuple_element_t< I, std::decay_t< U > >>... > type
Common tuple type based on an std::tuple.
Definition: tuple_traits.hpp:73
beluga
The main Beluga namespace.
Definition: 3d_embedding.hpp:21


beluga
Author(s):
autogenerated on Tue Jul 16 2024 02:59:53