2018-02-23 04:34:24 +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_IMPORT_H
|
|
|
|
|
|
|
|
namespace ircd::m
|
|
|
|
{
|
|
|
|
template<class prototype> struct import;
|
2018-08-15 01:47:42 +02:00
|
|
|
struct imports extern imports;
|
2018-02-23 04:34:24 +01:00
|
|
|
}
|
|
|
|
|
2018-08-15 01:47:42 +02:00
|
|
|
struct ircd::m::imports
|
|
|
|
:std::map<std::string, ircd::module, std::less<>>
|
|
|
|
{
|
|
|
|
void init();
|
|
|
|
};
|
|
|
|
|
2018-02-23 04:34:24 +01:00
|
|
|
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);
|
2018-08-16 08:13:25 +02:00
|
|
|
prototype *operator->();
|
|
|
|
prototype &operator*();
|
|
|
|
operator prototype &();
|
2018-02-23 04:34:24 +01:00
|
|
|
|
|
|
|
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)}
|
|
|
|
{}
|
|
|
|
|
2018-08-16 08:13:25 +02:00
|
|
|
template<class prototype>
|
|
|
|
ircd::m::import<prototype>::operator
|
|
|
|
prototype &()
|
|
|
|
{
|
|
|
|
return this->operator*();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class prototype>
|
|
|
|
prototype &
|
|
|
|
ircd::m::import<prototype>::operator*()
|
|
|
|
{
|
|
|
|
return *this->operator->();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class prototype>
|
|
|
|
prototype *
|
|
|
|
ircd::m::import<prototype>::operator->()
|
|
|
|
{
|
|
|
|
if(unlikely(!*this))
|
|
|
|
reload();
|
|
|
|
|
|
|
|
return mods::import<prototype>::operator->();
|
|
|
|
}
|
|
|
|
|
2018-02-23 04:34:24 +01:00
|
|
|
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));
|
2018-08-15 00:51:57 +02:00
|
|
|
import = { imports.at(modname), symname };
|
2018-02-23 04:34:24 +01:00
|
|
|
}
|
|
|
|
catch(const std::out_of_range &e)
|
|
|
|
{
|
|
|
|
throw m::UNAVAILABLE
|
|
|
|
{
|
|
|
|
"Sorry, %s in %s is currently unavailable.",
|
|
|
|
symname,
|
|
|
|
modname
|
|
|
|
};
|
|
|
|
}
|