-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathproperties.h
318 lines (230 loc) · 10 KB
/
properties.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* Copyright (c) 2014, 2024, Oracle and/or its affiliates.
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 designed to work 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 either included with
the program or referenced in the documentation.
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 */
#ifndef DD__PROPERTIES_INCLUDED
#define DD__PROPERTIES_INCLUDED
#include <map>
#include "sql/dd/string_type.h" // String_type, Stringstream_type
struct MEM_ROOT;
namespace dd {
///////////////////////////////////////////////////////////////////////////
/**
The Properties class defines an interface for storing key=value pairs,
where both key and value may be UTF-8 strings.
The interface contains functions for testing whether a key exists,
replacing or removing key=value pairs, iteration etc. The interface
also defines template functions for converting between strings and
various primitive types.
Please note that in debug builds, the get() functions will assert that
the key exists. This is to make sure that non-existing keys are
handled explicitly at the client side.
The raw_string() function returns a semicolon separated list of all
key=value pairs. Characters '=' and ';' that are part of key or value
are escaped using the '\' as an escape character. The escape character
itself must also be escaped if being part of key or value.
Examples of usage:
Add key=value:
p->set("akey=avalue");
Add a numeric value:
p->set("intvalue", 1234);
Get values:
int32 num;
p->get("intvalue", &num);
Get raw string:
String_type mylist= p->raw_string();
Further comments can be found in the files properties_impl.{h,cc}
where the interface is implemented.
*/
class Properties {
public:
/**
Convert a string to a value of an integral type. Verify correct
sign, check for overflow and conversion errors.
@tparam T Value type.
@param number String containing integral value to convert.
@param[out] value Converted value.
@return operation status.
@retval true if an error occurred
@retval false if success
*/
template <typename T>
static bool from_str(const String_type &number, T *value);
/**
Convert string to bool. Valid values are "true", "false", and
decimal numbers, where "0" will be taken to mean false, and
numbers != 0 will be taken to mean true.
@param bool_str String containing boolean value to convert.
@param[out] value Converted value.
@return operation status.
@retval true if an error occurred
@retval false if success
*/
static bool from_str(const String_type &bool_str, bool *value);
/**
Convert a value of an integral type (including bool) to a string.
Create an output stream and write the value.
@tparam T Value type.
@param value Actual value to convert.
@return string containing representation of the value.
*/
template <class T>
static String_type to_str(T value);
/**
Parse the submitted string for properties on the format
"key=value;key=value;...". Create new property object and add
the properties to the map in the object.
@param raw_properties string containing list of key=value pairs
@return pointer to new Property_impl object
@retval NULL if an error occurred
*/
static Properties *parse_properties(const String_type &raw_properties);
typedef std::map<String_type, String_type> Map;
typedef std::map<String_type, String_type>::size_type size_type;
typedef Map::iterator iterator;
typedef Map::const_iterator const_iterator;
virtual iterator begin() = 0;
virtual const_iterator begin() const = 0;
virtual iterator end() = 0;
virtual const_iterator end() const = 0;
/**
Insert keys and values from a different property object.
@note The valid keys in the target object are used to filter out invalid
keys, which will be silently ignored. The set of valid keys in the
source object is not copied.
@pre The 'this' object shall be empty.
@param properties Object which will have its properties
copied to 'this' object.
@return operation outcome, false if success, otherwise true.
*/
virtual bool insert_values(const Properties &properties) = 0;
/**
Insert keys and values from a raw string.
Invalid keys will be silently ignored, using the set of valid keys in
the target object as a filter. The source is a string, so it has no
definition of valid keys.
@pre The 'this' object shall be empty.
@param raw_string String with key/value pairs which will be
parsed and added to the 'this' object.
@return operation outcome, false if success, otherwise true.
*/
virtual bool insert_values(const String_type &raw_string) = 0;
/**
Get the number of key=value pairs.
@note Invalid keys that are present will also be reflected in the count.
@return number of key=value pairs
*/
virtual size_type size() const = 0;
/**
Are there any key=value pairs?
@return true if there is no key=value pair, else false.
*/
virtual bool empty() const = 0;
/**
Remove all key=value pairs.
*/
virtual void clear() = 0;
/**
Check if the submitted key is valid.
@param key Key to be checked.
@retval true if the key is valid, otherwise false.
*/
virtual bool valid_key(const String_type &key) const = 0;
/**
Check for the existence of a key=value pair given the key.
@param key Key to be checked.
@return true if the given key exists, false otherwise.
*/
virtual bool exists(const String_type &key) const = 0;
/**
Remove the key=value pair for the given key if it exists.
Otherwise, do nothing.
@param key key to lookup.
@return false if the given key existed, true otherwise.
*/
virtual bool remove(const String_type &key) = 0;
/**
Create a string containing all key=value pairs as a semicolon
separated list. Key and value are separated by '='. The '=' and
';' characters are escaped using '\' if part of key or value, hence,
the escape character '\' must also be escaped.
@return a string listing all key=value pairs.
*/
virtual const String_type raw_string() const = 0;
/**
Get the string value for a given key.
Return true (assert in debug builds) if the operation fails, i.e.,
if the key does not exist or if the key is invalid.
@param key Key to lookup the value for.
@param[out] value String value.
@return operation outcome, false if success, otherwise true.
*/
virtual bool get(const String_type &key, String_type *value) const = 0;
/**
Get the lex string value for a given key.
Return true (assert in debug builds) if the operation fails, i.e.,
if the key does not exist or if the key is invalid.
@tparam Lex_type Type of LEX string.
@param key Key to lookup the value for.
@param[out] value LEX_STRING or LEX_CSTRING value.
@param[in] mem_root MEM_ROOT to allocate string.
@return operation outcome, false if success, otherwise true.
*/
template <typename Lex_type>
bool get(const String_type &key, Lex_type *value, MEM_ROOT *mem_root) const;
/**
Get the string value for the key and convert it to the appropriate type.
Return true (assert in debug builds) if the operation fails, i.e.,
if the key does not exist or is invalid, or if the type conversion fails.
@tparam Value_type Type of the value to get.
@param key Key to lookup.
@param[out] value Value of appropriate type.
@return operation outcome, false if success, otherwise true.
*/
template <typename Value_type>
bool get(const String_type &key, Value_type *value) const;
/**
Add a new key=value pair where the value is a string. If the key already
exists, the associated value will be replaced by the new value argument.
Return true (assert in debug builds) if the operation fails, i.e.,
if the key is invalid.
@param key Key to map to a value.
@param value String value to be associated with the key.
@return operation outcome, false if success, otherwise true.
*/
virtual bool set(const String_type &key, const String_type &value) = 0;
/**
Convert the value to a string and set it for the given key.
Return true (assert in debug builds) if the operation fails, i.e.,
if the key is invalid.
@tparam Value_type Type of the value to set.
@param key Key to assign to.
@param[out] value Value of appropriate type.
@return operation outcome, false if success, otherwise true.
*/
template <typename Value_type>
bool set(const String_type &key, Value_type value) {
return set(key, to_str(value));
}
Properties() = default;
Properties(const Properties &) = default;
Properties &operator=(const Properties &) = delete;
virtual ~Properties() = default;
};
///////////////////////////////////////////////////////////////////////////
} // namespace dd
#endif // DD__PROPERTIES_INCLUDED