mirror of
https://github.com/matrix-construct/construct
synced 2024-12-28 00:14:07 +01:00
ircd::conf: Add set callback notification.
This commit is contained in:
parent
63aa8e310e
commit
ea172a3378
2 changed files with 54 additions and 13 deletions
|
@ -43,6 +43,8 @@ namespace ircd::conf
|
||||||
IRCD_EXCEPTION(error, not_found)
|
IRCD_EXCEPTION(error, not_found)
|
||||||
IRCD_EXCEPTION(error, bad_value)
|
IRCD_EXCEPTION(error, bad_value)
|
||||||
|
|
||||||
|
using set_cb = std::function<void ()>;
|
||||||
|
|
||||||
extern std::map<string_view, item<> *> items;
|
extern std::map<string_view, item<> *> items;
|
||||||
|
|
||||||
bool exists(const string_view &key);
|
bool exists(const string_view &key);
|
||||||
|
@ -59,6 +61,7 @@ struct ircd::conf::item<void>
|
||||||
json::strung feature_;
|
json::strung feature_;
|
||||||
json::object feature;
|
json::object feature;
|
||||||
string_view name;
|
string_view name;
|
||||||
|
conf::set_cb set_cb;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual string_view on_get(const mutable_buffer &) const;
|
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;
|
string_view get(const mutable_buffer &) const;
|
||||||
bool set(const string_view &);
|
bool set(const string_view &);
|
||||||
|
|
||||||
item(const json::members &);
|
item(const json::members &, conf::set_cb = {});
|
||||||
item(item &&) = delete;
|
item(item &&) = delete;
|
||||||
item(const item &) = delete;
|
item(const item &) = delete;
|
||||||
virtual ~item() noexcept;
|
virtual ~item() noexcept;
|
||||||
|
@ -111,9 +114,16 @@ struct ircd::conf::lex_castable
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
lex_castable(const json::members &members)
|
lex_castable(const json::members &members,
|
||||||
:conf::item<>{members}
|
conf::set_cb set_cb = {})
|
||||||
,conf::value<T>(feature.get("default", long(0)))
|
: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;
|
string_view on_get(const mutable_buffer &out) const override;
|
||||||
bool on_set(const string_view &s) 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<>
|
template<>
|
||||||
|
@ -141,7 +151,7 @@ struct ircd::conf::item<bool>
|
||||||
string_view on_get(const mutable_buffer &out) const override;
|
string_view on_get(const mutable_buffer &out) const override;
|
||||||
bool on_set(const string_view &s) 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<>
|
template<>
|
||||||
|
|
45
ircd/conf.cc
45
ircd/conf.cc
|
@ -80,7 +80,8 @@ ircd::conf::exists(const string_view &key)
|
||||||
//
|
//
|
||||||
|
|
||||||
/// Conf item abstract constructor.
|
/// 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_
|
:feature_
|
||||||
{
|
{
|
||||||
opts
|
opts
|
||||||
|
@ -93,6 +94,10 @@ ircd::conf::item<void>::item(const json::members &opts)
|
||||||
{
|
{
|
||||||
unquote(feature.at("name"))
|
unquote(feature.at("name"))
|
||||||
}
|
}
|
||||||
|
,set_cb
|
||||||
|
{
|
||||||
|
std::move(set_cb)
|
||||||
|
}
|
||||||
{
|
{
|
||||||
if(!items.emplace(name, this).second)
|
if(!items.emplace(name, this).second)
|
||||||
throw error
|
throw error
|
||||||
|
@ -120,6 +125,18 @@ ircd::conf::item<void>::set(const string_view &val)
|
||||||
on_set(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;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -151,9 +168,16 @@ const
|
||||||
// std::string
|
// std::string
|
||||||
//
|
//
|
||||||
|
|
||||||
ircd::conf::item<std::string>::item(const json::members &members)
|
ircd::conf::item<std::string>::item(const json::members &members,
|
||||||
:conf::item<>{members}
|
conf::set_cb set_cb)
|
||||||
,value{unquote(feature.get("default"))}
|
:conf::item<>
|
||||||
|
{
|
||||||
|
members, std::move(set_cb)
|
||||||
|
}
|
||||||
|
,value
|
||||||
|
{
|
||||||
|
unquote(feature.get("default"))
|
||||||
|
}
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -175,9 +199,16 @@ const
|
||||||
// bool
|
// bool
|
||||||
//
|
//
|
||||||
|
|
||||||
ircd::conf::item<bool>::item(const json::members &members)
|
ircd::conf::item<bool>::item(const json::members &members,
|
||||||
:conf::item<>{members}
|
conf::set_cb set_cb)
|
||||||
,value{feature.get<bool>("default", false)}
|
:conf::item<>
|
||||||
|
{
|
||||||
|
members, std::move(set_cb)
|
||||||
|
}
|
||||||
|
,value
|
||||||
|
{
|
||||||
|
feature.get<bool>("default", false)
|
||||||
|
}
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue