mirror of
https://github.com/matrix-construct/construct
synced 2024-09-27 19:28:52 +02:00
ircd::conf: Add template lex_cast convenience; limited to bool specialization.
This commit is contained in:
parent
829e641a48
commit
600563d272
2 changed files with 35 additions and 0 deletions
|
@ -70,6 +70,11 @@ namespace ircd::conf
|
||||||
string_view get(const mutable_buffer &out, const string_view &key);
|
string_view get(const mutable_buffer &out, const string_view &key);
|
||||||
std::string get(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(const string_view &key, const string_view &value);
|
||||||
bool set(std::nothrow_t, const string_view &key, const string_view &value);
|
bool set(std::nothrow_t, const string_view &key, const string_view &value);
|
||||||
|
|
||||||
|
|
30
ircd/conf.cc
30
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<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
|
std::string
|
||||||
ircd::conf::get(const string_view &key)
|
ircd::conf::get(const string_view &key)
|
||||||
try
|
try
|
||||||
|
|
Loading…
Reference in a new issue