MySQL 9.3.0
Source Code Documentation
config_option.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2019, 2025, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MYSQL_HARNESS_CONFIG_OPTION_INCLUDED
27#define MYSQL_HARNESS_CONFIG_OPTION_INCLUDED
28
29#include <charconv> // from_chars
30#include <chrono>
31#include <limits>
32#include <optional>
33#include <stdexcept>
34#include <string>
35#include <string_view>
36#include <type_traits>
37
40
41#include "harness_export.h"
42
43namespace mysql_harness {
44
45double HARNESS_EXPORT
46option_as_double(const std::string &value, const std::string &option_desc,
47 double min_value = 0,
48 double max_value = std::numeric_limits<double>::max());
49
50/**
51 * Gets an integer using the given option value.
52 *
53 * Gets an integer using the given option value. The type can be
54 * any integer type such as uint16_t, int8_t and bool.
55 *
56 * The min_value argument can be used to set a minimum value for
57 * the option. For example, when 0 (zero) is not allowed, min_value
58 * can be set to 1. The maximum value is whatever the maximum of the
59 * use type is.
60 *
61 * Throws std::invalid_argument on errors.
62 *
63 * @param value Option value
64 * @param option_desc Option name
65 * @param min_value Minimum value
66 * @param max_value Maximum value
67 * @return value read from the configuration
68 */
69template <typename T>
70T option_as_int(const std::string_view &value, const std::string &option_desc,
71 T min_value = std::numeric_limits<T>::min(),
72 T max_value = std::numeric_limits<T>::max()) {
73 const char *start = value.data();
74 const char *end = start + value.size();
75
76 // from_chars has no support for <bool>, map it to uint8_t
77 using integral_type = std::conditional_t<std::is_same_v<T, bool>, uint8_t, T>;
78
79 integral_type int_result;
80 const auto [ptr, ec]{std::from_chars(start, end, int_result)};
81
82 if (ptr == end && ec == std::errc{}) {
83 // before comparing, cast back to the target_type
84 //
85 // without the cast, MSVC warns: warning C4804: '<=': unsafe use of type
86 // 'bool' in operation
87 if (int_result <= static_cast<integral_type>(max_value) &&
88 int_result >= static_cast<integral_type>(min_value)) {
89 return int_result;
90 }
91 }
92
93 throw std::invalid_argument(option_desc + " needs value between " +
94 std::to_string(min_value) + " and " +
95 std::to_string(max_value) + " inclusive, was '" +
96 std::string(value) + "'");
97}
98
99/**
100 * Get a unsigned integer.
101 *
102 * use option_as_int<T> instead.
103 */
104template <typename T>
105T option_as_uint(const std::string_view &value, const std::string &option_desc,
106 T min_value = std::numeric_limits<T>::min(),
107 T max_value = std::numeric_limits<T>::max()) {
108 return option_as_int<T>(value, option_desc, min_value, max_value);
109}
110
111template <typename T>
113 public:
114 using value_type = T;
115
116 constexpr IntOption(T min_value = std::numeric_limits<T>::min(),
117 T max_value = std::numeric_limits<T>::max())
118 : min_value_{min_value}, max_value_{max_value} {}
119
120 T operator()(const std::string &value, const std::string &option_desc) {
121 return mysql_harness::option_as_int(value, option_desc, min_value_,
122 max_value_);
123 }
124
125 std::optional<T> operator()(const std::optional<std::string> &value,
126 const std::string &option_desc) {
127 if (!value.has_value()) return {};
128
129 return mysql_harness::option_as_int(value.value(), option_desc, min_value_,
130 max_value_);
131 }
132
133 private:
136};
137
138template <typename Option>
140 public:
141 using value_type = std::vector<typename Option::value_type>;
142
143 value_type operator()(const std::string &value,
144 const std::string &option_desc) {
146 auto array = mysql_harness::split_string(value, '"', false);
147
148 for (const auto &element : array) {
149 result.push_back(Option()(element, option_desc));
150 }
151
152 return result;
153 }
154};
155
157 public:
158 using value_type = std::string;
159 std::string operator()(const std::string &value,
160 const std::string & /* option_desc */) {
161 return value;
162 }
163};
164
166 public:
167 using value_type = bool;
168
169 bool operator()(const std::string &value, const std::string &option_desc) {
170 if (value == "true" || value == "1") return true;
171 if (value == "false" || value == "0") return false;
172
173 throw std::invalid_argument(
174 option_desc + " needs a value of either 0, 1, false or true, was '" +
175 value + "'");
176 }
177};
178
179template <typename V>
181 public:
182 using value_type = V;
183
185 value_type min_value = 0,
187 : min_value_{min_value}, max_value_{max_value} {}
188
189 value_type operator()(const std::string &value,
190 const std::string &option_desc) {
192 max_value_);
193 }
194
195 private:
198};
199
201
202template <typename Dur>
204 public:
205 using value_type = Dur;
206 using duration_type = Dur;
208
209 using __base::__base;
210
211 duration_type operator()(const std::string &value,
212 const std::string &option_desc) {
213 double result = __base::operator()(value, option_desc);
214
215 return std::chrono::duration_cast<duration_type>(
216 std::chrono::duration<double>(result));
217 }
218};
219
220/**
221 * a double option with milli-second precision.
222 *
223 * input is seconds as double.
224 * output is a std::chrono::millisecond
225 */
228
229} // namespace mysql_harness
230#endif
Definition: config_option.h:139
value_type operator()(const std::string &value, const std::string &option_desc)
Definition: config_option.h:143
std::vector< typename Option::value_type > value_type
Definition: config_option.h:141
Definition: config_option.h:165
bool value_type
Definition: config_option.h:167
bool operator()(const std::string &value, const std::string &option_desc)
Definition: config_option.h:169
Definition: config_option.h:203
Dur duration_type
Definition: config_option.h:206
duration_type operator()(const std::string &value, const std::string &option_desc)
Definition: config_option.h:211
Definition: config_option.h:180
FloatingPointOption(value_type min_value=0, value_type max_value=std::numeric_limits< value_type >::max())
Definition: config_option.h:184
value_type max_value_
Definition: config_option.h:197
value_type operator()(const std::string &value, const std::string &option_desc)
Definition: config_option.h:189
V value_type
Definition: config_option.h:182
value_type min_value_
Definition: config_option.h:196
Definition: config_option.h:112
std::optional< T > operator()(const std::optional< std::string > &value, const std::string &option_desc)
Definition: config_option.h:125
T min_value_
Definition: config_option.h:134
T value_type
Definition: config_option.h:114
T max_value_
Definition: config_option.h:135
constexpr IntOption(T min_value=std::numeric_limits< T >::min(), T max_value=std::numeric_limits< T >::max())
Definition: config_option.h:116
T operator()(const std::string &value, const std::string &option_desc)
Definition: config_option.h:120
Definition: config_option.h:156
std::string operator()(const std::string &value, const std::string &)
Definition: config_option.h:159
std::string value_type
Definition: config_option.h:158
static void start(mysql_harness::PluginFuncEnv *env)
Definition: http_auth_backend_plugin.cc:180
#define T
Definition: jit_executor_value.cc:373
static std::string to_string(const LEX_STRING &str)
Definition: lex_string.h:50
stdx::expected< T, std::error_code > from_chars(const std::string &value, int base=10)
convert a numeric string to a number.
Definition: destination.cc:61
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
ValueType max(X &&first)
Definition: gtid.h:103
Definition: common.h:44
T option_as_uint(const std::string_view &value, const std::string &option_desc, T min_value=std::numeric_limits< T >::min(), T max_value=std::numeric_limits< T >::max())
Get a unsigned integer.
Definition: config_option.h:105
HARNESS_EXPORT std::vector< std::string > split_string(const std::string_view &data, const char delimiter, bool allow_empty=true)
Splits a string using a delimiter.
Definition: string_utils.cc:37
double HARNESS_EXPORT option_as_double(const std::string &value, const std::string &option_desc, double min_value=0, double max_value=std::numeric_limits< double >::max())
Definition: config_option.cc:34
T option_as_int(const std::string_view &value, const std::string &option_desc, T min_value=std::numeric_limits< T >::min(), T max_value=std::numeric_limits< T >::max())
Gets an integer using the given option value.
Definition: config_option.h:70
FloatingPointOption< double > DoubleOption
Definition: config_option.h:200
struct result result
Definition: result.h:34
Definition: result.h:30