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