MySQL 9.6.0
Source Code Documentation
parse_options.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_STRCONV_DECODE_PARSE_OPTIONS_H
25#define MYSQL_STRCONV_DECODE_PARSE_OPTIONS_H
26
27/// @file
28/// Experimental API header
29
30#include <tuple> // tuple
31#include "mysql/meta/is_specialization.h" // Is_specialization
32#include "mysql/strconv/decode/checker.h" // Is_checker
33#include "mysql/strconv/decode/repeat.h" // Repeat
34#include "mysql/strconv/formats/format.h" // Is_format
35#include "mysql/strconv/formats/text_format.h" // Text_format
36#include "mysql/utils/tuple_find.h" // tuple_find
37
38/// @addtogroup GroupLibsMysqlStrconv
39/// @{
40
41namespace mysql::strconv {
42
43// ==== Helpers ====
44
45namespace detail {
46
47/// Type predicate with a member constant `value` that is true if Test is a
48/// format.
49template <class Test>
50struct Is_format_pred : public std::bool_constant<Is_format<Test>> {};
51
52/// Type predicate with a member constant `value` that is true if Test is
53/// `Repeat`.
54template <class Test>
55struct Is_repeat_pred : public std::bool_constant<Is_repeat<Test>> {};
56
57/// Class template parameterized by an object type, holding a member class
58/// template, which is a type predicate whose member constant `value` is true if
59/// Test is a checker for the object type.
60template <class Test>
61struct Is_checker_pred : public std::bool_constant<Is_checker<Test>> {};
62
63/// The number of elements in a Compound_parse_options of Format type.
64template <class Tuple_t>
65constexpr std::size_t Format_count =
66 mysql::utils::Tuple_matching_element_type_count<Tuple_t, Is_format_pred>;
67
68/// The number of elements in a Compound_parse_options of Repeat type.
69template <class Tuple_t>
70constexpr std::size_t Repeat_count =
71 mysql::utils::Tuple_matching_element_type_count<Tuple_t, Is_repeat_pred>;
72
73/// The number of element in a Compound_parse_options of Checker type
74template <class Tuple_t>
75constexpr std::size_t Checker_count =
76 mysql::utils::Tuple_matching_element_type_count<Tuple_t, Is_checker_pred>;
77
78template <class Tuple>
80 // At most one tuple element of each kind of type.
81 (Format_count<Tuple> <= 1) && (Repeat_count<Tuple> <= 1) &&
82 (Checker_count<Tuple> <= 1) &&
83 // No other tuple elements
84 (Format_count<Tuple> + Repeat_count<Tuple> + Checker_count<Tuple> ==
85 std::tuple_size_v<Tuple>);
86
87} // namespace detail
88
89// ==== Compound_parse_options ====
90
91/// Represents parse options consisting of a tuple where each of the following
92/// elements occurs optionally: Format, Repeat, Checker.
93template <detail::Is_compound_parse_options_tuple Tuple_tp>
95 using Tuple_t = Tuple_tp;
97 static constexpr bool has_format = detail::Format_count<Tuple_t> > 0;
98 static constexpr bool has_repeat = detail::Repeat_count<Tuple_t> > 0;
99 static constexpr bool has_checker = detail::Checker_count<Tuple_t> > 0;
100 // Deliberate use of `::type`
101 // NOLINTNEXTLINE(modernize-type-traits)
102 using Format_t = std::conditional_t<
105 std::type_identity<Text_format>>::type;
106};
107
108/// Deduction guide
109template <class... Args_t>
110Compound_parse_options(std::tuple<Args_t...>)
111 -> Compound_parse_options<std::tuple<Args_t...>>;
112
114
115// ==== Concepts to identify Compound_parse_options ====
116
117/// True if Test is a Compound_parse_options object with at most one Format
118/// element, at most one Repeat element, and at most one other element.
119template <class Test>
122
123/// True for any kind of parse options: Format, Repeat, Checker, or
124/// Compound_parse_options.
125template <class Test>
126concept Is_parse_options = // this comment helps clang-fornat
129
130/// True for any kind of parse options: Format, Repeat, Checker, or
131/// Compound_parse_options.
132template <class Test>
133concept Is_parse_options_nocheck = // this comment helps clang-fornat
135 (Is_compound_parse_options<Test> && !Test::has_checker);
136
137/// True for any kind of parse options for which `get_repeat()` returns
138/// `Repeat_optional`, i.e., it is known at compile-time that a string of length
139/// 0 matches, and thus parse error is impossible. Functions taking such an
140/// argument can may declared without `[[nodiscard]]` (if no other errors are
141/// possible either).
142template <class Test>
143concept Is_parse_options_optional = // this comment helps clang-format
144 Is_parse_options<Test> && // this comment helps clang-format
145 requires(Test opt) {
146 { get_repeat(opt) } -> std::same_as<Repeat_optional>;
147 };
148
149// ==== Projection functions ====
150
151/// Return the Format component of any parse options that has one.
152auto get_format(const Is_parse_options auto &) { return Text_format{}; }
153auto get_format(const Is_format auto &format) { return format; }
154template <Is_compound_parse_options Compound_parse_options_t>
155 requires Compound_parse_options_t::has_format
156auto get_format(const Compound_parse_options_t &opt) {
157 return mysql::utils::tuple_find<detail::Is_format_pred>(opt.m_tuple);
158}
159
160/// Return the Repeat component of any parse options, if it exists; otherwise a
161/// default-constructed Repeat object.
162auto get_repeat(const Is_parse_options auto &) { return Repeat{}; }
163auto get_repeat(const Is_repeat auto &repeat) { return repeat; }
164template <Is_compound_parse_options Compound_parse_options_t>
165 requires Compound_parse_options_t::has_repeat
166auto get_repeat(const Compound_parse_options_t &opt) {
167 return mysql::utils::tuple_find<detail::Is_repeat_pred>(opt.m_tuple);
168}
169
170/// Invoke the Checker member of any parse options, if it exists; otherwise do
171/// nothing.
172void invoke_checker(const Is_parse_options auto &) {}
173void invoke_checker(const Is_checker auto &checker) { checker.check(); }
174template <Is_compound_parse_options Compound_parse_options_t>
175 requires Compound_parse_options_t::has_checker
176void invoke_checker(const Compound_parse_options_t &opt) {
178 mysql::utils::tuple_find<detail::Is_checker_pred>(opt.m_tuple));
179}
180
181// ==== Combine operator ====
182
183namespace detail {
184
185/// Given any form of parse options; Format, Repeat, Checker, or
186/// Compound_parse_options; return a Compound_parse_options object.
189}
190template <Is_format Format_t>
191auto make_compound_parse_options(const Format_t &format) {
193}
194template <Is_repeat Repeat_t>
195auto make_compound_parse_options(const Repeat_t &repeat) {
197}
198template <Is_checker Checker_t>
199auto make_compound_parse_options(const Checker_t &checker) {
201}
203 return opt;
204}
205
206} // namespace detail
207
208/// Combine two Parse Options objects into one.
209template <Is_parse_options Opt1, Is_parse_options Opt2>
210auto operator|(const Opt1 &opt1, const Opt2 &opt2) {
212 std::tuple_cat(detail::make_compound_parse_options(opt1).m_tuple,
214}
215
216} // namespace mysql::strconv
217
218// addtogroup GroupLibsMysqlStrconv
219/// @}
220
221#endif // ifndef MYSQL_STRCONV_DECODE_PARSE_OPTIONS_H
Experimental API header.
Represents a range of integers specifying the number of times a token or pattern should be repeated w...
Definition: repeat.h:119
Concept used to determine at compile time whether a given type Test is a template specialization of t...
Definition: is_specialization.h:68
True for all Checker specializations.
Definition: checker.h:78
True if Test is a Compound_parse_options object with at most one Format element, at most one Repeat e...
Definition: parse_options.h:120
True if Test is a format.
Definition: format.h:42
True for any kind of parse options: Format, Repeat, Checker, or Compound_parse_options.
Definition: parse_options.h:133
True for any kind of parse options for which get_repeat() returns Repeat_optional,...
Definition: parse_options.h:143
True for any kind of parse options: Format, Repeat, Checker, or Compound_parse_options.
Definition: parse_options.h:126
True if Test is either Repeat or Repeat_optional.
Definition: repeat.h:180
Experimental API header.
Experimental API header.
std::string format(const routing_guidelines::Session_info &session_info, bool extended_session_info)
Definition: dest_metadata_cache.cc:170
Definition: fts0fts.cc:236
constexpr std::size_t Repeat_count
The number of elements in a Compound_parse_options of Repeat type.
Definition: parse_options.h:70
auto make_compound_parse_options()
Given any form of parse options; Format, Repeat, Checker, or Compound_parse_options; return a Compoun...
Definition: parse_options.h:187
constexpr std::size_t Format_count
The number of elements in a Compound_parse_options of Format type.
Definition: parse_options.h:65
constexpr std::size_t Checker_count
The number of element in a Compound_parse_options of Checker type.
Definition: parse_options.h:75
Definition: gtid_binary_format.h:41
void invoke_checker(const Is_parse_options auto &)
Invoke the Checker member of any parse options, if it exists; otherwise do nothing.
Definition: parse_options.h:172
Compound_parse_options(std::tuple< Args_t... >) -> Compound_parse_options< std::tuple< Args_t... > >
Deduction guide.
auto get_repeat(const Is_parse_options auto &)
Return the Repeat component of any parse options, if it exists; otherwise a default-constructed Repea...
Definition: parse_options.h:162
auto get_format(const Is_parse_options auto &)
Return the Format component of any parse options that has one.
Definition: parse_options.h:152
auto operator|(const Opt1 &opt1, const Opt2 &opt2)
Combine two Parse Options objects into one.
Definition: parse_options.h:210
Experimental API header.
required string type
Definition: replication_group_member_actions.proto:34
Represents parse options consisting of a tuple where each of the following elements occurs optionally...
Definition: parse_options.h:94
static constexpr bool has_repeat
Definition: parse_options.h:98
Tuple_tp Tuple_t
Definition: parse_options.h:95
std::conditional_t< has_format, mysql::utils::Tuple_find_helper< Tuple_t, detail::Is_format_pred >, std::type_identity< Text_format > >::type Format_t
Definition: parse_options.h:105
static constexpr bool has_format
Definition: parse_options.h:97
Tuple_t m_tuple
Definition: parse_options.h:96
static constexpr bool has_checker
Definition: parse_options.h:99
Format tag to identify text format.
Definition: text_format.h:38
Class template parameterized by an object type, holding a member class template, which is a type pred...
Definition: parse_options.h:61
Type predicate with a member constant value that is true if Test is a format.
Definition: parse_options.h:50
Type predicate with a member constant value that is true if Test is Repeat.
Definition: parse_options.h:55
Primary template for helper struct used to define Tuple_find_index.
Definition: tuple_find.h:43
Experimental API header.
Experimental API header.