json_object_put(mObject);
}
+ FromJsonVisitor& operator=(const FromJsonVisitor&) = delete;
+
template<class T>
void visit(const std::string& name, T& value)
{
private:
json_object* mObject;
- FromJsonVisitor& operator=(const FromJsonVisitor&) = delete;
explicit FromJsonVisitor(json_object* object)
: mObject(json_object_get(object))
--- /dev/null
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Jan Olszak (j.olszak@samsung.com)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+/**
+ * @file
+ * @author Jan Olszak (j.olszak@samsung.com)
+ * @brief Visitor for loading from KVStore
+ */
+
+#ifndef CONFIG_FROM_KVSTORE_VISITOR_HPP
+#define CONFIG_FROM_KVSTORE_VISITOR_HPP
+
+#include "config/is-visitable.hpp"
+#include "config/exception.hpp"
+#include "config/kvstore.hpp"
+
+#include <string>
+#include <memory>
+
+namespace config {
+
+class FromKVStoreVisitor {
+public:
+ explicit FromKVStoreVisitor(const std::string& filePath, const std::string& prefix)
+ : mStorePtr(new KVStore(filePath)),
+ mKeyPrefix(prefix)
+ {
+ }
+
+ FromKVStoreVisitor(const FromKVStoreVisitor& visitor, const std::string& prefix)
+ : mStorePtr(visitor.mStorePtr),
+ mKeyPrefix(prefix)
+ {
+ }
+
+ ~FromKVStoreVisitor()
+ {
+ }
+
+ FromKVStoreVisitor& operator=(const FromKVStoreVisitor&) = delete;
+
+ template<class T>
+ void visit(const std::string& name, T& value)
+ {
+ getInternal(key(mKeyPrefix, name), value);
+ }
+
+private:
+ std::shared_ptr<KVStore> mStorePtr;
+ std::string mKeyPrefix;
+
+ template<class T, class std::enable_if<!isVisitable<T>::value, int>::type = 0>
+ void getInternal(const std::string& name, T& value)
+ {
+ value = mStorePtr->get<T>(name);
+ }
+
+ template<class T, class std::enable_if<isVisitable<T>::value, int>::type = 0>
+ void getInternal(const std::string& name, T& value)
+ {
+ FromKVStoreVisitor visitor(*this, name);
+ value.accept(visitor);
+ }
+};
+
+} // namespace config
+
+#endif // CONFIG_FROM_KVSTORE_VISITOR_HPP
KVStore::KVStore(const std::string& path)
- : mConn(path)
+ : mPath(path),
+ mConn(path)
{
setupDb();
prepareStatements();
}
+KVStore::KVStore(const KVStore& store)
+ : mPath(store.mPath),
+ mConn(mPath)
+{
+ prepareStatements();
+}
+
KVStore::~KVStore()
{
}
* @param path configuration database file path
*/
KVStore(const std::string& path);
+ KVStore(const KVStore& store);
~KVStore();
/**
std::mutex mConnMtx;
+ std::string mPath;
sqlite3::Connection mConn;
std::unique_ptr<sqlite3::Statement> mGetValueStmt;
std::unique_ptr<sqlite3::Statement> mGetValueCountStmt;
#define CONFIG_MANAGER_HPP
#include "config/to-json-visitor.hpp"
+#include "config/to-kvstore-visitor.hpp"
#include "config/from-json-visitor.hpp"
+#include "config/from-kvstore-visitor.hpp"
#include "config/is-visitable.hpp"
#include "config/fs-utils.hpp"
namespace config {
+/**
+ * Fills the configuration with data stored in the json string
+ *
+ * @param jsonString configuration in a json format
+ * @param config visitable structure to fill
+ */
template <class Config>
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)
{
return visitor.toString();
}
+/**
+ * Loads the config from a json file
+ *
+ * @param filename path to the file
+ * @param config visitable structure to load
+ */
template <class Config>
void loadFromFile(const std::string& filename, Config& config)
{
}
}
+/**
+ * Saves the config in a json file
+ *
+ * @param filename path to the file
+ * @param config visitable structure to save
+ */
template <class Config>
void saveToFile(const std::string& filename, const Config& config)
{
}
}
+/**
+ * Loads a visitable configuration from KVStore.
+ *
+ * @param filename path to the KVStore db
+ * @param config visitable structure to load
+ * @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 = "")
+{
+ static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
+
+ FromKVStoreVisitor visitor(filename, configName);
+ config.accept(visitor);
+}
+
+/**
+ * Saves the config to a KVStore.
+ *
+ * @param filename path to the KVStore db
+ * @param config visitable structure to load
+ * @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 = "")
+{
+ static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
+
+ ToKVStoreVisitor visitor(filename, configName);
+ config.accept(visitor);
+}
+
} // namespace config
#endif // CONFIG_MANAGER_HPP
json_object_put(mObject);
}
+ ToJsonVisitor& operator=(const ToJsonVisitor&) = delete;
+
std::string toString() const
{
return json_object_to_json_string(mObject);
private:
json_object* mObject;
- ToJsonVisitor& operator=(const ToJsonVisitor&) = delete;
json_object* detach()
{
--- /dev/null
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Jan Olszak (j.olszak@samsung.com)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+/**
+ * @file
+ * @author Jan Olszak (j.olszak@samsung.com)
+ * @brief Visitor for saving to KVStore
+ */
+
+#ifndef CONFIG_TO_KVSTORE_VISITOR_HPP
+#define CONFIG_TO_KVSTORE_VISITOR_HPP
+
+#include "config/is-visitable.hpp"
+#include "config/kvstore.hpp"
+
+#include <string>
+#include <memory>
+
+namespace config {
+
+class ToKVStoreVisitor {
+
+public:
+ ToKVStoreVisitor(const std::string& filePath, const std::string& prefix)
+ : mStorePtr(new KVStore(filePath)),
+ mKeyPrefix(prefix)
+ {
+ }
+
+ ToKVStoreVisitor(const ToKVStoreVisitor& visitor, const std::string& prefix)
+ : mStorePtr(visitor.mStorePtr),
+ mKeyPrefix(prefix)
+ {
+ }
+
+ ~ToKVStoreVisitor()
+ {
+ }
+
+ ToKVStoreVisitor& operator=(const ToKVStoreVisitor&) = delete;
+
+ template<class T>
+ void visit(const std::string& name, const T& value)
+ {
+ setInternal(key(mKeyPrefix, name), value);
+ }
+
+private:
+ std::shared_ptr<KVStore> mStorePtr;
+ std::string mKeyPrefix;
+
+
+ template<class T, class std::enable_if<!isVisitable<T>::value, int>::type = 0>
+ void setInternal(const std::string& name, const T& value)
+ {
+ mStorePtr->set(name, value);
+ }
+
+ template<class T, class std::enable_if<isVisitable<T>::value, int>::type = 0>
+ void setInternal(const std::string& name, const T& value)
+ {
+ ToKVStoreVisitor visitor(*this, name);
+ value.accept(visitor);
+ }
+};
+
+} // namespace config
+
+#endif // CONFIG_TO_KVSTORE_VISITOR_HPP