7afde485320f24e5fc7640b1d9334560c6cbf959
[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     KVStore store(filename);
118     KVStore::Transaction transaction(store);
119     FromKVStoreVisitor visitor(store, configName);
120     config.accept(visitor);
121     transaction.commit();
122 }
123
124 /**
125  * Saves the config to a KVStore.
126  *
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
130  */
131 template <class Config>
132 void saveToKVStore(const std::string& filename, const Config& config, const std::string& configName)
133 {
134     static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
135
136     KVStore store(filename);
137     KVStore::Transaction transaction(store);
138     ToKVStoreVisitor visitor(store, configName);
139     config.accept(visitor);
140     transaction.commit();
141 }
142
143 /**
144  * Load the config from KVStore with defaults given in json
145  *
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
150  */
151 template <class Config>
152 void loadFromKVStoreWithJson(const std::string& kvfile,
153                              const std::string& json,
154                              Config& config,
155                              const std::string& kvConfigName)
156 {
157     static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
158
159     KVStore store(kvfile);
160     KVStore::Transaction transaction(store);
161     FromKVJsonVisitor visitor(store, json, kvConfigName);
162     config.accept(visitor);
163     transaction.commit();
164 }
165
166 /**
167  * Load the config from KVStore with defaults given in json file
168  *
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
173  */
174 template <class Config>
175 void loadFromKVStoreWithJsonFile(const std::string& kvfile,
176                                  const std::string& jsonfile,
177                                  Config& config,
178                                  const std::string& kvConfigName)
179 {
180     std::string content;
181     if (!fsutils::readFileContent(jsonfile, content)) {
182         throw ConfigException("Could not load " + jsonfile);
183     }
184     try {
185         loadFromKVStoreWithJson(kvfile, content, config, kvConfigName);
186     } catch (ConfigException& e) {
187         throw ConfigException("Error in " + jsonfile + ": " + e.what());
188     }
189 }
190
191 /**
192  * Load binary data from a file/socket/pipe represented by the fd
193  *
194  * @param fd file descriptor
195  * @param config visitable structure to load
196  */
197 template <class Config>
198 void loadFromFD(const int fd, Config& config)
199 {
200     static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
201
202     FromFDStoreVisitor visitor(fd);
203     config.accept(visitor);
204 }
205
206 /**
207  * Save binary data to a file/socket/pipe represented by the fd
208  *
209  * @param fd file descriptor
210  * @param config visitable structure to save
211  */
212 template <class Config>
213 void saveToFD(const int fd, const Config& config)
214 {
215     static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
216
217     ToFDStoreVisitor visitor(fd);
218     config.accept(visitor);
219 }
220
221 } // namespace config
222
223 #endif // CONFIG_MANAGER_HPP