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/to-fdstore-visitor.hpp"
31 #include "config/from-json-visitor.hpp"
32 #include "config/from-kvstore-visitor.hpp"
33 #include "config/from-fdstore-visitor.hpp"
34 #include "config/from-kvjson-visitor.hpp"
35 #include "config/fs-utils.hpp"
41 * Fills the configuration with data stored in the json string
43 * @param jsonString configuration in a json format
44 * @param config visitable structure to fill
46 template <class Config>
47 void loadFromJsonString(const std::string& jsonString, Config& config)
49 static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
51 FromJsonVisitor visitor(jsonString);
52 config.accept(visitor);
56 * Creates a string representation of the configuration in json format
58 * @param config visitable structure to convert
60 template <class Config>
61 std::string saveToJsonString(const Config& config)
63 static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
65 ToJsonVisitor visitor;
66 config.accept(visitor);
67 return visitor.toString();
71 * Loads the config from a json file
73 * @param filename path to the file
74 * @param config visitable structure to load
76 template <class Config>
77 void loadFromJsonFile(const std::string& filename, Config& config)
80 if (!fsutils::readFileContent(filename, content)) {
81 throw ConfigException("Could not load " + filename);
84 loadFromJsonString(content, config);
85 } catch (ConfigException& e) {
86 throw ConfigException("Error in " + filename + ": " + e.what());
91 * Saves the config in a json file
93 * @param filename path to the file
94 * @param config visitable structure to save
96 template <class Config>
97 void saveToJsonFile(const std::string& filename, const Config& config)
99 const std::string content = saveToJsonString(config);
100 if (!fsutils::saveFileContent(filename, content)) {
101 throw ConfigException("Could not save " + filename);
106 * Loads a visitable configuration from KVStore.
108 * @param filename path to the KVStore db
109 * @param config visitable structure to load
110 * @param configName name of the configuration inside the KVStore db
112 template <class Config>
113 void loadFromKVStore(const std::string& filename, Config& config, const std::string& configName)
115 static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
117 FromKVStoreVisitor visitor(filename, configName);
118 config.accept(visitor);
122 * Saves the config to a KVStore.
124 * @param filename path to the KVStore db
125 * @param config visitable structure to save
126 * @param configName name of the config inside the KVStore db
128 template <class Config>
129 void saveToKVStore(const std::string& filename, const Config& config, const std::string& configName)
131 static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
133 ToKVStoreVisitor visitor(filename, configName);
134 config.accept(visitor);
138 * Load the config from KVStore with defaults given in json
140 * @param kvfile path to the KVStore db
141 * @param jsonfile path to json file with defaults
142 * @param config visitable structure to save
143 * @param kvConfigName name of the config inside the KVStore db
145 template <class Config>
146 void loadFromKVStoreWithJson(const std::string& kvfile,
147 const std::string& json,
149 const std::string& kvConfigName)
151 static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
153 FromKVJsonVisitor visitor(kvfile, json, kvConfigName);
154 config.accept(visitor);
158 * Load the config from KVStore with defaults given in json file
160 * @param kvfile path to the KVStore db
161 * @param jsonfile path to json file with defaults
162 * @param config visitable structure to save
163 * @param kvConfigName name of the config inside the KVStore db
165 template <class Config>
166 void loadFromKVStoreWithJsonFile(const std::string& kvfile,
167 const std::string& jsonfile,
169 const std::string& kvConfigName)
172 if (!fsutils::readFileContent(jsonfile, content)) {
173 throw ConfigException("Could not load " + jsonfile);
176 loadFromKVStoreWithJson(kvfile, content, config, kvConfigName);
177 } catch (ConfigException& e) {
178 throw ConfigException("Error in " + jsonfile + ": " + e.what());
183 * Load binary data from a file/socket/pipe represented by the fd
185 * @param fd file descriptor
186 * @param config visitable structure to load
188 template <class Config>
189 void loadFromFD(const int fd, Config& config)
191 static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
193 FromFDStoreVisitor visitor(fd);
194 config.accept(visitor);
198 * Save binary data to a file/socket/pipe represented by the fd
200 * @param fd file descriptor
201 * @param config visitable structure to save
203 template <class Config>
204 void saveToFD(const int fd, const Config& config)
206 static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
208 ToFDStoreVisitor visitor(fd);
209 config.accept(visitor);
212 } // namespace config
214 #endif // CONFIG_MANAGER_HPP