aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/poco/Util/src/LayeredConfiguration.cpp
blob: 1cbd5d784774f88db5d251b7c20178cdc776b1ea (plain) (blame)
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
//
// 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* pConfig)
{
	add(pConfig, highest(), false, true);
}


void LayeredConfiguration::add(AbstractConfiguration* pConfig, const std::string& label)
{
	add(pConfig, label, highest(), false, true);
}


void LayeredConfiguration::add(AbstractConfiguration* pConfig, bool shared)
{
	add(pConfig, highest(), false, shared);
}


void LayeredConfiguration::add(AbstractConfiguration* pConfig, const std::string& label, bool shared)
{
	add(pConfig, label, highest(), false, shared);
}


void LayeredConfiguration::add(AbstractConfiguration* pConfig, int priority)
{
	add(pConfig, priority, false, true);
}


void LayeredConfiguration::add(AbstractConfiguration* pConfig, const std::string& label, int priority)
{
	add(pConfig, label, priority, false, true);
}


void LayeredConfiguration::add(AbstractConfiguration* pConfig, int priority, bool shared)
{
	add(pConfig, priority, false, shared);
}


void LayeredConfiguration::add(AbstractConfiguration* pConfig, const std::string& label, int priority, bool shared)
{
	add(pConfig, label, priority, false, shared);
}


void LayeredConfiguration::addFront(AbstractConfiguration* pConfig)
{
	add(pConfig, lowest(), false, true);
}


void LayeredConfiguration::addFront(AbstractConfiguration* pConfig, bool shared)
{
	add(pConfig, lowest(), false, shared);
}


void LayeredConfiguration::addWriteable(AbstractConfiguration* pConfig, int priority)
{
	add(pConfig, priority, true, true);
}


void LayeredConfiguration::addWriteable(AbstractConfiguration* pConfig, int priority, bool shared)
{
	add(pConfig, priority, true, shared);
}


void LayeredConfiguration::add(AbstractConfiguration* pConfig, int priority, bool writeable, bool shared)
{
	add(pConfig, std::string(), priority, writeable, shared);
}


void LayeredConfiguration::add(AbstractConfiguration* pConfig, const std::string& label, int priority, bool writeable, bool shared)
{
	ConfigItem item;
	item.pConfig   = ConfigPtr(pConfig, shared);
	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* pConfig)
{
	for (ConfigList::iterator it = _configs.begin(); it != _configs.end(); ++it)
	{
		if (it->pConfig == pConfig)
		{
			_configs.erase(it);
			break;
		}
	}
}


LayeredConfiguration::ConfigPtr LayeredConfiguration::find(const std::string& label) const
{
	for (ConfigList::const_iterator it = _configs.begin(); it != _configs.end(); ++it)
	{
		if (it->label == label)
		{
			return it->pConfig;
		}
	}
	return 0;
}


bool LayeredConfiguration::getRaw(const std::string& key, std::string& value) const
{
	for (ConfigList::const_iterator it = _configs.begin(); it != _configs.end(); ++it)
	{
		if (it->pConfig->getRaw(key, value))
			return true;
	}
	return false;
}


void LayeredConfiguration::setRaw(const std::string& key, const std::string& value)
{
	for (ConfigList::iterator it = _configs.begin(); it != _configs.end(); ++it)
	{
		if (it->writeable)
		{
			it->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 (ConfigList::const_iterator itc = _configs.begin(); itc != _configs.end(); ++itc)
	{
		Keys partRange;
		itc->pConfig->enumerate(key, partRange);
		for (Keys::const_iterator itr = partRange.begin(); itr != partRange.end(); ++itr)
		{
			if (keys.find(*itr) == keys.end())
			{
				range.push_back(*itr);
				keys.insert(*itr);
			}
		}
	}
}


void LayeredConfiguration::removeRaw(const std::string& key)
{
	for (ConfigList::iterator it = _configs.begin(); it != _configs.end(); ++it)
	{
		if (it->writeable)
		{
			it->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