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
20using Poco::AutoPtr;
21using Poco::RuntimeException;
22
23
24namespace Poco {
25namespace Util {
26
27
28LayeredConfiguration::LayeredConfiguration()
29{
30}
31
32
33LayeredConfiguration::~LayeredConfiguration()
34{
35}
36
37
38void LayeredConfiguration::add(AbstractConfiguration* pConfig)
39{
40 add(pConfig, highest(), false, true);
41}
42
43
44void LayeredConfiguration::add(AbstractConfiguration* pConfig, const std::string& label)
45{
46 add(pConfig, label, highest(), false, true);
47}
48
49
50void LayeredConfiguration::add(AbstractConfiguration* pConfig, bool shared)
51{
52 add(pConfig, highest(), false, shared);
53}
54
55
56void LayeredConfiguration::add(AbstractConfiguration* pConfig, const std::string& label, bool shared)
57{
58 add(pConfig, label, highest(), false, shared);
59}
60
61
62void LayeredConfiguration::add(AbstractConfiguration* pConfig, int priority)
63{
64 add(pConfig, priority, false, true);
65}
66
67
68void LayeredConfiguration::add(AbstractConfiguration* pConfig, const std::string& label, int priority)
69{
70 add(pConfig, label, priority, false, true);
71}
72
73
74void LayeredConfiguration::add(AbstractConfiguration* pConfig, int priority, bool shared)
75{
76 add(pConfig, priority, false, shared);
77}
78
79
80void LayeredConfiguration::add(AbstractConfiguration* pConfig, const std::string& label, int priority, bool shared)
81{
82 add(pConfig, label, priority, false, shared);
83}
84
85
86void LayeredConfiguration::addFront(AbstractConfiguration* pConfig)
87{
88 add(pConfig, lowest(), false, true);
89}
90
91
92void LayeredConfiguration::addFront(AbstractConfiguration* pConfig, bool shared)
93{
94 add(pConfig, lowest(), false, shared);
95}
96
97
98void LayeredConfiguration::addWriteable(AbstractConfiguration* pConfig, int priority)
99{
100 add(pConfig, priority, true, true);
101}
102
103
104void LayeredConfiguration::addWriteable(AbstractConfiguration* pConfig, int priority, bool shared)
105{
106 add(pConfig, priority, true, shared);
107}
108
109
110void LayeredConfiguration::add(AbstractConfiguration* pConfig, int priority, bool writeable, bool shared)
111{
112 add(pConfig, std::string(), priority, writeable, shared);
113}
114
115
116void LayeredConfiguration::add(AbstractConfiguration* pConfig, const std::string& label, int priority, bool writeable, bool shared)
117{
118 ConfigItem item;
119 item.pConfig = ConfigPtr(pConfig, shared);
120 item.priority = priority;
121 item.writeable = writeable;
122 item.label = label;
123
124 ConfigList::iterator it = _configs.begin();
125 while (it != _configs.end() && it->priority < priority)
126 ++it;
127
128 _configs.insert(it, item);
129}
130
131
132void LayeredConfiguration::removeConfiguration(AbstractConfiguration* pConfig)
133{
134 for (ConfigList::iterator it = _configs.begin(); it != _configs.end(); ++it)
135 {
136 if (it->pConfig == pConfig)
137 {
138 _configs.erase(it);
139 break;
140 }
141 }
142}
143
144
145LayeredConfiguration::ConfigPtr LayeredConfiguration::find(const std::string& label) const
146{
147 for (ConfigList::const_iterator it = _configs.begin(); it != _configs.end(); ++it)
148 {
149 if (it->label == label)
150 {
151 return it->pConfig;
152 }
153 }
154 return 0;
155}
156
157
158bool LayeredConfiguration::getRaw(const std::string& key, std::string& value) const
159{
160 for (ConfigList::const_iterator it = _configs.begin(); it != _configs.end(); ++it)
161 {
162 if (it->pConfig->getRaw(key, value))
163 return true;
164 }
165 return false;
166}
167
168
169void LayeredConfiguration::setRaw(const std::string& key, const std::string& value)
170{
171 for (ConfigList::iterator it = _configs.begin(); it != _configs.end(); ++it)
172 {
173 if (it->writeable)
174 {
175 it->pConfig->setRaw(key, value);
176 return;
177 }
178 }
179 throw RuntimeException("No writeable configuration object to store the property", key);
180}
181
182
183void LayeredConfiguration::enumerate(const std::string& key, Keys& range) const
184{
185 std::set<std::string> keySet;
186 for (ConfigList::const_iterator itc = _configs.begin(); itc != _configs.end(); ++itc)
187 {
188 Keys partRange;
189 itc->pConfig->enumerate(key, partRange);
190 for (Keys::const_iterator itr = partRange.begin(); itr != partRange.end(); ++itr)
191 {
192 if (keySet.find(*itr) == keySet.end())
193 {
194 range.push_back(*itr);
195 keySet.insert(*itr);
196 }
197 }
198 }
199}
200
201
202void LayeredConfiguration::removeRaw(const std::string& key)
203{
204 for (ConfigList::iterator it = _configs.begin(); it != _configs.end(); ++it)
205 {
206 if (it->writeable)
207 {
208 it->pConfig->remove(key);
209 return;
210 }
211 }
212}
213
214
215int LayeredConfiguration::lowest() const
216{
217 if (_configs.empty())
218 return 0;
219 else
220 return _configs.front().priority - 1;
221}
222
223
224int LayeredConfiguration::highest() const
225{
226 if (_configs.empty())
227 return 0;
228 else
229 return _configs.back().priority + 1;
230}
231
232
233} } // namespace Poco::Util
234