8#ifndef GALAXY_CORE_CONFIG_HPP_
9#define GALAXY_CORE_CONFIG_HPP_
11#include <nlohmann/json.hpp>
37 Config(std::string_view file)
noexcept;
51 void load(std::string_view file);
68 template<meta::standard_type Value>
69 void set(
const std::string& key,
const Value& value)
noexcept;
83 template<meta::standard_type Value>
84 void set(
const std::string& key,
const Value& value,
const std::string& section,
const std::string& delim =
".");
94 template<meta::standard_type Value>
95 void restore(
const std::string& key,
const Value& value)
noexcept;
107 template<meta::standard_type Value>
108 void restore(
const std::string& key,
const Value& value,
const std::string& section,
const std::string& delim =
".");
118 bool has(
const std::string& key)
noexcept;
130 bool has(
const std::string& key,
const std::string& section,
const std::string& delim =
".");
141 template<meta::standard_type Value>
143 std::optional<Value>
get(
const std::string& key);
156 template<meta::standard_type Value>
158 std::optional<Value>
get(
const std::string& key,
const std::string& section,
const std::string& delim =
".");
173 void raw(
const nlohmann::json& json)
noexcept;
181 const nlohmann::json&
raw()
const noexcept;
216 template<meta::standard_type Value>
217 inline void Config::set(
const std::string& key,
const Value& value)
noexcept
219 m_config[key] = value;
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)
225 const auto sections =
str::split(section, delim);
226 if (sections.empty())
235 for (
const auto& sec : sections)
237 if (!leaf->contains(sec))
239 (*leaf)[sec] = nlohmann::json::object();
242 leaf = &leaf->at(sec);
245 (*leaf)[key] = value;
249 template<meta::standard_type Value>
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)
261 if (!
has(key, section, delim))
263 set(key, value, section, delim);
267 template<meta::standard_type Value>
272 if (section.contains(key))
274 return section[key].get<Value>();
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)
283 const auto sections =
str::split(section, delim);
284 if (sections.empty())
288 if (root.contains(section))
290 const auto& obj = root[section];
291 if (obj.contains(key))
293 return obj[key].get<Value>();
303 for (
const auto& sec : sections)
305 if (leaf->contains(sec))
307 leaf = &leaf->at(sec);
311 if (leaf->contains(key))
313 return (*leaf)[key].get<Value>();
Allows you to read, write and manipulate JSON config files.
Config() noexcept
Constructor.
Config(Config &&)=delete
Move constructor.
void save()
Save the config file. Must be opened first.
void load(std::string_view file)
Loads config json from disk.
~Config() noexcept
Destructor.
std::optional< Value > get(const std::string &key)
Retrieve a root config value.
nlohmann::json m_config
Config file as JSON.
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.
bool empty() const
Is the config file blank.
std::string m_path
Filepath.
void set(const std::string &key, const Value &value) noexcept
Set a new key-value pair for the config.
const nlohmann::json & raw() const noexcept
Get raw json object.
bool has(const std::string &key) noexcept
Check if the config file actually has a value.
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.