From 7c21d579536f3804466f42f602c0d809ee723f58 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 10 Jul 2024 16:29:51 +0200 Subject: [PATCH] config/Block: add method GetDuration() --- src/config/Block.cxx | 24 ++++++++++++++++++++++++ src/config/Block.hxx | 9 +++++++++ 2 files changed, 33 insertions(+) diff --git a/src/config/Block.cxx b/src/config/Block.cxx index aa18b8f0c..dead04269 100644 --- a/src/config/Block.cxx +++ b/src/config/Block.cxx @@ -46,6 +46,18 @@ BlockParam::GetBoolValue() const 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 * ConfigBlock::GetBlockParam(const char *name) const noexcept { @@ -128,6 +140,18 @@ ConfigBlock::GetBlockValue(const char *name, bool default_value) const 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 ConfigBlock::ThrowWithNested() const { diff --git a/src/config/Block.hxx b/src/config/Block.hxx index 5dc0bb588..384ff5c8e 100644 --- a/src/config/Block.hxx +++ b/src/config/Block.hxx @@ -4,6 +4,7 @@ #pragma once #include +#include #include #include @@ -33,6 +34,9 @@ struct BlockParam { 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 * exception showing the location of this setting in the @@ -122,6 +126,11 @@ struct ConfigBlock { 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 * exception showing the location of this block in the