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{
19 namespace core
20 {
24 class Config final
25 {
26 public:
30 Config() noexcept;
31
39 Config(std::string_view file) noexcept;
40
44 ~Config() noexcept;
45
53 void load(std::string_view file);
54
58 void save();
59
70 template<meta::standard_type Value>
71 void set(const std::string& key, const Value& value) noexcept;
72
85 template<meta::standard_type Value>
86 void set(const std::string& key, const Value& value, const std::string& section, const std::string& delim = ".");
87
96 template<meta::standard_type Value>
97 void restore(const std::string& key, const Value& value) noexcept;
98
109 template<meta::standard_type Value>
110 void restore(const std::string& key, const Value& value, const std::string& section, const std::string& delim = ".");
111
119 [[nodiscard]]
120 bool has(const std::string& key) noexcept;
121
131 [[nodiscard]]
132 bool has(const std::string& key, const std::string& section, const std::string& delim = ".");
133
143 template<meta::standard_type Value>
144 [[nodiscard]]
145 Value get(const std::string& key);
146
158 template<meta::standard_type Value>
159 [[nodiscard]]
160 Value get(const std::string& key, const std::string& section, const std::string& delim = ".");
161
167 [[nodiscard]]
168 bool empty() const;
169
175 void raw(const nlohmann::json& json) noexcept;
176
182 [[nodiscard]]
183 const nlohmann::json& raw() const noexcept;
184
185 private:
189 Config(const Config&) = delete;
190
194 Config(Config&&) = delete;
195
199 Config& operator=(const Config&) = delete;
200
204 Config& operator=(Config&&) = delete;
205
206 private:
211
215 nlohmann::json m_config;
216
220 std::string m_path;
221 };
222
223 template<meta::standard_type Value>
224 inline void Config::set(const std::string& key, const Value& value) noexcept
225 {
226 if (m_loaded)
227 {
228 m_config["config"][key] = value;
229 }
230 else
231 {
232 GALAXY_LOG(GALAXY_ERROR, "Attempted to set value of an unloaded config file.");
233 }
234 }
235
236 template<meta::standard_type Value>
237 inline void Config::set(const std::string& key, const Value& value, const std::string& section, const std::string& delim)
238 {
239 if (m_loaded)
240 {
241 const auto sections = strutils::split(section, delim);
242 if (sections.empty())
243 {
244 // Single section.
245 m_config["config"][section][key] = value;
246 }
247 else
248 {
249 // Multiple sections.
250 nlohmann::json* leaf = &m_config["config"];
251 for (const auto& sec : sections)
252 {
253 if (!leaf->contains(sec))
254 {
255 (*leaf)[sec] = nlohmann::json::object();
256 }
257
258 leaf = &leaf->at(sec);
259 }
260
261 (*leaf)[key] = value;
262 }
263 }
264 else
265 {
266 GALAXY_LOG(GALAXY_ERROR, "Attempted to set value of an unloaded config file.");
267 }
268 }
269
270 template<meta::standard_type Value>
271 inline void Config::restore(const std::string& key, const Value& value) noexcept
272 {
273 if (!has(key))
274 {
275 set(key, value);
276 }
277 }
278
279 template<meta::standard_type Value>
280 inline void Config::restore(const std::string& key, const Value& value, const std::string& section, const std::string& delim)
281 {
282 if (!has(key, section, delim))
283 {
284 set(key, value, section, delim);
285 }
286 }
287
288 template<meta::standard_type Value>
289 inline Value Config::get(const std::string& key)
290 {
291 if (m_loaded)
292 {
293 const auto& section = m_config["config"];
294
295 if (section.contains(key))
296 {
297 return section[key].get<Value>();
298 }
299 }
300 else
301 {
302 GALAXY_LOG(GALAXY_FATAL, "Attempted to get value of an unloaded config file.");
303 }
304
305 GALAXY_LOG(GALAXY_FATAL, "Failed to retrieve '{0}' from config.", key);
306
307 return Value {};
308 }
309
310 template<meta::standard_type Value>
311 inline Value Config::get(const std::string& key, const std::string& section, const std::string& delim)
312 {
313 if (m_loaded)
314 {
315 const auto sections = strutils::split(section, delim);
316 if (sections.empty())
317 {
318 // Single section.
319 const auto& root = m_config["config"];
320 if (root.contains(section))
321 {
322 const auto& obj = root[section];
323 if (obj.contains(key))
324 {
325 return obj[key].get<Value>();
326 }
327 }
328
329 GALAXY_LOG(GALAXY_FATAL, "Failed to retrieve '{0}' from config.", key);
330 return Value {};
331 }
332 else
333 {
334 // Multiple sections.
335 nlohmann::json* leaf = &m_config["config"];
336 for (const auto& sec : sections)
337 {
338 if (leaf->contains(sec))
339 {
340 leaf = &leaf->at(sec);
341 }
342 }
343
344 if (leaf->contains(key))
345 {
346 return (*leaf)[key].get<Value>();
347 }
348
349 GALAXY_LOG(GALAXY_FATAL, "Failed to retrieve '{0}' from config.", key);
350 return Value {};
351 }
352 }
353 else
354 {
355 GALAXY_LOG(GALAXY_FATAL, "Attempted to get value of an unloaded config file.");
356 }
357
358 GALAXY_LOG(GALAXY_FATAL, "Failed to retrieve '{0}' from config.", key);
359 return Value {};
360 }
361 } // namespace core
362} // namespace galaxy
363
364#endif
#define GALAXY_LOG(level, msg,...)
Definition Log.hpp:29
#define GALAXY_FATAL
Definition Log.hpp:26
#define GALAXY_ERROR
Definition Log.hpp:25
Allows you to read, write and manipulate JSON config files.
Definition Config.hpp:25
nlohmann::json m_config
Config file as JSON.
Definition Config.hpp:215
Value get(const std::string &key)
Retrieve a root config value.
Definition Config.hpp:289
void restore(const std::string &key, const Value &value) noexcept
Sets a new setting, only if key or value is missing.
Definition Config.hpp:271
const nlohmann::json & raw() const noexcept
Get raw json object.
Definition Config.cpp:160
Config(Config &&)=delete
Move constructor.
Config(const Config &)=delete
Copy constructor.
~Config() noexcept
Destructor.
Definition Config.cpp:30
bool empty() const
Is the config file blank.
Definition Config.cpp:144
void save()
Save the config file. Must be opened first.
Definition Config.cpp:75
bool has(const std::string &key) noexcept
Check if the config file actually has a value.
Definition Config.cpp:98
void load(std::string_view file)
Checks if config exists and flags if a config needs to be created.
Definition Config.cpp:36
Config & operator=(const Config &)=delete
Copy assignment operator.
void set(const std::string &key, const Value &value) noexcept
Set a new key-value pair for the config.
Definition Config.hpp:224
Config() noexcept
Constructor.
Definition Config.cpp:16
bool m_loaded
Config loaded flag.
Definition Config.hpp:210
Config & operator=(Config &&)=delete
Move assignment operator.
std::string m_path
Filepath.
Definition Config.hpp:220
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 Async.hpp:17