MySQL 8.0.39
Source Code Documentation
abstract_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_OPTION_INCLUDED
27#define ABSTRACT_OPTION_INCLUDED
28
29#include <assert.h>
30#include <functional>
31#include <string>
32#include <vector>
33
35
36#include "my_getopt.h"
38
39namespace Mysql {
40namespace Tools {
41namespace Base {
42namespace Options {
43
44class Abstract_options_provider;
45
46/**
47 Abstract base with common option functionalities.
48 */
49template <typename T_type>
50class Abstract_option : public I_option {
51 public:
52 ~Abstract_option() override;
53
54 /**
55 Adds new callback for this option for option_parsed() event to callback
56 chain.
57 I_Callable can be replaced with std::Function<void(char*)> once we get
58 one.
59 */
60 void add_callback(std::function<void(char *)> *callback);
61
62 /**
63 Sets optid to given character to make possible usage of short option
64 alternative.
65 */
66 T_type *set_short_character(char code);
67
68 protected:
69 /**
70 Constructs new option.
71 @param value Pointer to object to receive option value.
72 @param var_type my_getopt internal option type.
73 @param name Name of option. It is used in command line option name as
74 --name.
75 @param description Description of option to be printed in --help.
76 @param default_value default value to be supplied to internal option
77 data structure.
78 */
79 Abstract_option(void *value, ulong var_type, std::string name,
80 std::string description, longlong default_value);
81
82 /**
83 Returns my_getopt internal option data structure representing this option.
84 To be used by Abstract_options_provider when preparing options array to
85 return.
86 */
88
89 /**
90 Method to set listener on option changed events.
91 For use from Abstract_options_provider class only.
92 */
94 I_option_changed_listener *listener) override;
95
97
98 private:
99 void call_callbacks(char *argument) override;
100
101 std::vector<std::function<void(char *)> *> m_callbacks;
103
105};
106
107template <typename T_type>
109 my_free(const_cast<char *>(m_option_structure.name));
110 my_free(const_cast<char *>(m_option_structure.comment));
111
112 for (std::vector<std::function<void(char *)> *>::iterator it =
113 this->m_callbacks.begin();
114 it != this->m_callbacks.end(); it++) {
115 delete *it;
116 }
117}
118
119template <typename T_type>
121 std::function<void(char *)> *callback) {
122 this->m_callbacks.push_back(callback);
123}
124
125template <typename T_type>
127 // Change optid to new one
128 uint32 old_optid = this->m_option_structure.id;
129 this->m_option_structure.id = (int)code;
130
131 // Inform that it has changed
132 if (this->m_option_changed_listener != nullptr) {
133 this->m_option_changed_listener->notify_option_optid_changed(this,
134 old_optid);
135 }
136
137 return (T_type *)this;
138}
139
140template <typename T_type>
142 std::string name,
143 std::string description,
144 longlong default_value)
145 : m_option_changed_listener(nullptr) {
149 this->m_option_structure.typelib = nullptr;
150 this->m_option_structure.u_max_value = nullptr;
151
152 this->m_option_structure.app_type = this;
155 my_strdup(PSI_NOT_INSTRUMENTED, description.c_str(), MYF(MY_FAE));
156 // This in future can be changed to atomic operation (compare_and_exchange)
159 ;
160 this->m_option_structure.def_value = default_value;
161
163 my_strdup(PSI_NOT_INSTRUMENTED, name.c_str(), MYF(MY_FAE));
164 /*
165 TODO mbabij 15-04-2014: this is based on previous usages of my_option.
166 Everyone sets this the same as my_option::value, explain why.
167 */
168 this->m_option_structure.u_max_value = value;
169
170 this->m_option_structure.value = value;
171 this->m_option_structure.var_type = var_type;
172 this->m_option_structure.arg_source = nullptr;
173}
174
175template <typename T_type>
177 return this->m_option_structure;
178}
179
180template <typename T_type>
182 I_option_changed_listener *listener) {
183 assert(this->m_option_changed_listener == nullptr);
184
185 this->m_option_changed_listener = listener;
186}
187
188template <typename T_type>
190 std::vector<std::function<void(char *)> *>::iterator callback_it;
191 for (callback_it = this->m_callbacks.begin();
192 callback_it != this->m_callbacks.end(); callback_it++) {
193 (**callback_it)(argument);
194 }
195}
196
197} // namespace Options
198} // namespace Base
199} // namespace Tools
200} // namespace Mysql
201
202#endif
Abstract base with common option functionalities.
Definition: abstract_option.h:50
my_option get_my_option() override
Returns my_getopt internal option data structure representing this option.
Definition: abstract_option.h:176
~Abstract_option() override
Definition: abstract_option.h:108
I_option_changed_listener * m_option_changed_listener
Definition: abstract_option.h:102
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
std::vector< std::function< void(char *)> * > m_callbacks
Definition: abstract_option.h:101
T_type * set_short_character(char code)
Sets optid to given character to make possible usage of short option alternative.
Definition: abstract_option.h:126
Abstract_option(void *value, ulong var_type, std::string name, std::string description, longlong default_value)
Constructs new option.
Definition: abstract_option.h:141
void call_callbacks(char *argument) override
Calls all option value callbacks.
Definition: abstract_option.h:189
my_option m_option_structure
Definition: abstract_option.h:96
void set_option_changed_listener(I_option_changed_listener *listener) override
Method to set listener on option changed events.
Definition: abstract_option.h:181
Common abstract class for options providers.
Definition: abstract_options_provider.h:55
Interface for listeners on some of option changes.
Definition: i_option_changed_listener.h:41
Common interface for all program option objects.
Definition: i_option.h:45
static uint32 last_optid
Definition: i_option.h:67
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:222
#define MY_FAE
Definition: my_sys.h:122
@ REQUIRED_ARG
Definition: my_getopt.h:81
long long int longlong
Definition: my_inttypes.h:55
#define MYF(v)
Definition: my_inttypes.h:97
uint32_t uint32
Definition: my_inttypes.h:67
void my_free(void *ptr)
Frees the memory pointed by the ptr.
Definition: my_memory.cc:81
constexpr pos_type Options
Definition: http_request.h:261
Definition: abstract_connection_program.h:38
std::vector< T, ut::allocator< T > > vector
Specialization of vector which uses allocator.
Definition: ut0new.h:2874
char * my_strdup(PSI_memory_key key, const char *from, myf_t flags)
Definition: my_malloc.cc:548
case opt name
Definition: sslopt-case.h:33
Definition: my_getopt.h:93
const char * comment
option comment, for autom.
Definition: my_getopt.h:113
longlong min_value
Min allowed value (for numbers)
Definition: my_getopt.h:123
ulonglong max_value
Max allowed value (for numbers)
Definition: my_getopt.h:124
longlong def_value
Default value.
Definition: my_getopt.h:122
void * app_type
To be used by an application.
Definition: my_getopt.h:128
long block_size
Value should be a mult.
Definition: my_getopt.h:127
const char * name
Name of the option.
Definition: my_getopt.h:94
void * u_max_value
The user def.
Definition: my_getopt.h:118
ulong var_type
GET_BOOL, GET_ULL, etc.
Definition: my_getopt.h:120
TYPELIB * typelib
Pointer to possible values.
Definition: my_getopt.h:119
void * value
A pointer to the variable value.
Definition: my_getopt.h:117
enum get_opt_arg_type arg_type
e.g.
Definition: my_getopt.h:121
int id
For 0<id<=255 it's means one character for a short option (like -A), if >255 no short option is creat...
Definition: my_getopt.h:98
struct get_opt_arg_source * arg_source
Represents source/path from where this variable is set.
Definition: my_getopt.h:125
#define PSI_NOT_INSTRUMENTED
Definition: validate_password_imp.cc:42