h2-mod/src/client/component/config.hpp

37 lines
838 B
C++
Raw Normal View History

2023-02-16 17:01:53 +01:00
#pragma once
namespace config
{
2023-02-22 04:36:28 +01:00
typedef nlohmann::json::value_t field_type;
typedef nlohmann::json field_value;
2023-02-16 17:01:53 +01:00
nlohmann::json read_config();
void write_config(const nlohmann::json& json);
2023-02-22 04:36:28 +01:00
nlohmann::json validate_config_field(const std::string& key, const field_value& value);
2023-02-22 05:48:38 +01:00
nlohmann::json get_default_value(const std::string& key);
2023-02-22 04:36:28 +01:00
2023-02-16 17:01:53 +01: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-22 04:36:28 +01:00
const auto value = validate_config_field(key, cfg[key]);
return {value.get<T>()};
2023-02-16 17:01:53 +01:00
}
2023-02-22 05:48:38 +01:00
nlohmann::json get_raw(const std::string& key);
2023-02-16 17:01:53 +01:00
template <typename T>
void set(const std::string& key, const T& value)
{
auto cfg = read_config();
2023-02-22 04:36:28 +01:00
cfg[key] = validate_config_field(key, value);
2023-02-16 17:01:53 +01:00
write_config(cfg);
}
}