2018-02-26 08:24:12 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_M_HOOK_H
|
|
|
|
|
2020-05-13 00:23:20 +02:00
|
|
|
/// Hooks allow dynamic functionality to be invoked as a result of an event
|
|
|
|
/// matching some criteria. Hooks are comprised of two interfacing components:
|
|
|
|
/// the hook function (callee) and the hook site (caller); these components
|
|
|
|
/// link and delink to each other during initialization. This hook system is
|
|
|
|
/// oriented around the m::event structure; every hook function has an m::event
|
|
|
|
/// as its first argument. An optional second argument can be specified with
|
|
|
|
/// the template to convey additional payload and options.
|
|
|
|
///
|
|
|
|
/// Hook functions and hook sites are constructed out of json::members (pairs
|
|
|
|
/// of json::value in an initializer list). We refer to this as the "feature."
|
|
|
|
/// Each member with a name directly corresponding to an m::event property is
|
|
|
|
/// a match parameter. The hook function is not called if a matching parameter
|
|
|
|
/// is specified in the feature, but the event input at the hook::site does not
|
|
|
|
/// match it. Undefined features match everything.
|
|
|
|
///
|
|
|
|
/// One can create a hook pair anywhere, simply create a m::hook::site with a
|
|
|
|
/// feature `{ "name", "myname" }` and a hook::hook with a similar feature
|
|
|
|
/// `{ "_site", "myname" }` matching the site's name; these objects must have
|
|
|
|
/// matching templates.
|
|
|
|
///
|
2018-05-27 07:05:56 +02:00
|
|
|
namespace ircd::m::hook
|
|
|
|
{
|
|
|
|
IRCD_EXCEPTION(ircd::error, error)
|
|
|
|
|
|
|
|
struct base;
|
|
|
|
struct maps;
|
|
|
|
|
|
|
|
template<class data = void> struct hook;
|
|
|
|
template<class data = void> struct site;
|
|
|
|
|
|
|
|
template<> struct hook<void>;
|
|
|
|
template<> struct site<void>;
|
|
|
|
}
|
|
|
|
|
2018-02-26 08:24:12 +01:00
|
|
|
namespace ircd::m
|
|
|
|
{
|
2018-05-27 07:05:56 +02:00
|
|
|
template<class data = void> using hookfn = m::hook::hook<data>;
|
2018-02-26 08:24:12 +01:00
|
|
|
}
|
|
|
|
|
2020-05-13 00:23:20 +02:00
|
|
|
/// Non-template base class for all hook functions. This is the handler
|
|
|
|
/// (or callee) component of the hook
|
|
|
|
///
|
2018-05-27 07:05:56 +02:00
|
|
|
struct ircd::m::hook::base
|
|
|
|
:instance_list<base>
|
2018-02-26 08:24:12 +01:00
|
|
|
{
|
|
|
|
struct site;
|
|
|
|
|
|
|
|
json::strung _feature;
|
|
|
|
json::object feature;
|
2018-02-26 09:07:53 +01:00
|
|
|
m::event matching;
|
2018-05-07 06:53:23 +02:00
|
|
|
bool registered {false};
|
2018-05-13 04:52:25 +02:00
|
|
|
size_t matchers {0};
|
2018-05-13 04:43:57 +02:00
|
|
|
size_t calls {0};
|
2019-04-19 06:42:47 +02:00
|
|
|
size_t calling {0};
|
2018-02-26 08:24:12 +01:00
|
|
|
|
2020-05-13 00:36:56 +02:00
|
|
|
public:
|
2020-05-13 00:38:23 +02:00
|
|
|
uint id() const;
|
2018-05-07 06:53:23 +02:00
|
|
|
site *find_site() const;
|
2020-05-13 00:36:56 +02:00
|
|
|
string_view site_name() const;
|
2018-05-07 06:53:23 +02:00
|
|
|
|
2018-05-27 07:05:56 +02:00
|
|
|
protected:
|
|
|
|
base(const json::members &);
|
|
|
|
base(base &&) = delete;
|
|
|
|
base(const base &) = delete;
|
|
|
|
virtual ~base() noexcept;
|
2018-02-26 08:24:12 +01:00
|
|
|
};
|
|
|
|
|
2023-02-03 08:33:32 +01:00
|
|
|
template<>
|
|
|
|
decltype(ircd::m::hook::base::list)
|
|
|
|
ircd::instance_list<ircd::m::hook::base>::list;
|
|
|
|
|
2020-05-13 00:23:20 +02:00
|
|
|
/// Non-template base class for all hook sites (dispatcher/caller component)
|
|
|
|
///
|
2018-05-27 07:05:56 +02:00
|
|
|
struct ircd::m::hook::base::site
|
2018-05-07 06:53:23 +02:00
|
|
|
:instance_list<site>
|
2018-02-26 08:24:12 +01:00
|
|
|
{
|
|
|
|
json::strung _feature;
|
|
|
|
json::object feature;
|
2018-02-26 09:07:53 +01:00
|
|
|
size_t count {0};
|
2018-05-27 07:05:56 +02:00
|
|
|
std::unique_ptr<struct maps> maps;
|
|
|
|
std::set<base *> hooks;
|
|
|
|
size_t matchers {0};
|
2018-09-18 05:45:05 +02:00
|
|
|
bool exceptions {true};
|
2020-05-25 05:56:30 +02:00
|
|
|
bool interrupts {true};
|
2019-04-19 06:42:47 +02:00
|
|
|
size_t calls {0};
|
|
|
|
size_t calling {0};
|
2018-02-26 08:24:12 +01:00
|
|
|
|
2020-05-13 00:36:56 +02:00
|
|
|
public:
|
2020-05-13 00:38:23 +02:00
|
|
|
uint id() const;
|
2018-05-25 03:27:46 +02:00
|
|
|
string_view name() const;
|
2020-05-13 00:36:56 +02:00
|
|
|
|
|
|
|
protected:
|
|
|
|
friend class base;
|
2018-05-27 07:05:56 +02:00
|
|
|
bool add(base &);
|
|
|
|
bool del(base &);
|
2018-02-26 08:24:12 +01:00
|
|
|
|
2018-05-27 07:05:56 +02:00
|
|
|
void match(const event &, const std::function<bool (base &)> &);
|
2018-02-26 08:24:12 +01:00
|
|
|
|
|
|
|
site(const json::members &);
|
|
|
|
site(site &&) = delete;
|
|
|
|
site(const site &) = delete;
|
2018-05-27 07:05:56 +02:00
|
|
|
virtual ~site() noexcept;
|
2018-02-26 08:24:12 +01:00
|
|
|
};
|
2018-05-27 07:05:56 +02:00
|
|
|
|
2023-02-03 08:33:32 +01:00
|
|
|
template<>
|
|
|
|
decltype(ircd::m::hook::base::site::list)
|
|
|
|
ircd::instance_list<ircd::m::hook::base::site>::list;
|
|
|
|
|
2020-05-13 00:23:20 +02:00
|
|
|
/// Hook function with no payload; only an m::event argument
|
2018-05-27 07:05:56 +02:00
|
|
|
template<>
|
|
|
|
struct ircd::m::hook::hook<void>
|
|
|
|
final
|
|
|
|
:base
|
|
|
|
{
|
|
|
|
std::function<void (const m::event &)> function;
|
|
|
|
|
|
|
|
public:
|
|
|
|
hook(const json::members &feature, decltype(function) function);
|
|
|
|
hook(decltype(function) function, const json::members &feature);
|
|
|
|
};
|
|
|
|
|
2020-05-13 00:23:20 +02:00
|
|
|
/// Hook site for functions with no payload.
|
2018-05-27 07:05:56 +02:00
|
|
|
template<>
|
|
|
|
struct ircd::m::hook::site<void>
|
|
|
|
final
|
|
|
|
:base::site
|
|
|
|
{
|
|
|
|
void call(hook<void> &, const event &);
|
|
|
|
|
|
|
|
public:
|
2020-05-13 01:12:48 +02:00
|
|
|
void operator()(base **const &, const event &);
|
2018-05-27 07:05:56 +02:00
|
|
|
void operator()(const event &);
|
|
|
|
|
|
|
|
site(const json::members &feature);
|
|
|
|
};
|
|
|
|
|
2020-05-13 00:23:20 +02:00
|
|
|
/// Hook function with a template class as the payload
|
2020-10-29 08:08:06 +01:00
|
|
|
template<class data_type>
|
2018-05-27 07:05:56 +02:00
|
|
|
struct ircd::m::hook::hook
|
|
|
|
:base
|
|
|
|
{
|
2020-10-29 08:08:06 +01:00
|
|
|
std::function<void (const m::event &, data_type)> function;
|
2018-05-27 07:05:56 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
hook(const json::members &feature, decltype(function) function)
|
|
|
|
:base{feature}
|
|
|
|
,function{std::move(function)}
|
|
|
|
{}
|
|
|
|
|
|
|
|
hook(decltype(function) function, const json::members &feature)
|
|
|
|
:base{feature}
|
|
|
|
,function{std::move(function)}
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2020-05-13 00:23:20 +02:00
|
|
|
/// Hook site for functions with a template class as the payload
|
2020-10-29 08:08:06 +01:00
|
|
|
template<class data_type>
|
2018-05-27 07:05:56 +02:00
|
|
|
struct ircd::m::hook::site
|
|
|
|
:base::site
|
|
|
|
{
|
2020-10-29 08:08:06 +01:00
|
|
|
void call(hook<data_type> &hfn, const event &event, data_type d);
|
2018-05-27 07:05:56 +02:00
|
|
|
|
|
|
|
public:
|
2020-10-29 08:08:06 +01:00
|
|
|
void operator()(base **const &, const event &event, data_type d);
|
|
|
|
void operator()(const event &event, data_type d);
|
2018-05-27 07:05:56 +02:00
|
|
|
|
|
|
|
site(const json::members &feature)
|
|
|
|
:base::site{feature}
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class data>
|
|
|
|
void
|
|
|
|
ircd::m::hook::site<data>::operator()(const event &event,
|
|
|
|
data d)
|
|
|
|
{
|
2020-05-13 01:12:48 +02:00
|
|
|
base *cur {nullptr};
|
|
|
|
operator()(&cur, event, std::forward<data>(d));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class data>
|
|
|
|
void
|
|
|
|
ircd::m::hook::site<data>::operator()(base **const &cur,
|
|
|
|
const event &event,
|
|
|
|
data d)
|
|
|
|
{
|
2020-05-25 05:56:30 +02:00
|
|
|
const ctx::uninterruptible::nothrow ui
|
2020-05-19 03:56:58 +02:00
|
|
|
{
|
|
|
|
!interrupts
|
|
|
|
};
|
|
|
|
|
2020-05-13 01:12:48 +02:00
|
|
|
// Iterate all matching hooks
|
|
|
|
match(event, [this, &cur, &event, &d]
|
2018-05-27 07:05:56 +02:00
|
|
|
(base &base)
|
|
|
|
{
|
2020-05-13 01:12:48 +02:00
|
|
|
// Indicate which hook we're entering
|
|
|
|
const scope_restore entered
|
|
|
|
{
|
|
|
|
*cur, std::addressof(base)
|
|
|
|
};
|
|
|
|
|
|
|
|
auto &hfn
|
|
|
|
{
|
|
|
|
dynamic_cast<hook<data> &>(base)
|
|
|
|
};
|
|
|
|
|
|
|
|
call(hfn, event, d);
|
2018-05-27 07:05:56 +02:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class data>
|
|
|
|
void
|
|
|
|
ircd::m::hook::site<data>::call(hook<data> &hfn,
|
|
|
|
const event &event,
|
|
|
|
data d)
|
|
|
|
try
|
|
|
|
{
|
2019-04-19 06:42:47 +02:00
|
|
|
// stats for site
|
|
|
|
++calls;
|
2019-07-26 21:17:47 +02:00
|
|
|
const scope_count site_calling
|
|
|
|
{
|
|
|
|
calling
|
|
|
|
};
|
2019-04-19 06:42:47 +02:00
|
|
|
|
|
|
|
// stats for hook
|
2018-05-27 07:05:56 +02:00
|
|
|
++hfn.calls;
|
2019-07-26 21:17:47 +02:00
|
|
|
const scope_count hook_calling
|
|
|
|
{
|
|
|
|
hfn.calling
|
|
|
|
};
|
2019-04-19 06:42:47 +02:00
|
|
|
|
|
|
|
// call hook
|
2018-05-27 07:05:56 +02:00
|
|
|
hfn.function(event, d);
|
|
|
|
}
|
2020-05-19 03:56:58 +02:00
|
|
|
catch(const ctx::interrupted &e)
|
|
|
|
{
|
|
|
|
if(exceptions && interrupts)
|
|
|
|
throw;
|
|
|
|
|
|
|
|
log::logf
|
|
|
|
{
|
|
|
|
log, interrupts? log::DERROR: log::ERROR,
|
|
|
|
"site:%u hook:%u %s error :%s",
|
|
|
|
id(),
|
|
|
|
hfn.id(),
|
|
|
|
string_view{hfn.feature},
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
}
|
2018-05-27 07:05:56 +02:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-09-18 05:45:05 +02:00
|
|
|
if(exceptions)
|
|
|
|
throw;
|
|
|
|
|
2018-05-27 07:05:56 +02:00
|
|
|
log::critical
|
|
|
|
{
|
2020-05-13 00:38:23 +02:00
|
|
|
log, "Unhandled site:%u hook:%u %s error :%s",
|
|
|
|
id(),
|
|
|
|
hfn.id(),
|
2018-05-27 07:05:56 +02:00
|
|
|
string_view{hfn.feature},
|
2020-05-13 00:38:23 +02:00
|
|
|
e.what(),
|
2018-05-27 07:05:56 +02:00
|
|
|
};
|
|
|
|
}
|