forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayeredConfiguration.cpp
More file actions
185 lines (142 loc) · 3.37 KB
/
LayeredConfiguration.cpp
File metadata and controls
185 lines (142 loc) · 3.37 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
//
// LayeredConfiguration.cpp
//
// Library: Util
// Package: Configuration
// Module: LayeredConfiguration
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Util/LayeredConfiguration.h"
#include "Poco/Exception.h"
#include <set>
using Poco::AutoPtr;
using Poco::RuntimeException;
namespace Poco {
namespace Util {
LayeredConfiguration::LayeredConfiguration()
{
}
LayeredConfiguration::~LayeredConfiguration()
{
}
void LayeredConfiguration::add(AbstractConfiguration::Ptr pConfig)
{
add(pConfig, highest(), false);
}
void LayeredConfiguration::add(AbstractConfiguration::Ptr pConfig, const std::string& label)
{
add(pConfig, label, highest(), false);
}
void LayeredConfiguration::add(AbstractConfiguration::Ptr pConfig, int priority)
{
add(pConfig, priority, false);
}
void LayeredConfiguration::add(AbstractConfiguration::Ptr pConfig, const std::string& label, int priority)
{
add(pConfig, label, priority, false);
}
void LayeredConfiguration::addWriteable(AbstractConfiguration::Ptr pConfig, int priority)
{
add(pConfig, priority, true);
}
void LayeredConfiguration::add(AbstractConfiguration::Ptr pConfig, int priority, bool writeable)
{
add(pConfig, std::string(), priority, writeable);
}
void LayeredConfiguration::add(AbstractConfiguration::Ptr pConfig, const std::string& label, int priority, bool writeable)
{
ConfigItem item;
item.pConfig = pConfig;
item.priority = priority;
item.writeable = writeable;
item.label = label;
ConfigList::iterator it = _configs.begin();
while (it != _configs.end() && it->priority < priority) ++it;
_configs.insert(it, item);
}
void LayeredConfiguration::removeConfiguration(AbstractConfiguration::Ptr pConfig)
{
for (ConfigList::iterator it = _configs.begin(); it != _configs.end(); ++it)
{
if (it->pConfig == pConfig)
{
_configs.erase(it);
break;
}
}
}
AbstractConfiguration::Ptr LayeredConfiguration::find(const std::string& label) const
{
for (const auto& conf: _configs)
{
if (conf.label == label) return conf.pConfig;
}
return 0;
}
bool LayeredConfiguration::getRaw(const std::string& key, std::string& value) const
{
for (const auto& conf: _configs)
{
if (conf.pConfig->getRaw(key, value)) return true;
}
return false;
}
void LayeredConfiguration::setRaw(const std::string& key, const std::string& value)
{
for (auto& conf: _configs)
{
if (conf.writeable)
{
conf.pConfig->setRaw(key, value);
return;
}
}
throw RuntimeException("No writeable configuration object to store the property", key);
}
void LayeredConfiguration::enumerate(const std::string& key, Keys& range) const
{
std::set<std::string> keys;
for (const auto& conf: _configs)
{
Keys partRange;
conf.pConfig->enumerate(key, partRange);
for (const auto& k: partRange)
{
if (keys.find(k) == keys.end())
{
range.push_back(k);
keys.insert(k);
}
}
}
}
void LayeredConfiguration::removeRaw(const std::string& key)
{
for (auto& conf: _configs)
{
if (conf.writeable)
{
conf.pConfig->remove(key);
return;
}
}
}
int LayeredConfiguration::lowest() const
{
if (_configs.empty())
return 0;
else
return _configs.front().priority - 1;
}
int LayeredConfiguration::highest() const
{
if (_configs.empty())
return 0;
else
return _configs.back().priority + 1;
}
} } // namespace Poco::Util