From 515370b2c24511b9d999f44dffbd7a892d84a530 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 22 Feb 2018 19:34:24 -0800 Subject: [PATCH] ircd::m: Add and apply module importer; replace any module name suffixes. --- include/ircd/m/import.h | 71 +++++++++++++++++++++++++++++++++++++++++ include/ircd/m/m.h | 2 +- ircd/m/m.cc | 56 ++++++++++---------------------- 3 files changed, 88 insertions(+), 41 deletions(-) create mode 100644 include/ircd/m/import.h diff --git a/include/ircd/m/import.h b/include/ircd/m/import.h new file mode 100644 index 000000000..00b8fed9c --- /dev/null +++ b/include/ircd/m/import.h @@ -0,0 +1,71 @@ +// Matrix Construct +// +// Copyright (C) Matrix Construct Developers, Authors & Contributors +// Copyright (C) 2016-2018 Jason Volk +// +// 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_IMPORT_H + +namespace ircd::m +{ + template struct import; + + extern std::map modules; +} + +template +struct ircd::m::import +:mods::import +{ + std::string modname; + std::string symname; + + void reload(); + + public: + template auto operator()(args&&... a); + + import(std::string modname, std::string symname); +}; + +template +ircd::m::import::import(std::string modname, + std::string symname) +:mods::import{} +,modname{std::move(modname)} +,symname{std::move(symname)} +{} + +template +template +auto +ircd::m::import::operator()(args&&... a) +{ + if(unlikely(!*this)) + reload(); + + return mods::import::operator()(std::forward(a)...); +} + +template +void +ircd::m::import::reload() +try +{ + auto &import(static_cast &>(*this)); + import = { modules.at(modname), symname }; +} +catch(const std::out_of_range &e) +{ + throw m::UNAVAILABLE + { + "Sorry, %s in %s is currently unavailable.", + symname, + modname + }; +} diff --git a/include/ircd/m/m.h b/include/ircd/m/m.h index 0e8d8f353..1617a3bd8 100644 --- a/include/ircd/m/m.h +++ b/include/ircd/m/m.h @@ -20,7 +20,6 @@ namespace ircd::m extern struct room my_room; extern struct room control; extern struct log::log log; - extern std::map modules; extern std::list listeners; IRCD_OVERLOAD(generate) @@ -45,6 +44,7 @@ namespace ircd #include "name.h" #include "error.h" +#include "import.h" #include "id.h" #include "event.h" #include "dbs.h" diff --git a/ircd/m/m.cc b/ircd/m/m.cc index 07e3cfc28..32c3f2d10 100644 --- a/ircd/m/m.cc +++ b/ircd/m/m.cc @@ -118,7 +118,7 @@ ircd::m::init::modules() if(startswith_any(name, std::begin(prefixes), std::end(prefixes))) m::modules.emplace(name, name); - m::modules.emplace("root.so"s, "root.so"s); + m::modules.emplace("root"s, "root"s); } namespace ircd::m @@ -275,20 +275,12 @@ ircd::m::create(const id::room &room_id, const id::user &creator, const string_view &type) { - static const auto modname - { - "client_createroom.so" - }; - - static const auto symname - { - "createroom__type" - }; - using prototype = room (const id::room &, const id::user &, const string_view &); - static mods::import function; - if(unlikely(!function)) - function = { modules.at(modname), symname }; + + static import function + { + "client_createroom", "createroom__type" + }; return function(room_id, creator, type); } @@ -299,20 +291,12 @@ ircd::m::create(const id::room &room_id, const id::room &parent, const string_view &type) { - static const auto modname - { - "client_createroom.so" - }; - - static const auto symname - { - "createroom__parent_type" - }; - using prototype = room (const id::room &, const id::user &, const id::room &, const string_view &); - static mods::import function; - if(unlikely(!function)) - function = { modules.at(modname), symname }; + + static import function + { + "client_createroom", "createroom__parent_type" + }; return function(room_id, creator, parent, type); } @@ -321,20 +305,12 @@ ircd::m::event::id::buf ircd::m::join(const room &room, const id::user &user_id) { - static const auto modname - { - "client_rooms_join.so" - }; - - static const auto symname - { - "join__room_user" - }; - using prototype = event::id::buf (const m::room &, const id::user &); - static mods::import function; - if(unlikely(!function)) - function = { modules.at(modname), symname }; + + static import function + { + "client_rooms", "join__room_user" + }; return function(room, user_id); }