config/Data: cast to std::chrono::steady_clock::duration properly

Oh no, 3413d1bf23 was broken!  Instead of passing a number as
"seconds" to the duration constructor, it just abused the duration
constructor as cast operator, which caused custom state_file_interval
settings to be extremely short.
This commit is contained in:
Max Kellermann 2020-10-08 19:57:00 +02:00
parent d64729065e
commit 27cc7b352d
5 changed files with 50 additions and 10 deletions

1
NEWS
View File

@ -4,6 +4,7 @@ ver 0.22.1 (not yet released)
- jack, pulse: reduce the delay when stopping or pausing playback - jack, pulse: reduce the delay when stopping or pausing playback
* playlist * playlist
- cue: fix two crash bugs - cue: fix two crash bugs
* state_file: fix the state_file_interval setting
ver 0.22 (2020/09/23) ver 0.22 (2020/09/23)
* protocol * protocol

View File

@ -101,6 +101,38 @@ ConfigData::GetPositive(ConfigOption option, unsigned default_value) const
}); });
} }
std::chrono::steady_clock::duration
ConfigData::GetUnsigned(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 not be negative");
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;
});
}
bool bool
ConfigData::GetBool(ConfigOption option, bool default_value) const ConfigData::GetBool(ConfigOption option, bool default_value) const
{ {

View File

@ -78,22 +78,14 @@ struct ConfigData {
std::chrono::steady_clock::duration std::chrono::steady_clock::duration
GetUnsigned(ConfigOption option, GetUnsigned(ConfigOption option,
std::chrono::steady_clock::duration default_value) const { std::chrono::steady_clock::duration default_value) const;
// TODO: allow unit suffixes
auto u = GetUnsigned(option, default_value.count());
return std::chrono::steady_clock::duration(u);
}
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, GetPositive(ConfigOption option,
std::chrono::steady_clock::duration default_value) const { std::chrono::steady_clock::duration default_value) const;
// TODO: allow unit suffixes
auto u = GetPositive(option, default_value.count());
return std::chrono::steady_clock::duration(u);
}
bool GetBool(ConfigOption option, bool default_value) const; bool GetBool(ConfigOption option, bool default_value) const;

View File

@ -135,3 +135,11 @@ ParseSize(const char *s, std::size_t default_factor)
return value; return value;
} }
std::chrono::steady_clock::duration
ParseDuration(const char *s)
{
// TODO: allow unit suffixes
const std::chrono::seconds seconds(ParseLong(s));
return std::chrono::duration_cast<std::chrono::steady_clock::duration>(seconds);
}

View File

@ -20,6 +20,7 @@
#ifndef MPD_CONFIG_PARSER_HXX #ifndef MPD_CONFIG_PARSER_HXX
#define MPD_CONFIG_PARSER_HXX #define MPD_CONFIG_PARSER_HXX
#include <chrono>
#include <cstddef> #include <cstddef>
/** /**
@ -54,4 +55,10 @@ ParsePositive(const char *s);
std::size_t std::size_t
ParseSize(const char *s, std::size_t default_factor=1); ParseSize(const char *s, std::size_t default_factor=1);
/**
* Throws on error.
*/
std::chrono::steady_clock::duration
ParseDuration(const char *s);
#endif #endif