/
abstract_option.h
197 lines (163 loc) · 5.57 KB
/
abstract_option.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
Copyright (c) 2014, 2016 Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef ABSTRACT_OPTION_INCLUDED
#define ABSTRACT_OPTION_INCLUDED
#include <string>
#include <vector>
#include "my_getopt.h"
#include "i_option_changed_listener.h"
#include "i_callable.h"
#include "mysql/service_mysql_alloc.h"
namespace Mysql{
namespace Tools{
namespace Base{
namespace Options{
class Abstract_options_provider;
/**
Abstract base with common option functionalities.
*/
template<typename T_type> class Abstract_option : public I_option
{
public:
virtual ~Abstract_option();
/**
Adds new callback for this option for option_parsed() event to callback
chain.
I_Callable can be replaced with std::Function<void(char*)> once we get
one.
*/
void add_callback(Mysql::I_callable<void, char*>* callback);
/**
Sets optid to given character to make possible usage of short option
alternative.
*/
T_type* set_short_character(char code);
protected:
/**
Constructs new option.
@param value Pointer to object to receive option value.
@param var_type my_getopt internal option type.
@param name Name of option. It is used in command line option name as
--name.
@param description Description of option to be printed in --help.
@param default_value default value to be supplied to internal option
data structure.
*/
Abstract_option(void* value, ulong var_type, std::string name,
std::string description, uint64 default_value);
/**
Returns my_getopt internal option data structure representing this option.
To be used by Abstract_options_provider when preparing options array to
return.
*/
my_option get_my_option();
/**
Method to set listener on option changed events.
For use from Abstract_options_provider class only.
*/
void set_option_changed_listener(I_option_changed_listener* listener);
my_option m_option_structure;
private:
void call_callbacks(char* argument);
std::vector<Mysql::I_callable<void, char*>*> m_callbacks;
I_option_changed_listener* m_option_changed_listener;
friend class Abstract_options_provider;
};
template<typename T_type> Abstract_option<T_type>::~Abstract_option()
{
my_free((void*)this->m_option_structure.name);
my_free((void*)this->m_option_structure.comment);
for (std::vector<Mysql::I_callable<void, char*>*>::iterator
it= this->m_callbacks.begin();
it != this->m_callbacks.end();
it++)
{
delete *it;
}
}
template<typename T_type> void Abstract_option<T_type>::add_callback(
Mysql::I_callable<void, char*>* callback)
{
this->m_callbacks.push_back(callback);
}
template<typename T_type> T_type* Abstract_option<T_type>::set_short_character(
char code)
{
// Change optid to new one
uint32 old_optid= this->m_option_structure.id;
this->m_option_structure.id= (int)code;
// Inform that it has changed
if (this->m_option_changed_listener != NULL)
{
this->m_option_changed_listener->notify_option_optid_changed(
this, old_optid);
}
return (T_type*)this;
}
template<typename T_type> Abstract_option<T_type>::Abstract_option(void* value,
ulong var_type, std::string name, std::string description,
uint64 default_value)
: m_option_changed_listener(NULL)
{
this->m_option_structure.block_size= 0;
this->m_option_structure.max_value= 0;
this->m_option_structure.min_value= 0;
this->m_option_structure.typelib= NULL;
this->m_option_structure.u_max_value= NULL;
this->m_option_structure.app_type= this;
this->m_option_structure.arg_type= REQUIRED_ARG;
this->m_option_structure.comment= my_strdup(
PSI_NOT_INSTRUMENTED, description.c_str(), MYF(MY_FAE));
// This in future can be changed to atomic operation (compare_and_exchange)
this->m_option_structure.id= Abstract_option::last_optid;
Abstract_option::last_optid++;
;
this->m_option_structure.def_value= default_value;
this->m_option_structure.name= my_strdup(
PSI_NOT_INSTRUMENTED, name.c_str(), MYF(MY_FAE));
/*
TODO mbabij 15-04-2014: this is based on previous usages of my_option.
Everyone sets this the same as my_option::value, explain why.
*/
this->m_option_structure.u_max_value= value;
this->m_option_structure.value= value;
this->m_option_structure.var_type= var_type;
this->m_option_structure.arg_source= 0;
}
template<typename T_type> my_option Abstract_option<T_type>::get_my_option()
{
return this->m_option_structure;
}
template<typename T_type> void
Abstract_option<T_type>::set_option_changed_listener(
I_option_changed_listener* listener)
{
DBUG_ASSERT(this->m_option_changed_listener == NULL);
this->m_option_changed_listener= listener;
}
template<typename T_type> void Abstract_option<T_type>::call_callbacks(
char* argument)
{
std::vector<Mysql::I_callable<void, char*>*>::iterator callback_it;
for (callback_it= this->m_callbacks.begin();
callback_it != this->m_callbacks.end(); callback_it++)
{
(**callback_it)(argument);
}
}
}
}
}
}
#endif