mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-26 18:42:41 +01:00
Poco::Util::LayeredConfiguration: added support for labelling configurations and finding them by their label
This commit is contained in:
@@ -49,6 +49,8 @@ class Util_API LayeredConfiguration: public AbstractConfiguration
|
|||||||
/// If no priority is specified, a priority of 0 is assumed.
|
/// If no priority is specified, a priority of 0 is assumed.
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
typedef Poco::AutoPtr<AbstractConfiguration> ConfigPtr;
|
||||||
|
|
||||||
LayeredConfiguration();
|
LayeredConfiguration();
|
||||||
/// Creates the LayeredConfiguration.
|
/// Creates the LayeredConfiguration.
|
||||||
|
|
||||||
@@ -58,30 +60,60 @@ public:
|
|||||||
/// configuration. In other words, the configuration's reference
|
/// configuration. In other words, the configuration's reference
|
||||||
/// count is incremented.
|
/// count is incremented.
|
||||||
|
|
||||||
|
void add(AbstractConfiguration* pConfig, const std::string& label);
|
||||||
|
/// Adds a read-only configuration with the given label to the back of the LayeredConfiguration.
|
||||||
|
/// The LayeredConfiguration does not take ownership of the given
|
||||||
|
/// configuration. In other words, the configuration's reference
|
||||||
|
/// count is incremented.
|
||||||
|
|
||||||
void add(AbstractConfiguration* pConfig, bool shared);
|
void add(AbstractConfiguration* pConfig, bool shared);
|
||||||
/// Adds a read-only configuration to the back of the LayeredConfiguration.
|
/// Adds a read-only configuration to the back of the LayeredConfiguration.
|
||||||
/// If shared is false, the LayeredConfiguration takes ownership
|
/// If shared is false, the LayeredConfiguration takes ownership
|
||||||
/// of the given configuration (and the configuration's reference
|
/// of the given configuration (and the configuration's reference
|
||||||
/// count remains unchanged).
|
/// count remains unchanged).
|
||||||
|
|
||||||
|
void add(AbstractConfiguration* pConfig, const std::string& label, bool shared);
|
||||||
|
/// Adds a read-only configuration with the given label to the back of the LayeredConfiguration.
|
||||||
|
/// If shared is false, the LayeredConfiguration takes ownership
|
||||||
|
/// of the given configuration (and the configuration's reference
|
||||||
|
/// count remains unchanged).
|
||||||
|
|
||||||
void add(AbstractConfiguration* pConfig, int priority);
|
void add(AbstractConfiguration* pConfig, int priority);
|
||||||
/// Adds a read-only configuration to the LayeredConfiguration.
|
/// Adds a read-only configuration to the LayeredConfiguration.
|
||||||
/// The LayeredConfiguration does not take ownership of the given
|
/// The LayeredConfiguration does not take ownership of the given
|
||||||
/// configuration. In other words, the configuration's reference
|
/// configuration. In other words, the configuration's reference
|
||||||
/// count is incremented.
|
/// count is incremented.
|
||||||
|
|
||||||
|
void add(AbstractConfiguration* pConfig, const std::string& label, int priority);
|
||||||
|
/// Adds a read-only configuration with the given label to the LayeredConfiguration.
|
||||||
|
/// The LayeredConfiguration does not take ownership of the given
|
||||||
|
/// configuration. In other words, the configuration's reference
|
||||||
|
/// count is incremented.
|
||||||
|
|
||||||
void add(AbstractConfiguration* pConfig, int priority, bool shared);
|
void add(AbstractConfiguration* pConfig, int priority, bool shared);
|
||||||
/// Adds a read-only configuration the LayeredConfiguration.
|
/// Adds a read-only configuration the LayeredConfiguration.
|
||||||
/// If shared is false, the LayeredConfiguration takes ownership
|
/// If shared is false, the LayeredConfiguration takes ownership
|
||||||
/// of the given configuration (and the configuration's reference
|
/// of the given configuration (and the configuration's reference
|
||||||
/// count remains unchanged).
|
/// count remains unchanged).
|
||||||
|
|
||||||
|
void add(AbstractConfiguration* pConfig, const std::string& label, int priority, bool shared);
|
||||||
|
/// Adds a read-only configuration with the given label the LayeredConfiguration.
|
||||||
|
/// If shared is false, the LayeredConfiguration takes ownership
|
||||||
|
/// of the given configuration (and the configuration's reference
|
||||||
|
/// count remains unchanged).
|
||||||
|
|
||||||
void add(AbstractConfiguration* pConfig, int priority, bool writeable, bool shared);
|
void add(AbstractConfiguration* pConfig, int priority, bool writeable, bool shared);
|
||||||
/// Adds a configuration to the LayeredConfiguration.
|
/// Adds a configuration to the LayeredConfiguration.
|
||||||
/// If shared is false, the LayeredConfiguration takes ownership
|
/// If shared is false, the LayeredConfiguration takes ownership
|
||||||
/// of the given configuration (and the configuration's reference
|
/// of the given configuration (and the configuration's reference
|
||||||
/// count remains unchanged).
|
/// count remains unchanged).
|
||||||
|
|
||||||
|
void add(AbstractConfiguration* pConfig, const std::string& label, int priority, bool writeable, bool shared);
|
||||||
|
/// Adds a configuration with the given label to the LayeredConfiguration.
|
||||||
|
/// If shared is false, the LayeredConfiguration takes ownership
|
||||||
|
/// of the given configuration (and the configuration's reference
|
||||||
|
/// count remains unchanged).
|
||||||
|
|
||||||
void addWriteable(AbstractConfiguration* pConfig, int priority);
|
void addWriteable(AbstractConfiguration* pConfig, int priority);
|
||||||
/// Adds a writeable configuration to the LayeredConfiguration.
|
/// Adds a writeable configuration to the LayeredConfiguration.
|
||||||
/// The LayeredConfiguration does not take ownership of the given
|
/// The LayeredConfiguration does not take ownership of the given
|
||||||
@@ -93,6 +125,11 @@ public:
|
|||||||
/// If shared is false, the LayeredConfiguration takes ownership
|
/// If shared is false, the LayeredConfiguration takes ownership
|
||||||
/// of the given configuration (and the configuration's reference
|
/// of the given configuration (and the configuration's reference
|
||||||
/// count remains unchanged).
|
/// count remains unchanged).
|
||||||
|
|
||||||
|
ConfigPtr find(const std::string& label) const;
|
||||||
|
/// Finds and returns the configuration with the given label.
|
||||||
|
///
|
||||||
|
/// Returns null if no such configuration can be found.
|
||||||
|
|
||||||
//@ deprecated
|
//@ deprecated
|
||||||
void addFront(AbstractConfiguration* pConfig);
|
void addFront(AbstractConfiguration* pConfig);
|
||||||
@@ -113,14 +150,13 @@ public:
|
|||||||
/// Does nothing if the given configuration is not part of the
|
/// Does nothing if the given configuration is not part of the
|
||||||
/// LayeredConfiguration.
|
/// LayeredConfiguration.
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
typedef Poco::AutoPtr<AbstractConfiguration> ConfigPtr;
|
|
||||||
|
|
||||||
struct ConfigItem
|
struct ConfigItem
|
||||||
{
|
{
|
||||||
ConfigPtr pConfig;
|
ConfigPtr pConfig;
|
||||||
int priority;
|
int priority;
|
||||||
bool writeable;
|
bool writeable;
|
||||||
|
std::string label;
|
||||||
};
|
};
|
||||||
|
|
||||||
bool getRaw(const std::string& key, std::string& value) const;
|
bool getRaw(const std::string& key, std::string& value) const;
|
||||||
|
|||||||
@@ -43,24 +43,48 @@ void LayeredConfiguration::add(AbstractConfiguration* pConfig)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void LayeredConfiguration::add(AbstractConfiguration* pConfig, const std::string& label)
|
||||||
|
{
|
||||||
|
add(pConfig, label, highest(), false, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void LayeredConfiguration::add(AbstractConfiguration* pConfig, bool shared)
|
void LayeredConfiguration::add(AbstractConfiguration* pConfig, bool shared)
|
||||||
{
|
{
|
||||||
add(pConfig, highest(), false, 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)
|
void LayeredConfiguration::add(AbstractConfiguration* pConfig, int priority)
|
||||||
{
|
{
|
||||||
add(pConfig, priority, false, true);
|
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)
|
void LayeredConfiguration::add(AbstractConfiguration* pConfig, int priority, bool shared)
|
||||||
{
|
{
|
||||||
add(pConfig, priority, false, 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)
|
void LayeredConfiguration::addFront(AbstractConfiguration* pConfig)
|
||||||
{
|
{
|
||||||
add(pConfig, lowest(), false, true);
|
add(pConfig, lowest(), false, true);
|
||||||
@@ -86,11 +110,18 @@ void LayeredConfiguration::addWriteable(AbstractConfiguration* pConfig, int prio
|
|||||||
|
|
||||||
|
|
||||||
void LayeredConfiguration::add(AbstractConfiguration* pConfig, int priority, bool writeable, bool 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;
|
ConfigItem item;
|
||||||
item.pConfig = ConfigPtr(pConfig, shared);
|
item.pConfig = ConfigPtr(pConfig, shared);
|
||||||
item.priority = priority;
|
item.priority = priority;
|
||||||
item.writeable = writeable;
|
item.writeable = writeable;
|
||||||
|
item.label = label;
|
||||||
|
|
||||||
ConfigList::iterator it = _configs.begin();
|
ConfigList::iterator it = _configs.begin();
|
||||||
while (it != _configs.end() && it->priority < priority)
|
while (it != _configs.end() && it->priority < priority)
|
||||||
@@ -113,6 +144,19 @@ void LayeredConfiguration::removeConfiguration(AbstractConfiguration* pConfig)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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
|
bool LayeredConfiguration::getRaw(const std::string& key, std::string& value) const
|
||||||
{
|
{
|
||||||
for (ConfigList::const_iterator it = _configs.begin(); it != _configs.end(); ++it)
|
for (ConfigList::const_iterator it = _configs.begin(); it != _configs.end(); ++it)
|
||||||
|
|||||||
@@ -197,6 +197,23 @@ void LayeredConfigurationTest::testRemove()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void LayeredConfigurationTest::testFind()
|
||||||
|
{
|
||||||
|
AutoPtr<LayeredConfiguration> pLC = new LayeredConfiguration;
|
||||||
|
AutoPtr<AbstractConfiguration> pMC1 = new MapConfiguration;
|
||||||
|
AutoPtr<AbstractConfiguration> pMC2 = new MapConfiguration;
|
||||||
|
|
||||||
|
pLC->add(pMC1, 0);
|
||||||
|
pLC->add(pMC2, "label", -1);
|
||||||
|
|
||||||
|
AutoPtr<AbstractConfiguration> pFound = pLC->find("label");
|
||||||
|
assert (pFound == pMC2);
|
||||||
|
|
||||||
|
pFound = pLC->find("notfound");
|
||||||
|
assert (pFound.isNull());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
AbstractConfiguration* LayeredConfigurationTest::allocConfiguration() const
|
AbstractConfiguration* LayeredConfigurationTest::allocConfiguration() const
|
||||||
{
|
{
|
||||||
LayeredConfiguration* pLC = new LayeredConfiguration;
|
LayeredConfiguration* pLC = new LayeredConfiguration;
|
||||||
@@ -230,6 +247,7 @@ CppUnit::Test* LayeredConfigurationTest::suite()
|
|||||||
CppUnit_addTest(pSuite, LayeredConfigurationTest, testTwoLayers);
|
CppUnit_addTest(pSuite, LayeredConfigurationTest, testTwoLayers);
|
||||||
CppUnit_addTest(pSuite, LayeredConfigurationTest, testThreeLayers);
|
CppUnit_addTest(pSuite, LayeredConfigurationTest, testThreeLayers);
|
||||||
CppUnit_addTest(pSuite, LayeredConfigurationTest, testRemove);
|
CppUnit_addTest(pSuite, LayeredConfigurationTest, testRemove);
|
||||||
|
CppUnit_addTest(pSuite, LayeredConfigurationTest, testFind);
|
||||||
|
|
||||||
return pSuite;
|
return pSuite;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ public:
|
|||||||
void testTwoLayers();
|
void testTwoLayers();
|
||||||
void testThreeLayers();
|
void testThreeLayers();
|
||||||
void testRemove();
|
void testRemove();
|
||||||
|
void testFind();
|
||||||
|
|
||||||
void setUp();
|
void setUp();
|
||||||
void tearDown();
|
void tearDown();
|
||||||
|
|||||||
Reference in New Issue
Block a user