galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
Config.hpp
Go to the documentation of this file.
1
7
8#ifndef GALAXY_CORE_CONFIG_HPP_
9#define GALAXY_CORE_CONFIG_HPP_
10
11#include <nlohmann/json.hpp>
12
16
17namespace galaxy
18{
22 class Config final
23 {
24 public:
28 Config() noexcept;
29
37 Config(std::string_view file) noexcept;
38
42 ~Config() noexcept;
43
51 void load(std::string_view file);
52
56 void save();
57
68 template<meta::standard_type Value>
69 void set(const std::string& key, const Value& value) noexcept;
70
83 template<meta::standard_type Value>
84 void set(const std::string& key, const Value& value, const std::string& section, const std::string& delim = ".");
85
94 template<meta::standard_type Value>
95 void restore(const std::string& key, const Value& value) noexcept;
96
107 template<meta::standard_type Value>
108 void restore(const std::string& key, const Value& value, const std::string& section, const std::string& delim = ".");
109
117 [[nodiscard]]
118 bool has(const std::string& key) noexcept;
119
129 [[nodiscard]]
130 bool has(const std::string& key, const std::string& section, const std::string& delim = ".");
131
141 template<meta::standard_type Value>
142 [[nodiscard]]
143 std::optional<Value> get(const std::string& key);
144
156 template<meta::standard_type Value>
157 [[nodiscard]]
158 std::optional<Value> get(const std::string& key, const std::string& section, const std::string& delim = ".");
159
165 [[nodiscard]]
166 bool empty() const;
167
173 void raw(const nlohmann::json& json) noexcept;
174
180 [[nodiscard]]
181 const nlohmann::json& raw() const noexcept;
182
183 private:
187 Config(const Config&) = delete;
188
192 Config(Config&&) = delete;
193
197 Config& operator=(const Config&) = delete;
198
202 Config& operator=(Config&&) = delete;
203
204 private:
208 nlohmann::json m_config;
209
213 std::string m_path;
214 };
215
216 template<meta::standard_type Value>
217 inline void Config::set(const std::string& key, const Value& value) noexcept
218 {
219 m_config[key] = value;
220 }
221
222 template<meta::standard_type Value>
223 inline void Config::set(const std::string& key, const Value& value, const std::string& section, const std::string& delim)
224 {
225 const auto sections = str::split(section, delim);
226 if (sections.empty())
227 {
228 // Single section.
229 m_config[section][key] = value;
230 }
231 else
232 {
233 // Multiple sections.
234 nlohmann::json* leaf = &m_config;
235 for (const auto& sec : sections)
236 {
237 if (!leaf->contains(sec))
238 {
239 (*leaf)[sec] = nlohmann::json::object();
240 }
241
242 leaf = &leaf->at(sec);
243 }
244
245 (*leaf)[key] = value;
246 }
247 }
248
249 template<meta::standard_type Value>
250 inline void Config::restore(const std::string& key, const Value& value) noexcept
251 {
252 if (!has(key))
253 {
254 set(key, value);
255 }
256 }
257
258 template<meta::standard_type Value>
259 inline void Config::restore(const std::string& key, const Value& value, const std::string& section, const std::string& delim)
260 {
261 if (!has(key, section, delim))
262 {
263 set(key, value, section, delim);
264 }
265 }
266
267 template<meta::standard_type Value>
268 inline std::optional<Value> Config::get(const std::string& key)
269 {
270 const auto& section = m_config;
271
272 if (section.contains(key))
273 {
274 return section[key].get<Value>();
275 }
276
277 return std::nullopt;
278 }
279
280 template<meta::standard_type Value>
281 inline std::optional<Value> Config::get(const std::string& key, const std::string& section, const std::string& delim)
282 {
283 const auto sections = str::split(section, delim);
284 if (sections.empty())
285 {
286 // Single section.
287 const auto& root = m_config;
288 if (root.contains(section))
289 {
290 const auto& obj = root[section];
291 if (obj.contains(key))
292 {
293 return obj[key].get<Value>();
294 }
295 }
296
297 return std::nullopt;
298 }
299 else
300 {
301 // Multiple sections.
302 nlohmann::json* leaf = &m_config;
303 for (const auto& sec : sections)
304 {
305 if (leaf->contains(sec))
306 {
307 leaf = &leaf->at(sec);
308 }
309 }
310
311 if (leaf->contains(key))
312 {
313 return (*leaf)[key].get<Value>();
314 }
315
316 return std::nullopt;
317 }
318 }
319} // namespace galaxy
320
321#endif
Allows you to read, write and manipulate JSON config files.
Definition Config.hpp:23
Config() noexcept
Constructor.
Definition Config.cpp:14
Config(Config &&)=delete
Move constructor.
void save()
Save the config file. Must be opened first.
Definition Config.cpp:59
void load(std::string_view file)
Loads config json from disk.
Definition Config.cpp:30
~Config() noexcept
Destructor.
Definition Config.cpp:25
std::optional< Value > get(const std::string &key)
Retrieve a root config value.
Definition Config.hpp:268
nlohmann::json m_config
Config file as JSON.
Definition Config.hpp:208
Config(const Config &)=delete
Copy constructor.
Config & operator=(Config &&)=delete
Move assignment operator.
void restore(const std::string &key, const Value &value) noexcept
Sets a new setting, only if key or value does not already exist.
Definition Config.hpp:250
bool empty() const
Is the config file blank.
Definition Config.cpp:106
std::string m_path
Filepath.
Definition Config.hpp:213
void set(const std::string &key, const Value &value) noexcept
Set a new key-value pair for the config.
Definition Config.hpp:217
const nlohmann::json & raw() const noexcept
Get raw json object.
Definition Config.cpp:116
bool has(const std::string &key) noexcept
Check if the config file actually has a value.
Definition Config.cpp:68
Config & operator=(const Config &)=delete
Copy assignment operator.
std::vector< std::string > split(std::string_view input, std::string_view delim) noexcept
Split a string based on a delimiter.
Timer.hpp galaxy.
Definition Timer.cpp:18