+/*
+ * 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