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/is-visitable.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 loadFromString(const std::string& jsonString, Config& config)
49 static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
51 FromJsonVisitor visitor(jsonString);
52 config.accept(visitor);
57 * Creates a string representation of the configuration in json format
59 * @param config visitable structure to convert
61 template <class Config>
62 std::string saveToString(const Config& config)
64 static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
66 ToJsonVisitor visitor;
67 config.accept(visitor);
68 return visitor.toString();
72 * Loads the config from a json file
74 * @param filename path to the file
75 * @param config visitable structure to load
77 template <class Config>
78 void loadFromFile(const std::string& filename, Config& config)
81 if (!fsutils::readFileContent(filename, content)) {
82 throw ConfigException("Could not load " + filename);
85 loadFromString(content, config);
86 } catch (ConfigException& e) {
87 throw ConfigException("Error in " + filename + ": " + e.what());
92 * Saves the config in a json file
94 * @param filename path to the file
95 * @param config visitable structure to save
97 template <class Config>
98 void saveToFile(const std::string& filename, const Config& config)
100 const std::string content = saveToString(config);
101 if (!fsutils::saveFileContent(filename, content)) {
102 throw ConfigException("Could not save " + filename);
107 * Loads a visitable configuration from KVStore.
109 * @param filename path to the KVStore db
110 * @param config visitable structure to load
111 * @param configName name of the configuration inside the KVStore db
113 template <class Config>
114 void loadFromKVStore(const std::string& filename, Config& config, const std::string& configName = "")
116 static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
118 FromKVStoreVisitor visitor(filename, configName);
119 config.accept(visitor);
123 * Saves the config to a KVStore.
125 * @param filename path to the KVStore db
126 * @param config visitable structure to save
127 * @param configName name of the config inside the KVStore db
129 template <class Config>
130 void saveToKVStore(const std::string& filename, const Config& config, const std::string& configName = "")
132 static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
134 ToKVStoreVisitor visitor(filename, configName);
135 config.accept(visitor);
139 * Load binary data from a file/socket/pipe represented by the fd
141 * @param fd file descriptor
142 * @param config visitable structure to load
144 template <class Config>
145 void loadFromFD(const int fd, Config& config)
147 static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
149 FromFDStoreVisitor visitor(fd);
150 config.accept(visitor);
154 * Save binary data to a file/socket/pipe represented by the fd
156 * @param fd file descriptor
157 * @param config visitable structure to save
159 template <class Config>
160 void saveToFD(const int fd, const Config& config)
162 static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
164 ToFDStoreVisitor visitor(fd);
165 config.accept(visitor);
168 } // namespace config
170 #endif // CONFIG_MANAGER_HPP