-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
abstract_options_provider.cc
204 lines (167 loc) · 7.52 KB
/
abstract_options_provider.cc
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
198
199
200
201
202
203
204
/*
Copyright (c) 2001, 2019, 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, version 2.0,
as published by the Free Software Foundation.
This program is also distributed with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have included with MySQL.
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, version 2.0, 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
*/
#include "client/base/abstract_options_provider.h"
#include <stdlib.h>
#include <iostream>
#include <vector>
#include "client/base/i_options_provider.h"
#include "my_dbug.h"
#include "my_getopt.h"
using std::map;
using std::string;
using std::vector;
using namespace Mysql::Tools::Base::Options;
using Mysql::Nullable;
Simple_option *Abstract_options_provider::create_new_option(
std::string name, std::string description) {
return this->attach_new_option<Simple_option>(
new Simple_option(name, description));
}
Disabled_option *Abstract_options_provider::create_new_disabled_option(
std::string name, std::string description) {
return this->attach_new_option<Disabled_option>(
new Disabled_option(name, description));
}
Char_array_option *Abstract_options_provider::create_new_option(
char **value, std::string name, std::string description) {
return this->attach_new_option<Char_array_option>(
new Char_array_option(value, false, name, description));
}
Password_option *Abstract_options_provider::create_new_password_option(
Nullable<string> *value, std::string name, std::string description) {
return this->attach_new_option<Password_option>(
new Password_option(value, name, description));
}
String_option *Abstract_options_provider::create_new_option(
Nullable<std::string> *value, std::string name, std::string description) {
return this->attach_new_option<String_option>(
new String_option(value, name, description));
}
Number_option<int32> *Abstract_options_provider::create_new_option(
int32 *value, std::string name, std::string description) {
return this->attach_new_option<Number_option<int32>>(
new Number_option<int32>(value, name, description));
}
Number_option<uint32> *Abstract_options_provider::create_new_option(
uint32 *value, std::string name, std::string description) {
return this->attach_new_option<Number_option<uint32>>(
new Number_option<uint32>(value, name, description));
}
Number_option<int64> *Abstract_options_provider::create_new_option(
int64 *value, std::string name, std::string description) {
return this->attach_new_option<Number_option<int64>>(
new Number_option<int64>(value, name, description));
}
Number_option<uint64> *Abstract_options_provider::create_new_option(
uint64 *value, std::string name, std::string description) {
return this->attach_new_option<Number_option<uint64>>(
new Number_option<uint64>(value, name, description));
}
Number_option<double> *Abstract_options_provider::create_new_option(
double *value, std::string name, std::string description) {
return this->attach_new_option<Number_option<double>>(
new Number_option<double>(value, name, description));
}
Bool_option *Abstract_options_provider::create_new_option(
bool *value, std::string name, std::string description) {
return this->attach_new_option<Bool_option>(
new Bool_option(value, name, description));
}
vector<my_option> Abstract_options_provider::generate_options() {
if (this->m_are_options_created == false) {
this->m_are_options_created = true;
this->create_options();
}
vector<my_option> res;
for (vector<I_option *>::iterator it = this->m_options_created.begin();
it != this->m_options_created.end(); it++) {
res.push_back((*it)->get_my_option());
}
return res;
}
void Abstract_options_provider::options_parsed() {}
Abstract_options_provider::Abstract_options_provider()
: m_are_options_created(false), m_option_changed_listener(nullptr) {}
Abstract_options_provider::~Abstract_options_provider() {
for (vector<I_option *>::iterator it = this->m_options_created.begin();
it != this->m_options_created.end(); it++) {
delete *it;
}
}
void Abstract_options_provider::set_option_changed_listener(
I_option_changed_listener *listener) {
DBUG_ASSERT(this->m_option_changed_listener == nullptr);
this->m_option_changed_listener = listener;
}
void Abstract_options_provider::notify_option_name_changed(I_option *source,
string old_name) {
// Check if it is modification or new assignment
if (old_name != "") {
this->m_name_usage.erase(this->m_name_usage.find(old_name));
}
string new_name = source->get_my_option().name;
// Try to find existing option with that name.
map<string, I_option *>::iterator name_item =
this->m_name_usage.find(new_name);
// Report error if already used.
if (name_item != this->m_name_usage.end()) {
std::cerr << "Cannot register new option \"" << new_name
<< "\" as it collides with existing one with following name \""
<< name_item->second->get_my_option().name
<< "\" and description: "
<< name_item->second->get_my_option().comment << std::endl;
exit(1);
}
// Add name usage.
this->m_name_usage.insert(std::make_pair(new_name, source));
// If we have listener we should inform it too.
if (this->m_option_changed_listener != nullptr) {
this->m_option_changed_listener->notify_option_name_changed(source,
old_name);
}
}
void Abstract_options_provider::notify_option_optid_changed(I_option *source,
uint32 old_optid) {
// Check if it is modification or new assignment
if (old_optid != 0) {
this->m_optid_usage.erase(this->m_optid_usage.find(old_optid));
}
uint32 new_optid = source->get_my_option().id;
// Try to find existing option with that optid.
map<uint32, I_option *>::iterator optid_item =
this->m_optid_usage.find(new_optid);
// Report error if already used.
if (optid_item != this->m_optid_usage.end()) {
string name = source->get_my_option().name;
std::cerr << "Cannot register new option \"" << name
<< "\" as it collides with existing one with following name \""
<< optid_item->second->get_my_option().name
<< "\" and description: "
<< optid_item->second->get_my_option().comment << std::endl;
exit(1);
}
// Add optid usage.
this->m_optid_usage.insert(std::make_pair(new_optid, source));
// If we have listener we should inform it too.
if (this->m_option_changed_listener != nullptr) {
this->m_option_changed_listener->notify_option_optid_changed(source,
old_optid);
}
}