MySQL 9.6.0
Source Code Documentation
tuple_find.h
Go to the documentation of this file.
1// Copyright (c) 2025, Oracle and/or its affiliates.
2//
3// This program is free software; you can redistribute it and/or modify
4// it under the terms of the GNU General Public License, version 2.0,
5// as published by the Free Software Foundation.
6//
7// This program is designed to work with certain software (including
8// but not limited to OpenSSL) that is licensed under separate terms,
9// as designated in a particular file or component or in included license
10// documentation. The authors of MySQL hereby grant you an additional
11// permission to link the program and your derivative works with the
12// separately licensed software that they have either included with
13// the program or referenced in the documentation.
14//
15// This program is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License, version 2.0, for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with this program; if not, write to the Free Software
22// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
24#ifndef MYSQL_UTILS_TUPLE_FIND_H
25#define MYSQL_UTILS_TUPLE_FIND_H
26
27/// @file
28/// Experimental API header
29
30#include <tuple> // tuple
31
32/// @addtogroup GroupLibsMysqlUtils
33/// @{
34
35namespace mysql::utils {
36
37/// Primary template for helper struct used to define Tuple_find_index.
38///
39/// This has the member constant `value` set to the smallest number N >= index
40/// such that the type of the Nth component satisfies the predicate, or no such
41/// member constant if there is no such component.
42template <class Tuple, template <class> class Pred, std::size_t index = 0>
44
45/// Specialization of Tuple_find_helper to the case where the component at
46/// position `index` satisfies the predicate.
47template <class Tuple, template <class> class Pred, std::size_t index>
48 requires(index < std::tuple_size_v<Tuple> &&
49 Pred<std::tuple_element_t<index, Tuple>>::value)
51 : public std::integral_constant<std::size_t, index> {};
52
53/// Specialization of Tuple_find_helper to the case where the component at
54/// position `index` does not satisfy the predicate.
55template <class Tuple, template <class> class Pred, std::size_t index>
56 requires(index < std::tuple_size_v<Tuple> &&
57 !Pred<std::tuple_element_t<index, Tuple>>::value)
60
61/// Primary template for helper struct used to define Tuple_find_index.
62///
63/// This has the member constant `value` set to the number of positions N >=
64/// index such that the type of the Nth component satisfies the predicate.
65template <class Tuple, template <class> class Pred, std::size_t index = 0>
66struct Tuple_count_helper : public std::integral_constant<std::size_t, 0> {};
67
68/// Specialization of Tuple_count_helper to the case where index is in range.
69template <class Tuple, template <class> class Pred, std::size_t index>
70 requires(index < std::tuple_size_v<Tuple>)
72 : public std::integral_constant<
73 std::size_t,
74 // Recursively compute the value for index + 1.
76 // Increment by 1 if the predicate matches.
77 (Pred<std::tuple_element_t<index, Tuple>>::value ? 1 : 0)> {};
78
79namespace detail {
80/// Struct template with with one template argument, having a member type
81/// Predicate, which is a type predicate that holds for types that are equal to
82/// the template argument of the (outer) struct.
83template <class Type1>
85 template <class Type2>
86 using Predicate = std::is_same<Type1, Type2>;
87};
88} // namespace detail
89
90/// Index of the first element of the tuple-like type whose type matches the
91/// type predicate.
92///
93/// This is undefined if no element type matches the predicate.
94///
95/// @tparam Tuple Any tuple-like object
96/// (https://en.cppreference.com/w/cpp/utility/tuple/tuple-like).
97///
98/// @tparam Pred Type predicate, i.e., a template class such that
99/// `Pred<T>::value` is a truth value. This may, for example, be a
100/// UnaryTypeTrait
101/// (https://en.cppreference.com/w/cpp/named_req/UnaryTypeTrait.html)
102///
103/// @code
104/// // The first element type that satisfies is_integral is `int` at index 1.
105/// static_assert(
106/// Tuple_find_index<std::is_integral, std::tuple<float, int>> == 1);
107/// @endcode
108template <class Tuple, template <class> class Pred>
109constexpr std::size_t Tuple_find_index =
111
112/// The first element type in the tuple-like type that matches the type
113/// predicate.
114///
115/// This is undefined if no element type matches the predicate.
116///
117/// @tparam Tuple Any tuple-like object
118/// (https://en.cppreference.com/w/cpp/utility/tuple/tuple-like).
119///
120/// @tparam Pred Type predicate, i.e., a template class such that
121/// `Pred<T>::value` is a truth value. This may, for example, be a
122/// UnaryTypeTrait
123/// (https://en.cppreference.com/w/cpp/named_req/UnaryTypeTrait.html)
124///
125/// @code
126/// // The first element type that satisfies is_integral is `short`.
127/// static_assert(std::same_as<
128/// Tuple_find<std::is_integral, std::tuple<std::string, float, short, int>>,
129/// short>);
130/// @endcode
131template <class Tuple, template <class> class Pred>
133
134/// True if at least one element type in the tuple-like type mathes the type
135/// predicate.
136///
137/// tparam Tuple Any tuple-like object
138/// (https://en.cppreference.com/w/cpp/utility/tuple/tuple-like).
139///
140/// tparam Pred Type predicate, i.e., a template class such that
141/// `Pred<T>::value` is a truth value. This may, for example, be a
142/// UnaryTypeTrait
143/// (https://en.cppreference.com/w/cpp/named_req/UnaryTypeTrait.html)
144///
145/// @code
146/// // `int` matches is_integral.
147/// static_assert(
148/// Tuple_has_matching_element_type<std::is_integral,
149/// std::tuple<std::string, float, int>>);
150/// // neither `string` nor `float` matches is_integral.
151/// static_assert(
152/// !Tuple_has_matching_element_type<std::is_integral,
153/// std::tuple<std::string, float>>);
154/// @endcode
155template <class Tuple, template <class> class Pred>
157 requires { typename Tuple_find_helper<Tuple, Pred, 0>::value; };
158
159/// Return the value of the first component of the tuple-like object whose type
160/// matches the given type predicate.
161///
162/// @tparam Tuple Any tuple-like object
163/// (https://en.cppreference.com/w/cpp/utility/tuple/tuple-like).
164///
165/// @tparam Pred Type predicate, i.e., a template class such that
166/// `Pred<T>::value` is a truth value. This may, for example, be a
167/// UnaryTypeTrait
168/// (https://en.cppreference.com/w/cpp/named_req/UnaryTypeTrait.html)
169///
170/// @code
171/// // The first element whose type satisfies is_integral is the 9 (an int).
172/// assert(tuple_find(std::is_integral, std::tuple("x", 2.1, 9)) == 9);
173/// @endcode
174template <template <class> class Pred, class Tuple>
175auto tuple_find(const Tuple &tuple) {
176 return std::get<Tuple_find_index<Tuple, Pred>>(tuple);
177}
178
179/// True if the tuple has an element of the given type.
180///
181/// tparam Tuple Any tuple-like object
182/// (https://en.cppreference.com/w/cpp/utility/tuple/tuple-like).
183///
184/// tparam Type The type to look for.
185template <class Tuple, class Type>
187 Tuple, detail::Is_same_helper<Type>::template Predicate>;
188
189/// The number of tuple element types that match the given predicate.
190///
191/// @tparam Tuple Any tuple-like object
192/// (https://en.cppreference.com/w/cpp/utility/tuple/tuple-like).
193///
194/// @tparam Pred Type predicate, i.e., a template class such that
195/// `Pred<T>::value` is a truth value. This may, for example, be a
196/// UnaryTypeTrait
197/// (https://en.cppreference.com/w/cpp/named_req/UnaryTypeTrait.html)
198template <class Tuple, template <class> class Pred>
201
202} // namespace mysql::utils
203
204// addtogroup GroupLibsMysqlUtils
205/// @}
206
207#endif // ifndef MYSQL_UTILS_TUPLE_FIND_H
True if the tuple has an element of the given type.
Definition: tuple_find.h:186
True if at least one element type in the tuple-like type mathes the type predicate.
Definition: tuple_find.h:156
Definition: fts0fts.cc:236
bool index(const std::string &value, const String &search_for, uint32_t *idx)
Definition: contains.h:76
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
Definition: gtid_format.h:47
Tuple_find_helper< Tuple, Pred >::type Tuple_find
The first element type in the tuple-like type that matches the type predicate.
Definition: tuple_find.h:132
constexpr std::size_t Tuple_matching_element_type_count
The number of tuple element types that match the given predicate.
Definition: tuple_find.h:199
constexpr std::size_t Tuple_find_index
Index of the first element of the tuple-like type whose type matches the type predicate.
Definition: tuple_find.h:109
auto tuple_find(const Tuple &tuple)
Return the value of the first component of the tuple-like object whose type matches the given type pr...
Definition: tuple_find.h:175
A filter of some sort that is not a join condition (those are stored in JoinPredicate objects).
Definition: access_path.h:133
Specialization of Tuple_count_helper to the case where index is in range.
Definition: tuple_find.h:77
Primary template for helper struct used to define Tuple_find_index.
Definition: tuple_find.h:66
Specialization of Tuple_find_helper to the case where the component at position index satisfies the p...
Definition: tuple_find.h:51
Primary template for helper struct used to define Tuple_find_index.
Definition: tuple_find.h:43
Struct template with with one template argument, having a member type Predicate, which is a type pred...
Definition: tuple_find.h:84