0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-05-18 10:53:48 +02:00

ircd::conf: Add template lex_cast convenience; limited to bool specialization.

This commit is contained in:
Jason Volk 2023-01-19 11:19:24 -08:00
parent 829e641a48
commit 600563d272
2 changed files with 35 additions and 0 deletions

View file

@ -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<class T> T as(const string_view &key, T def);
template<class T> 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);

View file

@ -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<bool>(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<bool>(val))
def = lex_cast<bool>(val);
return def;
}
std::string
ircd::conf::get(const string_view &key)
try