0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-06-02 18:18:56 +02:00

ircd::run: Add convenience overload for run::changed.

This commit is contained in:
Jason Volk 2020-02-28 09:55:36 -08:00
parent a6131d1268
commit a038116a50
2 changed files with 36 additions and 11 deletions

View file

@ -46,15 +46,46 @@ namespace ircd::run
struct ircd::run::changed
:instance_list<ircd::run::changed>
{
using handler = std::function<void (const enum level &)>;
static ctx::dock dock;
handler function;
static const enum level single_sentinel
{
std::numeric_limits<std::underlying_type<enum level>::type>::max()
};
enum level single
{
single_sentinel
};
std::function<void ()> handler_one;
std::function<void (const enum level &)> handler
{
[this](const auto &level)
{
if(single == single_sentinel || single != level)
return;
if(likely(handler_one))
handler_one();
}
};
/// The handler function will be called back for any run::level change while
/// this instance remains in scope.
changed(handler function) noexcept;
changed(decltype(handler) function) noexcept
:handler{std::move(function)}
{}
/// The handler function will be called back for the specific run::level
/// change while this instance remains in scope.
changed(const enum level &single, decltype(handler_one) function) noexcept
:single{single}
,handler_one{std::move(function)}
{}
/// Default construction for no-op
changed() = default;
};
/// The run::level allows all observers to know the coarse state of IRCd and to
@ -96,9 +127,3 @@ enum class ircd::run::level
RUN = 4, ///< O | IRCd in service.
QUIT = 5, ///< --> ^ Clean shutdown in progress.
};
inline
ircd::run::changed::changed(handler function)
noexcept
:function(std::move(function))
{}

View file

@ -99,7 +99,7 @@ try
changed::dock.notify_all();
for(const auto &handler : changed::list) try
{
handler->function(level);
handler->handler(level);
}
catch(const std::exception &e)
{