small improvements to config type
- made set() and setValue() functions return the actually stored value - added getOrAddValue() and getOrComputeValue() helpers - added min and max value as parameters to asInt()
This commit is contained in:
@@ -170,9 +170,9 @@ ConfigValue& ConfigSection::getOrAdd(std::string_view key)
|
||||
return mValues[key];
|
||||
}
|
||||
|
||||
void ConfigSection::set(std::string_view key, ConfigValue value)
|
||||
ConfigValue& ConfigSection::set(std::string_view key, ConfigValue value)
|
||||
{
|
||||
mValues[key] = std::move(value);
|
||||
return (mValues[key] = std::move(value));
|
||||
}
|
||||
|
||||
bool ConfigValue::asBool() const noexcept
|
||||
@@ -188,9 +188,9 @@ bool ConfigValue::asBool() const noexcept
|
||||
});
|
||||
}
|
||||
|
||||
config_int_t ConfigValue::asInt() const noexcept
|
||||
config_int_t ConfigValue::asInt(config_int_t minValue, config_int_t maxValue) const noexcept
|
||||
{
|
||||
return visit(mijin::Visitor{
|
||||
const config_int_t value = visit(mijin::Visitor{
|
||||
[](std::nullptr_t) { return config_int_t(0); },
|
||||
[](bool boolValue) { return config_int_t(boolValue); },
|
||||
[](config_int_t intValue) { return intValue; },
|
||||
@@ -205,6 +205,7 @@ config_int_t ConfigValue::asInt() const noexcept
|
||||
[](const ConfigArray&) { return config_int_t(0); },
|
||||
[](const ConfigSection&) { return config_int_t(0); }
|
||||
});
|
||||
return std::clamp(value, minValue, maxValue);
|
||||
}
|
||||
|
||||
double ConfigValue::asDouble() const noexcept
|
||||
@@ -293,7 +294,25 @@ const ConfigValue& FileConfig::getValue(std::string_view path) const noexcept
|
||||
return (*section)[path];
|
||||
}
|
||||
|
||||
void FileConfig::setValue(std::string_view path, ConfigValue value) noexcept
|
||||
const ConfigValue& FileConfig::getOrAddValue(std::string_view path, ConfigValue defaultValue)
|
||||
{
|
||||
const ConfigValue& value = getValue(path);
|
||||
if (value.isUndefined()) {
|
||||
return setValue(path, std::move(defaultValue));
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
const ConfigValue& FileConfig::getOrComputeValue(std::string_view path, std::function<ConfigValue()> computeFunc)
|
||||
{
|
||||
const ConfigValue& value = getValue(path);
|
||||
if (value.isUndefined()) {
|
||||
return setValue(path, computeFunc());
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
const ConfigValue& FileConfig::setValue(std::string_view path, ConfigValue value) noexcept
|
||||
{
|
||||
ConfigSection* section = &mRoot;
|
||||
while (true)
|
||||
@@ -312,14 +331,14 @@ void FileConfig::setValue(std::string_view path, ConfigValue value) noexcept
|
||||
else if (!existing.isSection())
|
||||
{
|
||||
MIJIN_ERROR("Value already exists, but is not a section.");
|
||||
return;
|
||||
return EMPTY_VALUE;
|
||||
}
|
||||
section = &existing.asMutableSection();
|
||||
path = path.substr(pos + 1);
|
||||
}
|
||||
|
||||
section->set(path, std::move(value));
|
||||
mDirty = true;
|
||||
return section->set(path, std::move(value));
|
||||
}
|
||||
|
||||
mijin::Result<> FileConfig::init(mijin::PathReference path)
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#define RAID_PUBLIC_RAID_CONFIG_HPP_INCLUDED 1
|
||||
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <variant>
|
||||
@@ -82,7 +83,7 @@ public:
|
||||
[[nodiscard]]
|
||||
ConfigValue& getOrAdd(std::string_view key);
|
||||
|
||||
void set(std::string_view key, ConfigValue value);
|
||||
ConfigValue& set(std::string_view key, ConfigValue value);
|
||||
|
||||
[[nodiscard]]
|
||||
mijin::VectorMap<std::string, ConfigValue>& getValues() noexcept { return mValues; }
|
||||
@@ -136,7 +137,8 @@ public:
|
||||
bool asBool() const noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
config_int_t asInt() const noexcept;
|
||||
config_int_t asInt(config_int_t minValue = std::numeric_limits<config_int_t>::min(),
|
||||
config_int_t maxValue = std::numeric_limits<config_int_t>::max()) const noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
double asDouble() const noexcept;
|
||||
@@ -173,7 +175,13 @@ public:
|
||||
[[nodiscard]]
|
||||
const ConfigValue& getValue(std::string_view path) const noexcept;
|
||||
|
||||
void setValue(std::string_view path, ConfigValue value) noexcept;
|
||||
[[nodiscard]]
|
||||
const ConfigValue& getOrAddValue(std::string_view path, ConfigValue defaultValue);
|
||||
|
||||
[[nodiscard]]
|
||||
const ConfigValue& getOrComputeValue(std::string_view path, std::function<ConfigValue()> computeFunc);
|
||||
|
||||
const ConfigValue& setValue(std::string_view path, ConfigValue value) noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
bool valueExists(std::string_view path) const noexcept;
|
||||
|
||||
Reference in New Issue
Block a user