1 | // |
2 | // LayeredConfiguration.cpp |
3 | // |
4 | // Library: Util |
5 | // Package: Configuration |
6 | // Module: LayeredConfiguration |
7 | // |
8 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/Util/LayeredConfiguration.h" |
16 | #include "Poco/Exception.h" |
17 | #include <set> |
18 | |
19 | |
20 | using Poco::AutoPtr; |
21 | using Poco::RuntimeException; |
22 | |
23 | |
24 | namespace Poco { |
25 | namespace Util { |
26 | |
27 | |
28 | LayeredConfiguration::LayeredConfiguration() |
29 | { |
30 | } |
31 | |
32 | |
33 | LayeredConfiguration::~LayeredConfiguration() |
34 | { |
35 | } |
36 | |
37 | |
38 | void LayeredConfiguration::add(AbstractConfiguration::Ptr pConfig) |
39 | { |
40 | add(pConfig, highest(), false); |
41 | } |
42 | |
43 | |
44 | void LayeredConfiguration::add(AbstractConfiguration::Ptr pConfig, const std::string& label) |
45 | { |
46 | add(pConfig, label, highest(), false); |
47 | } |
48 | |
49 | |
50 | void LayeredConfiguration::add(AbstractConfiguration::Ptr pConfig, int priority) |
51 | { |
52 | add(pConfig, priority, false); |
53 | } |
54 | |
55 | |
56 | void LayeredConfiguration::add(AbstractConfiguration::Ptr pConfig, const std::string& label, int priority) |
57 | { |
58 | add(pConfig, label, priority, false); |
59 | } |
60 | |
61 | |
62 | void LayeredConfiguration::addWriteable(AbstractConfiguration::Ptr pConfig, int priority) |
63 | { |
64 | add(pConfig, priority, true); |
65 | } |
66 | |
67 | |
68 | void LayeredConfiguration::add(AbstractConfiguration::Ptr pConfig, int priority, bool writeable) |
69 | { |
70 | add(pConfig, std::string(), priority, writeable); |
71 | } |
72 | |
73 | |
74 | void LayeredConfiguration::add(AbstractConfiguration::Ptr pConfig, const std::string& label, int priority, bool writeable) |
75 | { |
76 | ConfigItem item; |
77 | item.pConfig = pConfig; |
78 | item.priority = priority; |
79 | item.writeable = writeable; |
80 | item.label = label; |
81 | |
82 | ConfigList::iterator it = _configs.begin(); |
83 | while (it != _configs.end() && it->priority < priority) ++it; |
84 | _configs.insert(it, item); |
85 | } |
86 | |
87 | |
88 | void LayeredConfiguration::removeConfiguration(AbstractConfiguration::Ptr pConfig) |
89 | { |
90 | for (ConfigList::iterator it = _configs.begin(); it != _configs.end(); ++it) |
91 | { |
92 | if (it->pConfig == pConfig) |
93 | { |
94 | _configs.erase(it); |
95 | break; |
96 | } |
97 | } |
98 | } |
99 | |
100 | |
101 | AbstractConfiguration::Ptr LayeredConfiguration::find(const std::string& label) const |
102 | { |
103 | for (ConfigList::const_iterator it = _configs.begin(); it != _configs.end(); ++it) |
104 | { |
105 | if (it->label == label) return it->pConfig; |
106 | } |
107 | return 0; |
108 | } |
109 | |
110 | |
111 | bool LayeredConfiguration::(const std::string& key, std::string& value) const |
112 | { |
113 | for (ConfigList::const_iterator it = _configs.begin(); it != _configs.end(); ++it) |
114 | { |
115 | if (it->pConfig->getRaw(key, value)) return true; |
116 | } |
117 | return false; |
118 | } |
119 | |
120 | |
121 | void LayeredConfiguration::setRaw(const std::string& key, const std::string& value) |
122 | { |
123 | for (ConfigList::iterator it = _configs.begin(); it != _configs.end(); ++it) |
124 | { |
125 | if (it->writeable) |
126 | { |
127 | it->pConfig->setRaw(key, value); return; |
128 | } |
129 | } |
130 | throw RuntimeException("No writeable configuration object to store the property" , key); |
131 | } |
132 | |
133 | |
134 | void LayeredConfiguration::enumerate(const std::string& key, Keys& range) const |
135 | { |
136 | std::set<std::string> keySet; |
137 | for (ConfigList::const_iterator itc = _configs.begin(); itc != _configs.end(); ++itc) |
138 | { |
139 | Keys partRange; |
140 | itc->pConfig->enumerate(key, partRange); |
141 | for (Keys::const_iterator itr = partRange.begin(); itr != partRange.end(); ++itr) |
142 | { |
143 | if (keySet.find(*itr) == keySet.end()) |
144 | { |
145 | range.push_back(*itr); |
146 | keySet.insert(*itr); |
147 | } |
148 | } |
149 | } |
150 | } |
151 | |
152 | |
153 | void LayeredConfiguration::removeRaw(const std::string& key) |
154 | { |
155 | for (ConfigList::iterator it = _configs.begin(); it != _configs.end(); ++it) |
156 | { |
157 | if (it->writeable) |
158 | { |
159 | it->pConfig->remove(key); |
160 | return; |
161 | } |
162 | } |
163 | } |
164 | |
165 | |
166 | int LayeredConfiguration::lowest() const |
167 | { |
168 | if (_configs.empty()) |
169 | return 0; |
170 | else |
171 | return _configs.front().priority - 1; |
172 | } |
173 | |
174 | |
175 | int LayeredConfiguration::highest() const |
176 | { |
177 | if (_configs.empty()) |
178 | return 0; |
179 | else |
180 | return _configs.back().priority + 1; |
181 | } |
182 | |
183 | |
184 | } } // namespace Poco::Util |
185 | |