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