Config manager api refinements 23/33923/1
authorPiotr Bartosiewicz <p.bartosiewi@partner.samsung.com>
Fri, 16 Jan 2015 13:50:09 +0000 (14:50 +0100)
committerPiotr Bartosiewicz <p.bartosiewi@partner.samsung.com>
Fri, 16 Jan 2015 13:54:42 +0000 (14:54 +0100)
[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
src/config/from-kvstore-visitor.hpp
src/config/manager.hpp
src/config/to-kvstore-visitor.hpp

index bbae07e..d2c1060 100644 (file)
@@ -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) {
index c7e3540..3981bfd 100644 (file)
@@ -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<typename T>
@@ -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<typename T, typename std::enable_if<!isVisitable<T>::value, int>::type = 0>
     void getInternal(const std::string& name, T& value)
     {
index eb7c740..0785094 100644 (file)
@@ -45,7 +45,7 @@ namespace config {
  * @param config     visitable structure to fill
  */
 template <class Config>
-void loadFromString(const std::string& jsonString, Config& config)
+void loadFromJsonString(const std::string& jsonString, Config& config)
 {
     static_assert(isVisitable<Config>::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 <class Config>
-std::string saveToString(const Config& config)
+std::string saveToJsonString(const Config& config)
 {
     static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
 
@@ -76,14 +75,14 @@ std::string saveToString(const Config& config)
  * @param config   visitable structure to load
  */
 template <class Config>
-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 <class Config>
-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 <class Config>
-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<Config>::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 <class Config>
-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<Config>::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 <class Config>
-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<Config>::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 <class Config>
-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<Config>::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());
     }
index 37b70a9..071dece 100644 (file)
@@ -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<typename T>
@@ -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<typename T, typename std::enable_if<!isVisitable<T>::value, int>::type = 0>
     void setInternal(const std::string& name, const T& value)
     {