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
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
string_view site_name() const;
|
2018-05-07 06:53:23 +02:00
|
|
|
site *find_site() const;
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
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};
|
2019-04-19 06:42:47 +02:00
|
|
|
size_t calls {0};
|
|
|
|
size_t calling {0};
|
2018-02-26 08:24:12 +01:00
|
|
|
|
2018-05-27 07:05:56 +02:00
|
|
|
friend class base;
|
2018-05-25 03:27:46 +02:00
|
|
|
string_view name() const;
|
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
|
|
|
|
2018-05-27 07:05:56 +02:00
|
|
|
protected:
|
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
|
|
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct ircd::m::hook::site<void>
|
|
|
|
final
|
|
|
|
:base::site
|
|
|
|
{
|
|
|
|
void call(hook<void> &, const event &);
|
|
|
|
|
|
|
|
public:
|
|
|
|
void operator()(const event &);
|
|
|
|
|
|
|
|
site(const json::members &feature);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class data>
|
|
|
|
struct ircd::m::hook::hook
|
|
|
|
:base
|
|
|
|
{
|
|
|
|
std::function<void (const m::event &, data)> function;
|
|
|
|
|
|
|
|
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)}
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class data>
|
|
|
|
struct ircd::m::hook::site
|
|
|
|
:base::site
|
|
|
|
{
|
|
|
|
void call(hook<data> &hfn, const event &event, data d);
|
|
|
|
|
|
|
|
public:
|
|
|
|
void operator()(const event &event, data d);
|
|
|
|
|
|
|
|
site(const json::members &feature)
|
|
|
|
:base::site{feature}
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class data>
|
|
|
|
void
|
|
|
|
ircd::m::hook::site<data>::operator()(const event &event,
|
|
|
|
data d)
|
|
|
|
{
|
|
|
|
match(event, [this, &event, &d]
|
|
|
|
(base &base)
|
|
|
|
{
|
|
|
|
call(dynamic_cast<hook<data> &>(base), event, d);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
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
|
|
|
|
{
|
2019-07-26 21:17:47 +02:00
|
|
|
log, "Unhandled hookfn(%p) %s error :%s",
|
2018-05-27 07:05:56 +02:00
|
|
|
&hfn,
|
|
|
|
string_view{hfn.feature},
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
}
|