config/Block: add method GetDuration()

This commit is contained in:
Max Kellermann 2024-07-10 16:29:51 +02:00
parent 1e9e182a32
commit 7c21d57953
2 changed files with 33 additions and 0 deletions

View File

@ -46,6 +46,18 @@ BlockParam::GetBoolValue() const
return With(ParseBool); return With(ParseBool);
} }
std::chrono::steady_clock::duration
BlockParam::GetDuration(std::chrono::steady_clock::duration min_value) const
{
return With([min_value](const char *s){
auto duration = ParseDuration(s);
if (duration < min_value)
throw std::invalid_argument{"Value is too small"};
return duration;
});
}
const BlockParam * const BlockParam *
ConfigBlock::GetBlockParam(const char *name) const noexcept ConfigBlock::GetBlockParam(const char *name) const noexcept
{ {
@ -128,6 +140,18 @@ ConfigBlock::GetBlockValue(const char *name, bool default_value) const
return bp->GetBoolValue(); return bp->GetBoolValue();
} }
std::chrono::steady_clock::duration
ConfigBlock::GetDuration(const char *name,
std::chrono::steady_clock::duration min_value,
std::chrono::steady_clock::duration default_value) const
{
const BlockParam *bp = GetBlockParam(name);
if (bp == nullptr)
return default_value;
return bp->GetDuration(min_value);
}
void void
ConfigBlock::ThrowWithNested() const ConfigBlock::ThrowWithNested() const
{ {

View File

@ -4,6 +4,7 @@
#pragma once #pragma once
#include <concepts> #include <concepts>
#include <chrono>
#include <string> #include <string>
#include <vector> #include <vector>
@ -33,6 +34,9 @@ struct BlockParam {
bool GetBoolValue() const; bool GetBoolValue() const;
std::chrono::steady_clock::duration
GetDuration(std::chrono::steady_clock::duration min_value) const;
/** /**
* Call this method in a "catch" block to throw a nested * Call this method in a "catch" block to throw a nested
* exception showing the location of this setting in the * exception showing the location of this setting in the
@ -122,6 +126,11 @@ struct ConfigBlock {
bool GetBlockValue(const char *name, bool default_value) const; bool GetBlockValue(const char *name, bool default_value) const;
std::chrono::steady_clock::duration
GetDuration(const char *name,
std::chrono::steady_clock::duration min_value,
std::chrono::steady_clock::duration default_value) const;
/** /**
* Call this method in a "catch" block to throw a nested * Call this method in a "catch" block to throw a nested
* exception showing the location of this block in the * exception showing the location of this block in the