8#ifndef GALAXY_CORE_CONFIG_HPP_
9#define GALAXY_CORE_CONFIG_HPP_
11#include <nlohmann/json.hpp>
39 Config(std::string_view file)
noexcept;
53 void load(std::string_view file);
70 template<meta::standard_type Value>
71 void set(
const std::string& key,
const Value& value)
noexcept;
85 template<meta::standard_type Value>
86 void set(
const std::string& key,
const Value& value,
const std::string& section,
const std::string& delim =
".");
96 template<meta::standard_type Value>
97 void restore(
const std::string& key,
const Value& value)
noexcept;
109 template<meta::standard_type Value>
110 void restore(
const std::string& key,
const Value& value,
const std::string& section,
const std::string& delim =
".");
120 bool has(
const std::string& key)
noexcept;
132 bool has(
const std::string& key,
const std::string& section,
const std::string& delim =
".");
143 template<meta::standard_type Value>
145 Value
get(
const std::string& key);
158 template<meta::standard_type Value>
160 Value
get(
const std::string& key,
const std::string& section,
const std::string& delim =
".");
175 void raw(
const nlohmann::json& json)
noexcept;
183 const nlohmann::json&
raw()
const noexcept;
223 template<meta::standard_type Value>
224 inline void Config::set(
const std::string& key,
const Value& value)
noexcept
228 m_config[
"config"][key] = value;
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)
242 if (sections.empty())
245 m_config[
"config"][section][key] = value;
250 nlohmann::json* leaf = &
m_config[
"config"];
251 for (
const auto& sec : sections)
253 if (!leaf->contains(sec))
255 (*leaf)[sec] = nlohmann::json::object();
258 leaf = &leaf->at(sec);
261 (*leaf)[key] = value;
270 template<meta::standard_type Value>
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)
282 if (!
has(key, section, delim))
284 set(key, value, section, delim);
288 template<meta::standard_type Value>
293 const auto& section =
m_config[
"config"];
295 if (section.contains(key))
297 return section[key].get<Value>();
310 template<meta::standard_type Value>
311 inline Value
Config::get(
const std::string& key,
const std::string& section,
const std::string& delim)
316 if (sections.empty())
319 const auto& root =
m_config[
"config"];
320 if (root.contains(section))
322 const auto& obj = root[section];
323 if (obj.contains(key))
325 return obj[key].get<Value>();
335 nlohmann::json* leaf = &
m_config[
"config"];
336 for (
const auto& sec : sections)
338 if (leaf->contains(sec))
340 leaf = &leaf->at(sec);
344 if (leaf->contains(key))
346 return (*leaf)[key].get<Value>();
#define GALAXY_LOG(level, msg,...)
Allows you to read, write and manipulate JSON config files.
nlohmann::json m_config
Config file as JSON.
Value get(const std::string &key)
Retrieve a root config value.
void restore(const std::string &key, const Value &value) noexcept
Sets a new setting, only if key or value is missing.
const nlohmann::json & raw() const noexcept
Get raw json object.
Config(Config &&)=delete
Move constructor.
Config(const Config &)=delete
Copy constructor.
~Config() noexcept
Destructor.
bool empty() const
Is the config file blank.
void save()
Save the config file. Must be opened first.
bool has(const std::string &key) noexcept
Check if the config file actually has a value.
void load(std::string_view file)
Checks if config exists and flags if a config needs to be created.
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.
Config() noexcept
Constructor.
bool m_loaded
Config loaded flag.
Config & operator=(Config &&)=delete
Move assignment operator.
std::string m_path
Filepath.
std::vector< std::string > split(std::string_view input, std::string_view delim) noexcept
Split a string based on a delimiter.