0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-27 07:54:05 +01:00

ircd::conf: Add set callback notification.

This commit is contained in:
Jason Volk 2018-08-22 13:58:10 -07:00
parent 63aa8e310e
commit ea172a3378
2 changed files with 54 additions and 13 deletions

View file

@ -43,6 +43,8 @@ namespace ircd::conf
IRCD_EXCEPTION(error, not_found)
IRCD_EXCEPTION(error, bad_value)
using set_cb = std::function<void ()>;
extern std::map<string_view, item<> *> items;
bool exists(const string_view &key);
@ -59,6 +61,7 @@ struct ircd::conf::item<void>
json::strung feature_;
json::object feature;
string_view name;
conf::set_cb set_cb;
protected:
virtual string_view on_get(const mutable_buffer &) const;
@ -68,7 +71,7 @@ struct ircd::conf::item<void>
string_view get(const mutable_buffer &) const;
bool set(const string_view &);
item(const json::members &);
item(const json::members &, conf::set_cb = {});
item(item &&) = delete;
item(const item &) = delete;
virtual ~item() noexcept;
@ -111,9 +114,16 @@ struct ircd::conf::lex_castable
return true;
}
lex_castable(const json::members &members)
:conf::item<>{members}
,conf::value<T>(feature.get("default", long(0)))
lex_castable(const json::members &members,
conf::set_cb set_cb = {})
:conf::item<>
{
members, std::move(set_cb)
}
,conf::value<T>
(
feature.get("default", long(0))
)
{}
};
@ -130,7 +140,7 @@ struct ircd::conf::item<std::string>
string_view on_get(const mutable_buffer &out) const override;
bool on_set(const string_view &s) override;
item(const json::members &members);
item(const json::members &members, conf::set_cb set_cb = {});
};
template<>
@ -141,7 +151,7 @@ struct ircd::conf::item<bool>
string_view on_get(const mutable_buffer &out) const override;
bool on_set(const string_view &s) override;
item(const json::members &members);
item(const json::members &members, conf::set_cb set_cb = {});
};
template<>

View file

@ -80,7 +80,8 @@ ircd::conf::exists(const string_view &key)
//
/// Conf item abstract constructor.
ircd::conf::item<void>::item(const json::members &opts)
ircd::conf::item<void>::item(const json::members &opts,
conf::set_cb set_cb)
:feature_
{
opts
@ -93,6 +94,10 @@ ircd::conf::item<void>::item(const json::members &opts)
{
unquote(feature.at("name"))
}
,set_cb
{
std::move(set_cb)
}
{
if(!items.emplace(name, this).second)
throw error
@ -120,6 +125,18 @@ ircd::conf::item<void>::set(const string_view &val)
on_set(val)
};
if(set_cb) try
{
set_cb();
}
catch(const std::exception &e)
{
log::error
{
"conf item[%s] set callback :%s", e.what()
};
}
return ret;
}
@ -151,9 +168,16 @@ const
// std::string
//
ircd::conf::item<std::string>::item(const json::members &members)
:conf::item<>{members}
,value{unquote(feature.get("default"))}
ircd::conf::item<std::string>::item(const json::members &members,
conf::set_cb set_cb)
:conf::item<>
{
members, std::move(set_cb)
}
,value
{
unquote(feature.get("default"))
}
{
}
@ -175,9 +199,16 @@ const
// bool
//
ircd::conf::item<bool>::item(const json::members &members)
:conf::item<>{members}
,value{feature.get<bool>("default", false)}
ircd::conf::item<bool>::item(const json::members &members,
conf::set_cb set_cb)
:conf::item<>
{
members, std::move(set_cb)
}
,value
{
feature.get<bool>("default", false)
}
{
}