config/Data: merge the two duration parser methods, pass minimum value

This commit is contained in:
Max Kellermann 2024-07-10 16:24:19 +02:00
parent 3733bc57b7
commit 1e9e182a32
3 changed files with 9 additions and 26 deletions

View File

@ -10,7 +10,8 @@
StateFileConfig::StateFileConfig(const ConfigData &config) StateFileConfig::StateFileConfig(const ConfigData &config)
:path(config.GetPath(ConfigOption::STATE_FILE)), :path(config.GetPath(ConfigOption::STATE_FILE)),
interval(config.GetUnsigned(ConfigOption::STATE_FILE_INTERVAL, interval(config.GetDuration(ConfigOption::STATE_FILE_INTERVAL,
std::chrono::seconds{1},
DEFAULT_INTERVAL)), DEFAULT_INTERVAL)),
restore_paused(config.GetBool(ConfigOption::RESTORE_PAUSED, false)) restore_paused(config.GetBool(ConfigOption::RESTORE_PAUSED, false))
{ {

View File

@ -86,32 +86,17 @@ ConfigData::GetPositive(ConfigOption option, unsigned default_value) const
} }
std::chrono::steady_clock::duration std::chrono::steady_clock::duration
ConfigData::GetUnsigned(ConfigOption option, ConfigData::GetDuration(ConfigOption option,
std::chrono::steady_clock::duration min_value,
std::chrono::steady_clock::duration default_value) const std::chrono::steady_clock::duration default_value) const
{ {
return With(option, [default_value](const char *s){ return With(option, [min_value, default_value](const char *s){
if (s == nullptr) if (s == nullptr)
return default_value; return default_value;
auto value = ParseDuration(s); auto value = ParseDuration(s);
if (value < std::chrono::steady_clock::duration{}) if (value < min_value)
throw std::runtime_error("Value must not be negative"); throw std::runtime_error{"Value is too small"};
return value;
});
}
std::chrono::steady_clock::duration
ConfigData::GetPositive(ConfigOption option,
std::chrono::steady_clock::duration default_value) const
{
return With(option, [default_value](const char *s){
if (s == nullptr)
return default_value;
auto value = ParseDuration(s);
if (value <= std::chrono::steady_clock::duration{})
throw std::runtime_error("Value must be positive");
return value; return value;
}); });

View File

@ -60,15 +60,12 @@ struct ConfigData {
unsigned GetUnsigned(ConfigOption option, unsigned GetUnsigned(ConfigOption option,
unsigned default_value) const; unsigned default_value) const;
std::chrono::steady_clock::duration
GetUnsigned(ConfigOption option,
std::chrono::steady_clock::duration default_value) const;
unsigned GetPositive(ConfigOption option, unsigned GetPositive(ConfigOption option,
unsigned default_value) const; unsigned default_value) const;
std::chrono::steady_clock::duration std::chrono::steady_clock::duration
GetPositive(ConfigOption option, GetDuration(ConfigOption option,
std::chrono::steady_clock::duration min_value,
std::chrono::steady_clock::duration default_value) const; std::chrono::steady_clock::duration default_value) const;
bool GetBool(ConfigOption option, bool default_value) const; bool GetBool(ConfigOption option, bool default_value) const;