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