merge LayeredConfiguration changes (labels) from 1.7.9

This commit is contained in:
Günter Obiltschnig 2017-09-01 20:35:22 +02:00
parent c81de1d34b
commit 1c639d6c2c
4 changed files with 101 additions and 2 deletions

View File

@ -49,6 +49,8 @@ class Util_API LayeredConfiguration: public AbstractConfiguration
/// If no priority is specified, a priority of 0 is assumed.
{
public:
typedef Poco::AutoPtr<AbstractConfiguration> ConfigPtr;
LayeredConfiguration();
/// Creates the LayeredConfiguration.
@ -58,30 +60,60 @@ public:
/// configuration. In other words, the configuration's reference
/// 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);
/// Adds a read-only configuration 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, 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);
/// Adds a read-only configuration 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, 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);
/// Adds a read-only configuration 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, 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);
/// Adds a configuration to 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, 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);
/// Adds a writeable configuration to the LayeredConfiguration.
/// The LayeredConfiguration does not take ownership of the given
@ -94,6 +126,11 @@ public:
/// of the given configuration (and the configuration's reference
/// 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
void addFront(AbstractConfiguration* pConfig);
/// Adds a read-only configuration to the front of the LayeredConfiguration.
@ -114,13 +151,12 @@ public:
/// LayeredConfiguration.
protected:
typedef Poco::AutoPtr<AbstractConfiguration> ConfigPtr;
struct ConfigItem
{
ConfigPtr pConfig;
int priority;
bool writeable;
std::string label;
};
bool getRaw(const std::string& key, std::string& value) const;

View File

@ -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)
{
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);
@ -86,11 +110,18 @@ void LayeredConfiguration::addWriteable(AbstractConfiguration* pConfig, int prio
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)
@ -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
{
for (ConfigList::const_iterator it = _configs.begin(); it != _configs.end(); ++it)

View File

@ -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
{
LayeredConfiguration* pLC = new LayeredConfiguration;
@ -230,6 +247,7 @@ CppUnit::Test* LayeredConfigurationTest::suite()
CppUnit_addTest(pSuite, LayeredConfigurationTest, testTwoLayers);
CppUnit_addTest(pSuite, LayeredConfigurationTest, testThreeLayers);
CppUnit_addTest(pSuite, LayeredConfigurationTest, testRemove);
CppUnit_addTest(pSuite, LayeredConfigurationTest, testFind);
return pSuite;
}

View File

@ -31,6 +31,7 @@ public:
void testTwoLayers();
void testThreeLayers();
void testRemove();
void testFind();
void setUp();
void tearDown();