-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathconfiguration_config.cpp
More file actions
603 lines (484 loc) · 15.6 KB
/
configuration_config.cpp
File metadata and controls
603 lines (484 loc) · 15.6 KB
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/*---------------------------------------------------------------------------*\
*
* 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;
}
/*!
Gets a reference to the specified option.
\param key is the name of the option
\result A reference to the specified option.
*/
Config::Option & Config::getOption(const std::string &key)
{
return const_cast<Option &>(static_cast<const Config &>(*this).getOption(key));
}
/*!
Gets a constant reference to the specified option.
\param key is the name of the option
\result A constant reference to the specified option.
*/
const Config::Option & Config::getOption(const std::string &key) const
{
return (*m_options).at(key);
}
/*!
Gets the value of the specified option.
If the option does not exists an exception is thrown.
\param key is the name of the option
\result The value of the specified option.
*/
const std::string & Config::get(const std::string &key) const
{
return getOption(key).value;
}
/*!
Gets the value of 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 value of the specified option or the fallback value if the
options does not exist.
*/
std::string Config::get(const std::string &key, const std::string &fallback) const
{
if (hasOption(key)) {
return getOption(key).value;
} else {
return fallback;
}
}
/*!
Set the value of the specified option.
If the option does not exists, a new option will be added.
\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)
{
if (hasOption(key)) {
getOption(key).value = value;
} else {
addOption(key, value);
}
}
/*!
Gets the value of the specified option attribute.
If the option or the attribute does not exists, an exception is thrown.
\param key is the name of the option
\param name is the name of the attribute
\result The value of the specified attribute.
*/
const std::string & Config::getAttribute(const std::string &key, const std::string &name) const
{
return getOption(key).attributes.at(name);
}
/*!
Gets the value of the specified option attribute.
If the option does not exists, an exception will be thrown. However, if
the attribute do not exists, the fallback walue will be returned
\param key is the name of the option
\param name is the name of the attribute
\param fallback is the value that will be returned if the specified
attribute does not exist
\result The value of the specified attribute or the fallback value if
the options or the attribute does not exist.
*/
std::string Config::getAttribute(const std::string &key, const std::string &name, const std::string &fallback) const
{
const Option &option = getOption(key);
if (option.attributes.count(name) > 0) {
return option.attributes.at(name);
}
return fallback;
}
/*!
Set the value of the specified option attribute.
If the option does not exists, an exception will be thrown. However,
if the attribute does not exists, a new attribute will be added.
\param key is the name of the option
\param name is the name of the attribute
\param value is the value of the attribute
*/
void Config::setAttribute(const std::string &key, const std::string &name, const std::string &value)
{
getOption(key).attributes[name] = value;
}
/*!
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);
}
/*!
Add an option to the configuration storage.
If an option with the same key already exists, it will be overwritten.
\param key is the name of the option
\param option is the option that will be added
*/
void Config::addOption(const std::string &key, const Option &option)
{
(*m_options)[key] = option;
}
/*!
Add an option to the configuration storage.
If an option with the same key already exists, it will be overwritten.
\param key is the name of the option
\param option is the option that will be added
*/
void Config::addOption(const std::string &key, Option &&option)
{
(*m_options)[key] = std::move(option);
}
/*!
Add an option to the configuration storage.
If an option with the same key already exists, it will be overwritten.
\param key is the name of the option
\param value is the value of the option
*/
void Config::addOption(const std::string &key, const std::string &value)
{
addOption(key, std::string(value), Attributes());
}
/*!
Add an option to the configuration storage.
If an option with the same key already exists, it will be overwritten.
\param key is the name of the option
\param value is the value of the option
*/
void Config::addOption(const std::string &key, std::string &&value)
{
addOption(key, std::move(value), Attributes());
}
/*!
Add an option to the configuration storage.
If an option with the same key already exists, it will be overwritten.
\param key is the name of the option
\param value is the value of the option
\param attributes are the attributes of the option
*/
void Config::addOption(const std::string &key, const std::string &value, const Attributes &attributes)
{
Option option;
option.value = value;
option.attributes = attributes;
addOption(key, std::move(option));
}
/*!
Add an option to the configuration storage.
If an option with the same key already exists, it will be overwritten.
\param key is the name of the option
\param value is the value of the option
\param attributes are the attributes of the option
*/
void Config::addOption(const std::string &key, std::string &&value, Attributes &&attributes)
{
Option option;
option.value = std::move(value);
option.attributes = std::move(attributes);
addOption(key, std::move(option));
}
/*!
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()) {
const std::string &key = entry.first;
const Option &option = entry.second;
// Option value
out << indent << padding << key << " = " << option.value << std::endl;
// Option attributes
if (!option.attributes.empty()) {
for (const auto &attributeEntry : option.attributes) {
const std::string &name = attributeEntry.first;
const std::string &value = attributeEntry.second;
out << indent << padding << padding << "Attribute: " << name << " = " << value << std::endl;
}
} else {
out << indent << padding << padding << "Option has not attributes." << 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);
}
}
}