MySQL 8.0.39
Source Code Documentation
abstract_string_option.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2014, 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, version 2.0, 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 ABSTRACT_STRING_OPTION_INCLUDED
27#define ABSTRACT_STRING_OPTION_INCLUDED
28
29#include <functional>
30#include <optional>
31#include <string>
32
34#include "my_getopt.h"
35
36namespace Mysql {
37namespace Tools {
38namespace Base {
39namespace Options {
40
41/**
42 Abstract option to handle options accepting string value argument.
43 */
44template <typename T_type>
46 public:
47 /**
48 Sets value for this option. If it is specified before handling commandline
49 options then supplied value is used as default value of this option.
50 */
51 T_type *set_value(std::string value);
52
53 protected:
54 /**
55 Constructs new string option.
56 @param value Pointer to string object to receive option value.
57 @param var_type my_getopt internal option type.
58 @param name Name of option. It is used in command line option name as
59 --name.
60 @param description Description of option to be printed in --help.
61 */
62 Abstract_string_option(std::optional<std::string> *value, ulong var_type,
63 std::string name, std::string description);
64
65 std::optional<std::string> *m_destination_value;
66
67 private:
68 void string_callback(char *argument);
69
70 const char *m_original_value;
71};
72
73template <typename T_type>
75 std::optional<std::string> *value, ulong var_type, std::string name,
76 std::string description)
77 : Abstract_value_option<T_type>(&this->m_original_value, var_type, name,
78 description, (uint64)NULL),
79 m_destination_value(value) {
80 *value = std::optional<std::string>();
81 this->m_original_value = nullptr;
82
83 this->add_callback(new std::function<void(char *)>(
85 std::placeholders::_1)));
86}
87
88template <typename T_type>
90 *this->m_destination_value = std::optional<std::string>(value);
91 this->m_original_value = this->m_destination_value->value().c_str();
92 this->m_option_structure.def_value =
93 (uint64)this->m_destination_value->value().c_str();
94 return (T_type *)this;
95}
96
97template <typename T_type>
99 if (argument != nullptr) {
100 // Copy argument value from char* to destination string.
101 *this->m_destination_value =
102 std::optional<std::string>(this->m_original_value);
103 } else {
104 // There is no argument supplied, we shouldn't change default value.
105 }
106}
107
108} // namespace Options
109} // namespace Base
110} // namespace Tools
111} // namespace Mysql
112
113#endif
void add_callback(std::function< void(char *)> *callback)
Adds new callback for this option for option_parsed() event to callback chain.
Definition: abstract_option.h:120
Abstract option to handle options accepting string value argument.
Definition: abstract_string_option.h:45
void string_callback(char *argument)
Definition: abstract_string_option.h:98
std::optional< std::string > * m_destination_value
Definition: abstract_string_option.h:65
const char * m_original_value
Definition: abstract_string_option.h:70
Abstract_string_option(std::optional< std::string > *value, ulong var_type, std::string name, std::string description)
Constructs new string option.
Definition: abstract_string_option.h:74
T_type * set_value(std::string value)
Sets value for this option.
Definition: abstract_string_option.h:89
Abstract option to handle options accepting value argument.
Definition: abstract_value_option.h:43
uint64_t uint64
Definition: my_inttypes.h:69
constexpr pos_type Options
Definition: http_request.h:261
Definition: abstract_connection_program.h:38
stdx::expected< void, error_type > bind(native_handle_type native_handle, const struct sockaddr *addr, size_t addr_len)
wrap bind() in a portable way.
Definition: socket.h:339
case opt name
Definition: sslopt-case.h:33
#define NULL
Definition: types.h:55