2023-02-16 11:01:53 -05:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
namespace config
|
|
|
|
{
|
2023-02-21 22:36:28 -05:00
|
|
|
typedef nlohmann::json::value_t field_type;
|
|
|
|
typedef nlohmann::json field_value;
|
|
|
|
|
2023-02-16 11:01:53 -05:00
|
|
|
nlohmann::json read_config();
|
|
|
|
void write_config(const nlohmann::json& json);
|
|
|
|
|
2023-02-21 22:36:28 -05:00
|
|
|
nlohmann::json validate_config_field(const std::string& key, const field_value& value);
|
2023-02-21 23:48:38 -05:00
|
|
|
nlohmann::json get_default_value(const std::string& key);
|
2023-02-21 22:36:28 -05:00
|
|
|
|
2023-02-16 11:01:53 -05:00
|
|
|
template <typename T>
|
|
|
|
std::optional<T> get(const std::string& key)
|
|
|
|
{
|
|
|
|
const auto cfg = read_config();
|
|
|
|
if (!cfg.is_object() || !cfg.contains(key))
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-02-21 22:36:28 -05:00
|
|
|
const auto value = validate_config_field(key, cfg[key]);
|
|
|
|
return {value.get<T>()};
|
2023-02-16 11:01:53 -05:00
|
|
|
}
|
|
|
|
|
2023-02-21 23:48:38 -05:00
|
|
|
nlohmann::json get_raw(const std::string& key);
|
|
|
|
|
2023-02-16 11:01:53 -05:00
|
|
|
template <typename T>
|
|
|
|
void set(const std::string& key, const T& value)
|
|
|
|
{
|
|
|
|
auto cfg = read_config();
|
2023-02-21 22:36:28 -05:00
|
|
|
cfg[key] = validate_config_field(key, value);
|
2023-02-16 11:01:53 -05:00
|
|
|
write_config(cfg);
|
|
|
|
}
|
|
|
|
}
|