0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-07-05 10:08:36 +02:00

ircd:Ⓜ️ Add and apply module importer; replace any module name suffixes.

This commit is contained in:
Jason Volk 2018-02-22 19:34:24 -08:00
parent c6365fc609
commit 515370b2c2
3 changed files with 88 additions and 41 deletions

71
include/ircd/m/import.h Normal file
View file

@ -0,0 +1,71 @@
// 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_IMPORT_H
namespace ircd::m
{
template<class prototype> struct import;
extern std::map<std::string, ircd::module> modules;
}
template<class prototype>
struct ircd::m::import
:mods::import<prototype>
{
std::string modname;
std::string symname;
void reload();
public:
template<class... args> auto operator()(args&&... a);
import(std::string modname, std::string symname);
};
template<class prototype>
ircd::m::import<prototype>::import(std::string modname,
std::string symname)
:mods::import<prototype>{}
,modname{std::move(modname)}
,symname{std::move(symname)}
{}
template<class prototype>
template<class... args>
auto
ircd::m::import<prototype>::operator()(args&&... a)
{
if(unlikely(!*this))
reload();
return mods::import<prototype>::operator()(std::forward<args>(a)...);
}
template<class prototype>
void
ircd::m::import<prototype>::reload()
try
{
auto &import(static_cast<mods::import<prototype> &>(*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
};
}

View file

@ -20,7 +20,6 @@ namespace ircd::m
extern struct room my_room;
extern struct room control;
extern struct log::log log;
extern std::map<std::string, ircd::module> modules;
extern std::list<ircd::net::listener> 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"

View file

@ -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<prototype> function;
if(unlikely(!function))
function = { modules.at(modname), symname };
static import<prototype> 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<prototype> function;
if(unlikely(!function))
function = { modules.at(modname), symname };
static import<prototype> 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<prototype> function;
if(unlikely(!function))
function = { modules.at(modname), symname };
static import<prototype> function
{
"client_rooms", "join__room_user"
};
return function(room, user_id);
}