2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
21 * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
22 * @brief Configuration management functions
25 #ifndef CONFIG_MANAGER_HPP
26 #define CONFIG_MANAGER_HPP
28 #include "config/to-json-visitor.hpp"
29 #include "config/to-kvstore-visitor.hpp"
30 #include "config/from-json-visitor.hpp"
31 #include "config/from-kvstore-visitor.hpp"
32 #include "config/is-visitable.hpp"
33 #include "config/fs-utils.hpp"
39 * Fills the configuration with data stored in the json string
41 * @param jsonString configuration in a json format
42 * @param config visitable structure to fill
44 template <class Config>
45 void loadFromString(const std::string& jsonString, Config& config)
47 static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
49 FromJsonVisitor visitor(jsonString);
50 config.accept(visitor);
55 * Creates a string representation of the configuration in json format
57 * @param config visitable structure to convert
59 template <class Config>
60 std::string saveToString(const Config& config)
62 static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
64 ToJsonVisitor visitor;
65 config.accept(visitor);
66 return visitor.toString();
70 * Loads the config from a json file
72 * @param filename path to the file
73 * @param config visitable structure to load
75 template <class Config>
76 void loadFromFile(const std::string& filename, Config& config)
79 if (!fsutils::readFileContent(filename, content)) {
80 throw ConfigException("Could not load " + filename);
83 loadFromString(content, config);
84 } catch (ConfigException& e) {
85 throw ConfigException("Error in " + filename + ": " + e.what());
90 * Saves the config in a json file
92 * @param filename path to the file
93 * @param config visitable structure to save
95 template <class Config>
96 void saveToFile(const std::string& filename, const Config& config)
98 const std::string content = saveToString(config);
99 if (!fsutils::saveFileContent(filename, content)) {
100 throw ConfigException("Could not save " + filename);
105 * Loads a visitable configuration from KVStore.
107 * @param filename path to the KVStore db
108 * @param config visitable structure to load
109 * @param configName name of the configuration inside the KVStore db
111 template <class Config>
112 void loadFromKVStore(const std::string& filename, Config& config, const std::string& configName = "")
114 static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
116 FromKVStoreVisitor visitor(filename, configName);
117 config.accept(visitor);
121 * Saves the config to a KVStore.
123 * @param filename path to the KVStore db
124 * @param config visitable structure to load
125 * @param configName name of the config inside the KVStore db
127 template <class Config>
128 void saveToKVStore(const std::string& filename, const Config& config, const std::string& configName = "")
130 static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
132 ToKVStoreVisitor visitor(filename, configName);
133 config.accept(visitor);
136 } // namespace config
138 #endif // CONFIG_MANAGER_HPP