MySQL 8.0.39
Source Code Documentation
config_option.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2019, 2024, 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 <stdexcept>
33#include <string>
34#include <string_view>
35#include <type_traits>
36
37#include "harness_export.h"
38
39namespace mysql_harness {
40
41double HARNESS_EXPORT
42option_as_double(const std::string &value, const std::string &option_desc,
43 double min_value = 0,
44 double max_value = std::numeric_limits<double>::max());
45
46/**
47 * Gets an integer using the given option value.
48 *
49 * Gets an integer using the given option value. The type can be
50 * any integer type such as uint16_t, int8_t and bool.
51 *
52 * The min_value argument can be used to set a minimum value for
53 * the option. For example, when 0 (zero) is not allowed, min_value
54 * can be set to 1. The maximum value is whatever the maximum of the
55 * use type is.
56 *
57 * Throws std::invalid_argument on errors.
58 *
59 * @param value Option value
60 * @param option_desc Option name
61 * @param min_value Minimum value
62 * @param max_value Maximum value
63 * @return value read from the configuration
64 */
65template <typename T>
66T option_as_int(const std::string_view &value, const std::string &option_desc,
67 T min_value = std::numeric_limits<T>::min(),
68 T max_value = std::numeric_limits<T>::max()) {
69 const char *start = value.data();
70 const char *end = start + value.size();
71
72 // from_chars has no support for <bool>, map it to uint8_t
73 using integral_type = std::conditional_t<std::is_same_v<T, bool>, uint8_t, T>;
74
75 integral_type int_result;
76 const auto [ptr, ec]{std::from_chars(start, end, int_result)};
77
78 if (ptr == end && ec == std::errc{}) {
79 // before comparing, cast back to the target_type
80 //
81 // without the cast, MSVC warns: warning C4804: '<=': unsafe use of type
82 // 'bool' in operation
83 if (int_result <= static_cast<integral_type>(max_value) &&
84 int_result >= static_cast<integral_type>(min_value)) {
85 return int_result;
86 }
87 }
88
89 throw std::invalid_argument(option_desc + " needs value between " +
90 std::to_string(min_value) + " and " +
91 std::to_string(max_value) + " inclusive, was '" +
92 std::string(value) + "'");
93}
94
95/**
96 * Get a unsigned integer.
97 *
98 * use option_as_int<T> instead.
99 */
100template <typename T>
101T option_as_uint(const std::string_view &value, const std::string &option_desc,
102 T min_value = std::numeric_limits<T>::min(),
103 T max_value = std::numeric_limits<T>::max()) {
104 return option_as_int<T>(value, option_desc, min_value, max_value);
105}
106
107template <typename T>
109 public:
110 constexpr IntOption(T min_value = std::numeric_limits<T>::min(),
111 T max_value = std::numeric_limits<T>::max())
112 : min_value_{min_value}, max_value_{max_value} {}
113
114 T operator()(const std::string &value, const std::string &option_desc) {
115 return mysql_harness::option_as_int(value, option_desc, min_value_,
116 max_value_);
117 }
118
119 private:
122};
123
125 public:
126 std::string operator()(const std::string &value,
127 const std::string & /* option_desc */) {
128 return value;
129 }
130};
131
133 public:
134 bool operator()(const std::string &value, const std::string &option_desc) {
135 if (value == "true" || value == "1") return true;
136 if (value == "false" || value == "0") return false;
137
138 throw std::invalid_argument(
139 option_desc + " needs a value of either 0, 1, false or true, was '" +
140 value + "'");
141 }
142};
143
144template <typename V>
146 public:
147 using value_type = V;
148
150 value_type min_value = 0,
151 value_type max_value = std::numeric_limits<value_type>::max())
152 : min_value_{min_value}, max_value_{max_value} {}
153
154 value_type operator()(const std::string &value,
155 const std::string &option_desc) {
156 return mysql_harness::option_as_double(value, option_desc, min_value_,
157 max_value_);
158 }
159
160 private:
163};
164
166
167template <typename Dur>
169 public:
170 using duration_type = Dur;
172
173 using __base::__base;
174
175 duration_type operator()(const std::string &value,
176 const std::string &option_desc) {
177 double result = __base::operator()(value, option_desc);
178
179 return std::chrono::duration_cast<std::chrono::milliseconds>(
180 std::chrono::duration<double>(result));
181 }
182};
183
184/**
185 * a double option with milli-second precision.
186 *
187 * input is seconds as double.
188 * output is a std::chrono::millisecond
189 */
191
192} // namespace mysql_harness
193#endif
Definition: config_option.h:132
bool operator()(const std::string &value, const std::string &option_desc)
Definition: config_option.h:134
Definition: config_option.h:168
Dur duration_type
Definition: config_option.h:170
duration_type operator()(const std::string &value, const std::string &option_desc)
Definition: config_option.h:175
Definition: config_option.h:145
FloatingPointOption(value_type min_value=0, value_type max_value=std::numeric_limits< value_type >::max())
Definition: config_option.h:149
value_type max_value_
Definition: config_option.h:162
value_type operator()(const std::string &value, const std::string &option_desc)
Definition: config_option.h:154
V value_type
Definition: config_option.h:147
value_type min_value_
Definition: config_option.h:161
Definition: config_option.h:108
T min_value_
Definition: config_option.h:120
T max_value_
Definition: config_option.h:121
constexpr IntOption(T min_value=std::numeric_limits< T >::min(), T max_value=std::numeric_limits< T >::max())
Definition: config_option.h:110
T operator()(const std::string &value, const std::string &option_desc)
Definition: config_option.h:114
Definition: config_option.h:124
std::string operator()(const std::string &value, const std::string &)
Definition: config_option.h:126
static void start(mysql_harness::PluginFuncEnv *env)
Definition: http_auth_backend_plugin.cc:177
static std::string to_string(const LEX_STRING &str)
Definition: lex_string.h:50
Definition: common.h:42
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:101
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:33
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:66
FloatingPointOption< double > DoubleOption
Definition: config_option.h:165
Definition: result.h:30
static std::enable_if_t< std::is_unsigned< T >::value, stdx::expected< T, std::error_code > > from_chars(const std::string &value, int base=10)
convert a numeric string to a number.
Definition: tcp_address.cc:63