From 600563d272b150353e52be81a7988a7686ffb373 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 19 Jan 2023 11:19:24 -0800 Subject: [PATCH] ircd::conf: Add template lex_cast convenience; limited to bool specialization. --- include/ircd/conf.h | 5 +++++ ircd/conf.cc | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/include/ircd/conf.h b/include/ircd/conf.h index 6c7068e0f..b53cbbd18 100644 --- a/include/ircd/conf.h +++ b/include/ircd/conf.h @@ -70,6 +70,11 @@ namespace ircd::conf string_view get(const mutable_buffer &out, const string_view &key); std::string get(const string_view &key); + template T as(const string_view &key, T def); + template T as(const string_view &key); + template<> bool as(const string_view &key, bool def); + template<> bool as(const string_view &key); + bool set(const string_view &key, const string_view &value); bool set(std::nothrow_t, const string_view &key, const string_view &value); diff --git a/ircd/conf.cc b/ircd/conf.cc index 737174888..9e909d204 100644 --- a/ircd/conf.cc +++ b/ircd/conf.cc @@ -155,6 +155,36 @@ catch(const std::out_of_range &e) }; } +template<> +bool +ircd::conf::as(const string_view &key) +{ + char buf[5]; // "true" or "false" + const auto val + { + get(buf, key) + }; + + return lex_cast(val); +} + +template<> +bool +ircd::conf::as(const string_view &key, + bool def) +{ + char buf[5]; // "true" or "false" + const auto val + { + get(std::nothrow, buf, key) + }; + + if(val && ircd::lex_castable(val)) + def = lex_cast(val); + + return def; +} + std::string ircd::conf::get(const string_view &key) try