Saving and loading configurations from KVStore
[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/from-json-visitor.hpp"
31 #include "config/from-kvstore-visitor.hpp"
32 #include "config/is-visitable.hpp"
33 #include "config/fs-utils.hpp"
34
35
36 namespace config {
37
38 /**
39  * Fills the configuration with data stored in the json string
40  *
41  * @param jsonString configuration in a json format
42  * @param config     visitable structure to fill
43  */
44 template <class Config>
45 void loadFromString(const std::string& jsonString, Config& config)
46 {
47     static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
48
49     FromJsonVisitor visitor(jsonString);
50     config.accept(visitor);
51 }
52
53
54 /**
55  * Creates a string representation of the configuration in json format
56  *
57  * @param config   visitable structure to convert
58  */
59 template <class Config>
60 std::string saveToString(const Config& config)
61 {
62     static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
63
64     ToJsonVisitor visitor;
65     config.accept(visitor);
66     return visitor.toString();
67 }
68
69 /**
70  * Loads the config from a json file
71  *
72  * @param filename path to the file
73  * @param config   visitable structure to load
74  */
75 template <class Config>
76 void loadFromFile(const std::string& filename, Config& config)
77 {
78     std::string content;
79     if (!fsutils::readFileContent(filename, content)) {
80         throw ConfigException("Could not load " + filename);
81     }
82     try {
83         loadFromString(content, config);
84     } catch (ConfigException& e) {
85         throw ConfigException("Error in " + filename + ": " + e.what());
86     }
87 }
88
89 /**
90  * Saves the config in a json file
91  *
92  * @param filename path to the file
93  * @param config   visitable structure to save
94  */
95 template <class Config>
96 void saveToFile(const std::string& filename, const Config& config)
97 {
98     const std::string content = saveToString(config);
99     if (!fsutils::saveFileContent(filename, content)) {
100         throw ConfigException("Could not save " + filename);
101     }
102 }
103
104 /**
105  * Loads a visitable configuration from KVStore.
106  *
107  * @param filename   path to the KVStore db
108  * @param config     visitable structure to load
109  * @param configName name of the configuration inside the KVStore db
110  */
111 template <class Config>
112 void loadFromKVStore(const std::string& filename, Config& config, const std::string& configName = "")
113 {
114     static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
115
116     FromKVStoreVisitor visitor(filename, configName);
117     config.accept(visitor);
118 }
119
120 /**
121  * Saves the config to a KVStore.
122  *
123  * @param filename   path to the KVStore db
124  * @param config     visitable structure to load
125  * @param configName name of the config inside the KVStore db
126  */
127 template <class Config>
128 void saveToKVStore(const std::string& filename, const Config& config, const std::string& configName = "")
129 {
130     static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
131
132     ToKVStoreVisitor visitor(filename, configName);
133     config.accept(visitor);
134 }
135
136 } // namespace config
137
138 #endif // CONFIG_MANAGER_HPP