Changed config to save the data type with the data so it can be loaded with the correct type.

This commit is contained in:
Patrick Wuttke
2026-02-17 10:32:31 +01:00
parent 707207a83d
commit 85f75d9814
2 changed files with 32 additions and 10 deletions

View File

@@ -7,6 +7,13 @@
#include <mijin/util/variant.hpp>
#include <yaml-cpp/yaml.h>
namespace
{
const std::string TAG_BOOL = "bool";
const std::string TAG_INT = "int";
const std::string TAG_DOUBLE = "double";
}
template<>
struct YAML::convert<raid::ConfigSection>
{
@@ -68,6 +75,15 @@ struct YAML::convert<raid::ConfigValue>
value.visit([&]<typename T>(const T& content) {
if constexpr (!std::is_same_v<T, std::nullptr_t>) {
node = content;
if constexpr (std::is_same_v<T, bool>) {
node.SetTag(TAG_BOOL);
}
else if constexpr (std::is_same_v<T, raid::config_int_t>) {
node.SetTag(TAG_INT);
}
else if constexpr (std::is_same_v<T, double>) {
node.SetTag(TAG_DOUBLE);
}
}
else {
node = {};
@@ -91,19 +107,23 @@ struct YAML::convert<raid::ConfigValue>
value = node.as<raid::ConfigSection>();
break;
case YAML::NodeType::Scalar:
try
{
value = node.as<raid::config_int_t>();
break;
{
const std::string& tag = node.Tag();
if (tag == TAG_BOOL) {
value = node.as<bool>();
}
catch(const YAML::Exception&) {} // NOLINT(bugprone-empty-catch)
try
{
else if (tag == TAG_INT) {
value = node.as<raid::config_int_t>();
}
else if (tag == TAG_DOUBLE) {
value = node.as<double>();
}
catch(const YAML::Exception&) {} // NOLINT(bugprone-empty-catch)
value = node.as<std::string>();
else {
value = node.as<std::string>();
}
break;
}
default:
return false;
}
@@ -302,7 +322,6 @@ void FileConfig::setValue(std::string_view path, ConfigValue value) noexcept
mDirty = true;
}
mijin::Result<> FileConfig::init(mijin::PathReference path)
{
mPath = std::move(path);

View File

@@ -175,6 +175,9 @@ public:
void setValue(std::string_view path, ConfigValue value) noexcept;
[[nodiscard]]
bool valueExists(std::string_view path) const noexcept;
[[nodiscard]]
mijin::Result<> init(mijin::PathReference path);