From 8d65ebf02a80e1c708c59fd58b313e8a2572dbef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnter=20Obiltschnig?= Date: Fri, 1 Sep 2017 20:23:35 +0200 Subject: [PATCH] Poco::Util::LayeredConfiguration: added support for labelling configurations and finding them by their label --- Util/include/Poco/Util/LayeredConfiguration.h | 48 ++++++++++++++++--- Util/src/LayeredConfiguration.cpp | 44 +++++++++++++++++ .../src/LayeredConfigurationTest.cpp | 18 +++++++ Util/testsuite/src/LayeredConfigurationTest.h | 1 + 4 files changed, 105 insertions(+), 6 deletions(-) diff --git a/Util/include/Poco/Util/LayeredConfiguration.h b/Util/include/Poco/Util/LayeredConfiguration.h index 8c258ed95..fbf80cb1b 100644 --- a/Util/include/Poco/Util/LayeredConfiguration.h +++ b/Util/include/Poco/Util/LayeredConfiguration.h @@ -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 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 @@ -93,6 +125,11 @@ public: /// If shared is false, the LayeredConfiguration takes ownership /// 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); @@ -113,14 +150,13 @@ public: /// Does nothing if the given configuration is not part of the /// LayeredConfiguration. -protected: - typedef Poco::AutoPtr ConfigPtr; - +protected: struct ConfigItem { - ConfigPtr pConfig; - int priority; - bool writeable; + ConfigPtr pConfig; + int priority; + bool writeable; + std::string label; }; bool getRaw(const std::string& key, std::string& value) const; diff --git a/Util/src/LayeredConfiguration.cpp b/Util/src/LayeredConfiguration.cpp index 14bbeea1e..f897ed8dd 100644 --- a/Util/src/LayeredConfiguration.cpp +++ b/Util/src/LayeredConfiguration.cpp @@ -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) diff --git a/Util/testsuite/src/LayeredConfigurationTest.cpp b/Util/testsuite/src/LayeredConfigurationTest.cpp index 693cdcc01..32616caa2 100644 --- a/Util/testsuite/src/LayeredConfigurationTest.cpp +++ b/Util/testsuite/src/LayeredConfigurationTest.cpp @@ -197,6 +197,23 @@ void LayeredConfigurationTest::testRemove() } +void LayeredConfigurationTest::testFind() +{ + AutoPtr pLC = new LayeredConfiguration; + AutoPtr pMC1 = new MapConfiguration; + AutoPtr pMC2 = new MapConfiguration; + + pLC->add(pMC1, 0); + pLC->add(pMC2, "label", -1); + + AutoPtr 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; } diff --git a/Util/testsuite/src/LayeredConfigurationTest.h b/Util/testsuite/src/LayeredConfigurationTest.h index 72f58ca80..ec6eadde5 100644 --- a/Util/testsuite/src/LayeredConfigurationTest.h +++ b/Util/testsuite/src/LayeredConfigurationTest.h @@ -31,6 +31,7 @@ public: void testTwoLayers(); void testThreeLayers(); void testRemove(); + void testFind(); void setUp(); void tearDown();