-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathconfiguration_config.cpp
419 lines (338 loc) · 10.2 KB
/
configuration_config.cpp
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*---------------------------------------------------------------------------*\
*
* bitpit
*
* Copyright (C) 2015-2021 OPTIMAD engineering Srl
*
* -------------------------------------------------------------------------
* License
* This file is part of bitpit.
*
* bitpit is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License v3 (LGPL)
* as published by the Free Software Foundation.
*
* bitpit 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 Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with bitpit. If not, see <http://www.gnu.org/licenses/>.
*
\*---------------------------------------------------------------------------*/
#include <stdexcept>
#include "configuration_config.hpp"
namespace bitpit {
/*!
\class Config
\ingroup Configuration
\brief Configuration storage
This class implements a configuration storage.
*/
/*!
Create a new configuration.
\param multiSections if set to true the configuration will allow multiple
sections with the same name
*/
Config::Config(bool multiSections)
: m_multiSections(multiSections),
m_options(std::unique_ptr<Options>(new Options())),
m_sections(std::unique_ptr<Sections>(new Sections()))
{
}
/*!
Copy constructor.
\param other is the object to be copied
*/
Config::Config(const Config &other)
: m_multiSections(other.m_multiSections),
m_options(std::unique_ptr<Options>(new Options(*(other.m_options)))),
m_sections(std::unique_ptr<Sections>(new Sections()))
{
for (const auto &entry : *(other.m_sections)) {
std::unique_ptr<Config> config = std::unique_ptr<Config>(new Config(*(entry.second)));
m_sections->insert(std::make_pair(entry.first, std::move(config)));
}
}
/*!
Copy assigment operator.
\param other is the object to be copied
*/
Config & Config::operator=(Config other)
{
this->swap(other);
return *this;
}
/*!
Swap operator.
\param other is another config whose content is swapped with that of
this config
*/
void Config::swap(Config &other)
{
std::swap(m_multiSections, other.m_multiSections);
std::swap(m_options, other.m_options);
std::swap(m_sections, other.m_sections);
}
/*!
Returns true if multi-sections are enabled.
\result Returns true if multi-sections are enabled.
*/
bool Config::isMultiSectionsEnabled() const
{
return m_multiSections;
}
/*!
Count the number of options.
\result The number of options stored.
*/
int Config::getOptionCount() const
{
return m_options->size();
}
/*!
Gets a reference to the stored options.
\result A reference to the stored options.
*/
Config::Options & Config::getOptions()
{
return const_cast<Options &>(static_cast<const Config &>(*this).getOptions());
}
/*!
Gets a constant reference to the stored options.
\result A constant reference to the stored options.
*/
const Config::Options & Config::getOptions() const
{
return *m_options;
}
/*!
Checks if the specified option exists.
\param key is the name of the option
\result True is the option exists, false otherwise.
*/
bool Config::hasOption(const std::string &key) const
{
return (m_options->count(key) > 0);
}
/*!
Gets the specified option.
If the option does not exists an exception is thrown.
\param key is the name of the option
\result The specified option.
*/
const std::string & Config::get(const std::string &key) const
{
return m_options->at(key);
}
/*!
Gets the specified option.
If the option does not exists the fallback value is returned.
\param key is the name of the option
\param fallback is the value that will be returned if the specified
options does not exist
\result The specified option or the fallback value if the specified
options does not exist.
*/
std::string Config::get(const std::string &key, const std::string &fallback) const
{
if (hasOption(key)) {
return get(key);
} else {
return fallback;
}
}
/*!
Set the given option to the specified value
\param key is the name of the option
\param value is the value of the option
*/
void Config::set(const std::string &key, const std::string &value)
{
(*m_options)[key] = value;
}
/*!
Remove the specified option.
\param key is the name of the option
\result Returns true if the option existed, otherwise it returns false.
*/
bool Config::removeOption(const std::string &key)
{
return (m_options->erase(key) != 0);
}
/*!
Count the number of sections.
\result The number of sections stored.
*/
int Config::getSectionCount() const
{
return m_sections->size();
}
/*!
Count the number of sections with the specified name.
\param key is the name of the section
\result The number of sections with the specified name.
*/
int Config::getSectionCount(const std::string &key) const
{
return m_sections->count(key);
}
/*!
Gets a reference to the stored sections.
\result A reference to the stored sections.
*/
Config::Sections & Config::getSections()
{
return const_cast<Sections &>(static_cast<const Config &>(*this).getSections());
}
/*!
Gets a constant reference to the stored options.
\result A constant reference to the stored options.
*/
const Config::Sections & Config::getSections() const
{
return *m_sections;
}
/*!
Gets a list of pointers to the sections with the specified name.
\param key is the name of the section
\result A list of pointers to the sections with the specified name.
*/
Config::MultiSection Config::getSections(const std::string &key)
{
MultiSection sections;
sections.reserve(getSectionCount(key));
auto range = m_sections->equal_range(key);
for (auto itr = range.first; itr != range.second; ++itr) {
sections.push_back(itr->second.get());
}
return sections;
}
/*!
Gets a list of constant pointers to the sections with the specified name.
\param key is the name of the section
\result A list of constant pointers to the sections with the specified name.
*/
Config::ConstMultiSection Config::getSections(const std::string &key) const
{
ConstMultiSection sections;
sections.reserve(getSectionCount(key));
auto range = m_sections->equal_range(key);
for (auto itr = range.first; itr != range.second; ++itr) {
sections.push_back(const_cast<const Section *>(itr->second.get()));
}
return sections;
}
/*!
Checks if the specified section exists.
\param key is the name of the section
\result True is the section exists, false otherwise.
*/
bool Config::hasSection(const std::string &key) const
{
return (getSectionCount(key) > 0);
}
/*!
Gets a reference to the specified section.
If the section does not exists an exception is thrown.
\param key is the name of the section
\result A reference to the specified section.
*/
Config::Section & Config::getSection(const std::string &key)
{
return const_cast<Section &>(static_cast<const Config &>(*this).getSection(key));
}
/*!
Gets a constant reference to the specified section.
If the section does not exists an exception is thrown.
\param key is the name of the section
\result A constant reference to the specified section.
*/
const Config::Section & Config::getSection(const std::string &key) const
{
auto sectionItr = m_sections->find(key);
if (sectionItr == m_sections->end()) {
throw std::runtime_error("The section named \"" + key + "\" does not esists");
}
return *(sectionItr->second);
}
/*!
Add a section with the specified name.
If a section with the given name already exists, an exception is raised.
\param key is the name of the section
\return A reference to the newly added section.
*/
Config::Section & Config::addSection(const std::string &key)
{
if (!m_multiSections && hasSection(key)) {
throw std::runtime_error("A section named \"" + key + "\" already esists");
}
std::unique_ptr<Section> section = std::unique_ptr<Section>(new Section(m_multiSections));
auto sectionItr = m_sections->insert(std::make_pair(key, std::move(section)));
return *(sectionItr->second);
}
/*!
Remove the specified section.
\param key is the name of the section
\result Returns true if the section existed, otherwise it returns false.
*/
bool Config::removeSection(const std::string &key)
{
return (m_sections->erase(key) != 0);
}
/*!
Clear the configuration.
*/
void Config::clear()
{
m_options->clear();
m_sections->clear();
}
/*!
Get a constant reference of the specified section.
If the section does not exists an exception is thrown.
\param key is the name of the section
\result A constant reference to the specified section.
*/
const Config::Section & Config::operator[](const std::string &key) const
{
return getSection(key);
}
/*!
Get a reference of the specified section.
If the section does not exists an exception is thrown.
\param key is the name of the section
\result A reference to the specified section.
*/
Config::Section & Config::operator[](const std::string &key)
{
return getSection(key);
}
/*!
Write the specified configuration to screen.
\param out is the stream where the configuration will be written
\param indentLevel is the indentation level
*/
void Config::dump(std::ostream &out, int indentLevel) const
{
const int INDENT_SIZE = 2;
std::string padding(INDENT_SIZE, ' ');
std::string indent(INDENT_SIZE * indentLevel, ' ');
out << std::endl;
out << indent << "Options..." << std::endl;
if (getOptionCount() > 0) {
for (const auto &entry : getOptions()) {
out << indent << padding << entry.first << " = " << entry.second << std::endl;
}
} else {
out << indent << padding << "No options." << std::endl;
}
++indentLevel;
for (const auto &entry : getSections()) {
out << std::endl;
out << indent << padding << "::: Section " << entry.first << " :::" << std::endl;
entry.second->dump(out, indentLevel);
}
}
}