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 KVStore store(filename);
118 KVStore::Transaction transaction(store);
119 FromKVStoreVisitor visitor(store, configName);
120 config.accept(visitor);
121 transaction.commit();
125 * Saves the config to a KVStore.
127 * @param filename path to the KVStore db
128 * @param config visitable structure to save
129 * @param configName name of the config inside the KVStore db
131 template <class Config>
132 void saveToKVStore(const std::string& filename, const Config& config, const std::string& configName)
134 static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
136 KVStore store(filename);
137 KVStore::Transaction transaction(store);
138 ToKVStoreVisitor visitor(store, configName);
139 config.accept(visitor);
140 transaction.commit();
144 * Load the config from KVStore with defaults given in json
146 * @param kvfile path to the KVStore db
147 * @param jsonfile path to json file with defaults
148 * @param config visitable structure to save
149 * @param kvConfigName name of the config inside the KVStore db
151 template <class Config>
152 void loadFromKVStoreWithJson(const std::string& kvfile,
153 const std::string& json,
155 const std::string& kvConfigName)
157 static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
159 KVStore store(kvfile);
160 KVStore::Transaction transaction(store);
161 FromKVJsonVisitor visitor(store, json, kvConfigName);
162 config.accept(visitor);
163 transaction.commit();
167 * Load the config from KVStore with defaults given in json file
169 * @param kvfile path to the KVStore db
170 * @param jsonfile path to json file with defaults
171 * @param config visitable structure to save
172 * @param kvConfigName name of the config inside the KVStore db
174 template <class Config>
175 void loadFromKVStoreWithJsonFile(const std::string& kvfile,
176 const std::string& jsonfile,
178 const std::string& kvConfigName)
181 if (!fsutils::readFileContent(jsonfile, content)) {
182 throw ConfigException("Could not load " + jsonfile);
185 loadFromKVStoreWithJson(kvfile, content, config, kvConfigName);
186 } catch (ConfigException& e) {
187 throw ConfigException("Error in " + jsonfile + ": " + e.what());
192 * Load binary data from a file/socket/pipe represented by the fd
194 * @param fd file descriptor
195 * @param config visitable structure to load
197 template <class Config>
198 void loadFromFD(const int fd, Config& config)
200 static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
202 FromFDStoreVisitor visitor(fd);
203 config.accept(visitor);
207 * Save binary data to a file/socket/pipe represented by the fd
209 * @param fd file descriptor
210 * @param config visitable structure to save
212 template <class Config>
213 void saveToFD(const int fd, const Config& config)
215 static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
217 ToFDStoreVisitor visitor(fd);
218 config.accept(visitor);
221 } // namespace config
223 #endif // CONFIG_MANAGER_HPP