0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-25 16:22:35 +01:00

ircd::conf: Abstraction for all lex_cast'able types; add additional duration specializations.

This commit is contained in:
Jason Volk 2018-03-09 12:20:23 -08:00
parent 27b01a58eb
commit 4145a94fc8
2 changed files with 60 additions and 57 deletions

View file

@ -14,12 +14,17 @@
namespace ircd::conf
{
template<class T = void> struct item; // doesn't exist
template<class T> struct value; // abstraction for carrying item value
template<> struct item<void>; // base class of all conf items
template<> struct item<std::string>;
template<> struct item<uint64_t>;
template<> struct item<int64_t>;
template<> struct item<hours>;
template<> struct item<seconds>;
template<> struct item<milliseconds>;
template<> struct item<microseconds>;
template<class T> struct value; // abstraction for carrying item value
template<class T> struct lex_castable; // abstraction for lex_cast compatible
IRCD_EXCEPTION(ircd::error, error)
IRCD_EXCEPTION(error, not_found)
@ -70,7 +75,29 @@ struct ircd::conf::value
template<class... A>
value(A&&... a)
:_value{std::forward<A>(a)...}
:_value(std::forward<A>(a)...)
{}
};
template<class T>
struct ircd::conf::lex_castable
:conf::item<>
,conf::value<T>
{
string_view get(const mutable_buffer &out) const override
{
return lex_cast(this->_value, out);
}
bool set(const string_view &s) override
{
this->_value = lex_cast<T>(s);
return true;
}
lex_castable(const json::members &members)
:conf::item<>{members}
,conf::value<T>(feature.get("default", long(0)))
{}
};
@ -95,74 +122,50 @@ struct ircd::conf::item<std::string>
return true;
}
item(const json::members &memb)
:conf::item<>{memb}
item(const json::members &members)
:conf::item<>{members}
,value{unquote(feature.get("default"))}
{}
};
template<>
struct ircd::conf::item<uint64_t>
:conf::item<>
,conf::value<uint64_t>
:lex_castable<uint64_t>
{
string_view get(const mutable_buffer &out) const override
{
return lex_cast(_value, out);
}
bool set(const string_view &s) override
{
_value = lex_cast<uint64_t>(s);
return true;
}
item(const json::members &memb)
:conf::item<>{memb}
,value{feature.get("default", 0UL)}
{}
using lex_castable::lex_castable;
};
template<>
struct ircd::conf::item<int64_t>
:conf::item<>
,conf::value<int64_t>
:lex_castable<int64_t>
{
string_view get(const mutable_buffer &out) const override
{
return lex_cast(_value, out);
}
using lex_castable::lex_castable;
};
bool set(const string_view &s) override
{
_value = lex_cast<int64_t>(s);
return true;
}
item(const json::members &memb)
:conf::item<>{memb}
,value{feature.get("default", 0L)}
{}
template<>
struct ircd::conf::item<ircd::hours>
:lex_castable<ircd::hours>
{
using lex_castable::lex_castable;
};
template<>
struct ircd::conf::item<ircd::seconds>
:conf::item<>
,conf::value<seconds>
:lex_castable<ircd::seconds>
{
string_view get(const mutable_buffer &out) const override
{
return lex_cast(_value, out);
}
bool set(const string_view &s) override
{
_value = lex_cast<seconds>(s);
return true;
}
item(const json::members &memb)
:conf::item<>{memb}
,value{feature.get("default", 0L)}
{}
using lex_castable::lex_castable;
};
template<>
struct ircd::conf::item<ircd::milliseconds>
:lex_castable<ircd::milliseconds>
{
using lex_castable::lex_castable;
};
template<>
struct ircd::conf::item<ircd::microseconds>
:lex_castable<ircd::microseconds>
{
using lex_castable::lex_castable;
};

View file

@ -72,8 +72,9 @@ libircd_la_LIBADD = \
# the units that compile spirit grammars otherwise they thrash weaker
# systems.
libircd_la_SOURCES = \
http.cc \
exception.cc \
lexical.cc \
json.cc \
locale.cc \
logger.cc \
info.cc \
@ -82,7 +83,6 @@ libircd_la_SOURCES = \
rfc1459.cc \
rand.cc \
hash.cc \
lexical.cc \
base.cc \
parse.cc \
openssl.cc \
@ -95,7 +95,7 @@ libircd_la_SOURCES = \
fmt.cc \
db.cc \
net.cc \
json.cc \
http.cc \
server.cc \
client.cc \
resource.cc \