7c50087dc5364f780bf03089e213efb541238d52
[archive/platform/core/system/libConfig.git] / src / config / manager.hpp
1 /*
2  *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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
17  */
18
19 /**
20  * @file
21  * @author  Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
22  * @brief   Configuration management functions
23  */
24
25 #ifndef CONFIG_MANAGER_HPP
26 #define CONFIG_MANAGER_HPP
27
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"
36
37
38 namespace config {
39
40 /**
41  * Fills the configuration with data stored in the json string
42  *
43  * @param jsonString configuration in a json format
44  * @param config     visitable structure to fill
45  */
46 template <class Config>
47 void loadFromJsonString(const std::string& jsonString, Config& config)
48 {
49     static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
50
51     FromJsonVisitor visitor(jsonString);
52     config.accept(visitor);
53 }
54
55 /**
56  * Creates a string representation of the configuration in json format
57  *
58  * @param config   visitable structure to convert
59  */
60 template <class Config>
61 std::string saveToJsonString(const Config& config)
62 {
63     static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
64
65     ToJsonVisitor visitor;
66     config.accept(visitor);
67     return visitor.toString();
68 }
69
70 /**
71  * Loads the config from a json file
72  *
73  * @param filename path to the file
74  * @param config   visitable structure to load
75  */
76 template <class Config>
77 void loadFromJsonFile(const std::string& filename, Config& config)
78 {
79     std::string content;
80     if (!fsutils::readFileContent(filename, content)) {
81         throw ConfigException("Could not load " + filename);
82     }
83     try {
84         loadFromJsonString(content, config);
85     } catch (ConfigException& e) {
86         throw ConfigException("Error in " + filename + ": " + e.what());
87     }
88 }
89
90 /**
91  * Saves the config in a json file
92  *
93  * @param filename path to the file
94  * @param config   visitable structure to save
95  */
96 template <class Config>
97 void saveToJsonFile(const std::string& filename, const Config& config)
98 {
99     const std::string content = saveToJsonString(config);
100     if (!fsutils::saveFileContent(filename, content)) {
101         throw ConfigException("Could not save " + filename);
102     }
103 }
104
105 /**
106  * Loads a visitable configuration from KVStore.
107  *
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
111  */
112 template <class Config>
113 void loadFromKVStore(const std::string& filename, Config& config, const std::string& configName)
114 {
115     static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
116
117     FromKVStoreVisitor visitor(filename, configName);
118     config.accept(visitor);
119 }
120
121 /**
122  * Saves the config to a KVStore.
123  *
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
127  */
128 template <class Config>
129 void saveToKVStore(const std::string& filename, const Config& config, const std::string& configName)
130 {
131     static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
132
133     ToKVStoreVisitor visitor(filename, configName);
134     config.accept(visitor);
135 }
136
137 /**
138  * Load the config from KVStore with defaults given in json
139  *
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
144  */
145 template <class Config>
146 void loadFromKVStoreWithJson(const std::string& kvfile,
147                              const std::string& json,
148                              Config& config,
149                              const std::string& kvConfigName)
150 {
151     static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
152
153     FromKVJsonVisitor visitor(kvfile, json, kvConfigName);
154     config.accept(visitor);
155 }
156
157 /**
158  * Load the config from KVStore with defaults given in json file
159  *
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
164  */
165 template <class Config>
166 void loadFromKVStoreWithJsonFile(const std::string& kvfile,
167                                  const std::string& jsonfile,
168                                  Config& config,
169                                  const std::string& kvConfigName)
170 {
171     std::string content;
172     if (!fsutils::readFileContent(jsonfile, content)) {
173         throw ConfigException("Could not load " + jsonfile);
174     }
175     try {
176         loadFromKVStoreWithJson(kvfile, content, config, kvConfigName);
177     } catch (ConfigException& e) {
178         throw ConfigException("Error in " + jsonfile + ": " + e.what());
179     }
180 }
181
182 /**
183  * Load binary data from a file/socket/pipe represented by the fd
184  *
185  * @param fd file descriptor
186  * @param config visitable structure to load
187  */
188 template <class Config>
189 void loadFromFD(const int fd, Config& config)
190 {
191     static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
192
193     FromFDStoreVisitor visitor(fd);
194     config.accept(visitor);
195 }
196
197 /**
198  * Save binary data to a file/socket/pipe represented by the fd
199  *
200  * @param fd file descriptor
201  * @param config visitable structure to save
202  */
203 template <class Config>
204 void saveToFD(const int fd, const Config& config)
205 {
206     static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
207
208     ToFDStoreVisitor visitor(fd);
209     config.accept(visitor);
210 }
211
212 } // namespace config
213
214 #endif // CONFIG_MANAGER_HPP