From f181b5f2d77b6981c0c05f88af63f1bf99535749 Mon Sep 17 00:00:00 2001 From: Piotr Bartosiewicz Date: Fri, 16 Jan 2015 14:50:09 +0100 Subject: [PATCH] Config manager api refinements [Bug/Feature] Always use prefix in db. Function renames. [Cause] N/A [Solution] N/A [Verification] Build, run tests Change-Id: Ia32747c15c904b3c67b00aeb7a6bf051c9591de9 --- src/config/from-kvjson-visitor.hpp | 5 +++-- src/config/from-kvstore-visitor.hpp | 18 +++++++----------- src/config/manager.hpp | 36 +++++++++++++++++++++--------------- src/config/to-kvstore-visitor.hpp | 18 +++++++----------- 4 files changed, 38 insertions(+), 39 deletions(-) diff --git a/src/config/from-kvjson-visitor.hpp b/src/config/from-kvjson-visitor.hpp index bbae07e..d2c1060 100644 --- a/src/config/from-kvjson-visitor.hpp +++ b/src/config/from-kvjson-visitor.hpp @@ -33,8 +33,9 @@ namespace config { class FromKVJsonVisitor { public: - FromKVJsonVisitor(const std::string& db, const std::string& json) : - mStorePtr(new KVStore(db)) + FromKVJsonVisitor(const std::string& db, const std::string& json, const std::string& prefix) + : mStorePtr(new KVStore(db)) + , mKeyPrefix(prefix) { mObject = json_tokener_parse(json.c_str()); if (mObject == nullptr) { diff --git a/src/config/from-kvstore-visitor.hpp b/src/config/from-kvstore-visitor.hpp index c7e3540..3981bfd 100644 --- a/src/config/from-kvstore-visitor.hpp +++ b/src/config/from-kvstore-visitor.hpp @@ -43,17 +43,6 @@ public: { } - FromKVStoreVisitor(const FromKVStoreVisitor& visitor, const std::string& prefix) - : mStorePtr(visitor.mStorePtr), - mKeyPrefix(prefix), - mTransaction(visitor.mTransaction) - { - } - - ~FromKVStoreVisitor() - { - } - FromKVStoreVisitor& operator=(const FromKVStoreVisitor&) = delete; template @@ -67,6 +56,13 @@ private: std::string mKeyPrefix; KVStore::Transaction mTransaction; + FromKVStoreVisitor(const FromKVStoreVisitor& visitor, const std::string& prefix) + : mStorePtr(visitor.mStorePtr), + mKeyPrefix(prefix), + mTransaction(visitor.mTransaction) + { + } + template::value, int>::type = 0> void getInternal(const std::string& name, T& value) { diff --git a/src/config/manager.hpp b/src/config/manager.hpp index eb7c740..0785094 100644 --- a/src/config/manager.hpp +++ b/src/config/manager.hpp @@ -45,7 +45,7 @@ namespace config { * @param config visitable structure to fill */ template -void loadFromString(const std::string& jsonString, Config& config) +void loadFromJsonString(const std::string& jsonString, Config& config) { static_assert(isVisitable::value, "Use CONFIG_REGISTER macro"); @@ -53,14 +53,13 @@ void loadFromString(const std::string& jsonString, Config& config) config.accept(visitor); } - /** * Creates a string representation of the configuration in json format * * @param config visitable structure to convert */ template -std::string saveToString(const Config& config) +std::string saveToJsonString(const Config& config) { static_assert(isVisitable::value, "Use CONFIG_REGISTER macro"); @@ -76,14 +75,14 @@ std::string saveToString(const Config& config) * @param config visitable structure to load */ template -void loadFromFile(const std::string& filename, Config& config) +void loadFromJsonFile(const std::string& filename, Config& config) { std::string content; if (!fsutils::readFileContent(filename, content)) { throw ConfigException("Could not load " + filename); } try { - loadFromString(content, config); + loadFromJsonString(content, config); } catch (ConfigException& e) { throw ConfigException("Error in " + filename + ": " + e.what()); } @@ -96,9 +95,9 @@ void loadFromFile(const std::string& filename, Config& config) * @param config visitable structure to save */ template -void saveToFile(const std::string& filename, const Config& config) +void saveToJsonFile(const std::string& filename, const Config& config) { - const std::string content = saveToString(config); + const std::string content = saveToJsonString(config); if (!fsutils::saveFileContent(filename, content)) { throw ConfigException("Could not save " + filename); } @@ -112,7 +111,7 @@ void saveToFile(const std::string& filename, const Config& config) * @param configName name of the configuration inside the KVStore db */ template -void loadFromKVStore(const std::string& filename, Config& config, const std::string& configName = "") +void loadFromKVStore(const std::string& filename, Config& config, const std::string& configName) { static_assert(isVisitable::value, "Use CONFIG_REGISTER macro"); @@ -128,7 +127,7 @@ void loadFromKVStore(const std::string& filename, Config& config, const std::str * @param configName name of the config inside the KVStore db */ template -void saveToKVStore(const std::string& filename, const Config& config, const std::string& configName = "") +void saveToKVStore(const std::string& filename, const Config& config, const std::string& configName) { static_assert(isVisitable::value, "Use CONFIG_REGISTER macro"); @@ -142,33 +141,40 @@ void saveToKVStore(const std::string& filename, const Config& config, const std: * @param kvfile path to the KVStore db * @param jsonfile path to json file with defaults * @param config visitable structure to save + * @param kvConfigName name of the config inside the KVStore db */ template -void loadFromKVStoreWithJson(const std::string& kvfile, const std::string& json, Config& config) +void loadFromKVStoreWithJson(const std::string& kvfile, + const std::string& json, + Config& config, + const std::string& kvConfigName) { static_assert(isVisitable::value, "Use CONFIG_REGISTER macro"); - FromKVJsonVisitor visitor(kvfile, json); + FromKVJsonVisitor visitor(kvfile, json, kvConfigName); config.accept(visitor); } + /** * Load the config from KVStore with defaults given in json file * * @param kvfile path to the KVStore db * @param jsonfile path to json file with defaults * @param config visitable structure to save + * @param kvConfigName name of the config inside the KVStore db */ template -void loadFromKVStoreWithJsonFile(const std::string& kvfile, const std::string& jsonfile, Config& config) +void loadFromKVStoreWithJsonFile(const std::string& kvfile, + const std::string& jsonfile, + Config& config, + const std::string& kvConfigName) { - static_assert(isVisitable::value, "Use CONFIG_REGISTER macro"); - std::string content; if (!fsutils::readFileContent(jsonfile, content)) { throw ConfigException("Could not load " + jsonfile); } try { - loadFromKVStoreWithJson(kvfile, content, config); + loadFromKVStoreWithJson(kvfile, content, config, kvConfigName); } catch (ConfigException& e) { throw ConfigException("Error in " + jsonfile + ": " + e.what()); } diff --git a/src/config/to-kvstore-visitor.hpp b/src/config/to-kvstore-visitor.hpp index 37b70a9..071dece 100644 --- a/src/config/to-kvstore-visitor.hpp +++ b/src/config/to-kvstore-visitor.hpp @@ -43,17 +43,6 @@ public: { } - ToKVStoreVisitor(const ToKVStoreVisitor& visitor, const std::string& prefix) - : mStorePtr(visitor.mStorePtr), - mKeyPrefix(prefix), - mTransaction(visitor.mTransaction) - { - } - - ~ToKVStoreVisitor() - { - } - ToKVStoreVisitor& operator=(const ToKVStoreVisitor&) = delete; template @@ -67,6 +56,13 @@ private: std::string mKeyPrefix; KVStore::Transaction mTransaction; + ToKVStoreVisitor(const ToKVStoreVisitor& visitor, const std::string& prefix) + : mStorePtr(visitor.mStorePtr), + mKeyPrefix(prefix), + mTransaction(visitor.mTransaction) + { + } + template::value, int>::type = 0> void setInternal(const std::string& name, const T& value) { -- 2.7.4