2018-03-24 05:40:20 +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_MODS_IMPORT_H
|
|
|
|
|
|
|
|
namespace ircd::mods
|
|
|
|
{
|
|
|
|
template<class T> struct import;
|
2018-09-14 03:02:10 +02:00
|
|
|
struct imports extern imports;
|
2019-02-10 02:38:46 +01:00
|
|
|
|
|
|
|
std::string make_target_name(const string_view &name, const string_view &demangled);
|
2018-03-24 05:40:20 +01:00
|
|
|
}
|
|
|
|
|
2018-09-14 03:02:10 +02:00
|
|
|
struct ircd::mods::imports
|
|
|
|
:std::map<std::string, mods::module, std::less<>>
|
|
|
|
{};
|
|
|
|
|
2018-03-24 05:40:20 +01:00
|
|
|
/// Representation of a symbol in a loaded shared library
|
|
|
|
///
|
|
|
|
template<class T>
|
|
|
|
struct ircd::mods::import
|
|
|
|
:sym_ptr
|
|
|
|
{
|
2019-02-10 02:38:46 +01:00
|
|
|
string_view mangled_type;
|
|
|
|
std::string demangled_type;
|
2018-09-14 03:02:10 +02:00
|
|
|
std::string module_name;
|
|
|
|
std::string symbol_name;
|
2019-02-10 02:38:46 +01:00
|
|
|
std::string target_name;
|
2018-09-14 03:02:10 +02:00
|
|
|
|
|
|
|
void reload();
|
|
|
|
|
|
|
|
public:
|
2019-02-11 22:15:17 +01:00
|
|
|
template<class... args> decltype(auto) operator()(args&&...) const;
|
|
|
|
template<class... args> decltype(auto) operator()(args&&...);
|
2018-09-14 00:56:11 +02:00
|
|
|
|
|
|
|
const T *operator->() const;
|
|
|
|
const T &operator*() const;
|
|
|
|
operator const T &() const;
|
|
|
|
|
|
|
|
T *operator->();
|
|
|
|
T &operator*();
|
|
|
|
operator T &();
|
2018-03-24 05:40:20 +01:00
|
|
|
|
2018-09-14 03:02:10 +02:00
|
|
|
explicit import(const mods::module &, std::string symbol_name);
|
|
|
|
import(std::string module_name, std::string symbol_name);
|
2018-03-24 05:40:20 +01:00
|
|
|
};
|
2018-09-14 00:56:11 +02:00
|
|
|
|
|
|
|
template<class T>
|
2018-09-14 03:02:10 +02:00
|
|
|
ircd::mods::import<T>::import(std::string module_name,
|
|
|
|
std::string symbol_name)
|
|
|
|
:sym_ptr
|
|
|
|
{
|
|
|
|
// Note sym_ptr is purposely default constructed; this makes the import
|
|
|
|
// "lazy" and will be loaded via miss on first use. This is useful in
|
|
|
|
// the general use-case of static construction.
|
|
|
|
}
|
2019-02-10 02:38:46 +01:00
|
|
|
,mangled_type
|
2019-02-09 12:05:19 +01:00
|
|
|
{
|
|
|
|
typeid(T).name()
|
|
|
|
}
|
2019-02-10 02:38:46 +01:00
|
|
|
,demangled_type
|
2019-02-09 12:05:19 +01:00
|
|
|
{
|
2019-02-10 02:38:46 +01:00
|
|
|
demangle(mangled_type)
|
2019-02-09 12:05:19 +01:00
|
|
|
}
|
2018-09-14 03:02:10 +02:00
|
|
|
,module_name
|
|
|
|
{
|
|
|
|
std::move(module_name)
|
|
|
|
}
|
|
|
|
,symbol_name
|
|
|
|
{
|
|
|
|
std::move(symbol_name)
|
|
|
|
}
|
2019-02-10 02:38:46 +01:00
|
|
|
,target_name
|
|
|
|
{
|
|
|
|
make_target_name(this->symbol_name, demangled_type)
|
|
|
|
}
|
2018-09-14 03:02:10 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
ircd::mods::import<T>::import(const mods::module &module,
|
|
|
|
std::string symbol_name)
|
|
|
|
:sym_ptr
|
|
|
|
{
|
2019-02-10 02:38:46 +01:00
|
|
|
|
2018-09-14 03:02:10 +02:00
|
|
|
}
|
2019-02-10 02:38:46 +01:00
|
|
|
,mangled_type
|
2019-02-09 12:05:19 +01:00
|
|
|
{
|
|
|
|
typeid(T).name()
|
|
|
|
}
|
2019-02-10 02:38:46 +01:00
|
|
|
,demangled_type
|
2019-02-09 12:05:19 +01:00
|
|
|
{
|
2019-02-10 02:38:46 +01:00
|
|
|
demangle(mangled_type)
|
2019-02-09 12:05:19 +01:00
|
|
|
}
|
2018-09-14 03:02:10 +02:00
|
|
|
,module_name
|
|
|
|
{
|
|
|
|
module.name()
|
|
|
|
}
|
|
|
|
,symbol_name
|
|
|
|
{
|
|
|
|
std::move(symbol_name)
|
|
|
|
}
|
2019-02-10 02:38:46 +01:00
|
|
|
,target_name
|
|
|
|
{
|
|
|
|
make_target_name(this->symbol_name, demangled_type)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
auto &sp
|
|
|
|
{
|
|
|
|
static_cast<sym_ptr &>(*this)
|
|
|
|
};
|
|
|
|
|
|
|
|
sp =
|
|
|
|
{
|
|
|
|
module, !empty(target_name)? target_name : this->symbol_name
|
|
|
|
};
|
|
|
|
}
|
2018-09-14 03:02:10 +02:00
|
|
|
|
|
|
|
template<class T>
|
|
|
|
ircd::mods::import<T>::operator
|
|
|
|
T &()
|
2018-09-14 00:56:11 +02:00
|
|
|
{
|
2018-09-14 03:02:10 +02:00
|
|
|
return this->operator*();
|
2018-09-14 00:56:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
T &
|
|
|
|
ircd::mods::import<T>::operator*()
|
|
|
|
{
|
2018-09-14 03:02:10 +02:00
|
|
|
return *this->operator->();
|
2018-09-14 00:56:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
T *
|
|
|
|
ircd::mods::import<T>::operator->()
|
|
|
|
{
|
2018-09-14 03:02:10 +02:00
|
|
|
if(unlikely(!*this))
|
|
|
|
reload();
|
|
|
|
|
2018-09-14 00:56:11 +02:00
|
|
|
return sym_ptr::operator-><T>();
|
|
|
|
}
|
|
|
|
|
2019-02-11 22:15:17 +01:00
|
|
|
template<class T>
|
|
|
|
template<class... args>
|
2019-07-17 23:31:52 +02:00
|
|
|
inline decltype(auto)
|
2019-02-11 22:15:17 +01:00
|
|
|
ircd::mods::import<T>::operator()(args&&... a)
|
|
|
|
{
|
|
|
|
if(unlikely(!*this))
|
|
|
|
reload();
|
|
|
|
|
|
|
|
return sym_ptr::operator()<T>(std::forward<args>(a)...);
|
|
|
|
}
|
|
|
|
|
2018-09-14 00:56:11 +02:00
|
|
|
template<class T>
|
|
|
|
ircd::mods::import<T>::operator
|
|
|
|
const T &()
|
|
|
|
const
|
|
|
|
{
|
2018-09-14 03:02:10 +02:00
|
|
|
return this->operator*();
|
2018-09-14 00:56:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
const T &
|
|
|
|
ircd::mods::import<T>::operator*()
|
|
|
|
const
|
|
|
|
{
|
2018-09-14 03:02:10 +02:00
|
|
|
return this->operator->();
|
2018-09-14 00:56:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
const T *
|
|
|
|
ircd::mods::import<T>::operator->()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return sym_ptr::operator-><T>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
template<class... args>
|
2019-07-17 23:31:52 +02:00
|
|
|
__attribute__((artificial))
|
|
|
|
inline decltype(auto)
|
2018-09-14 00:56:11 +02:00
|
|
|
ircd::mods::import<T>::operator()(args&&... a)
|
|
|
|
const
|
|
|
|
{
|
2019-02-11 22:15:17 +01:00
|
|
|
return sym_ptr::operator()<T>(std::forward<args>(a)...);
|
2018-09-14 00:56:11 +02:00
|
|
|
}
|
2018-09-14 03:02:10 +02:00
|
|
|
|
|
|
|
template<class T>
|
|
|
|
void
|
|
|
|
ircd::mods::import<T>::reload()
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto &module
|
|
|
|
{
|
|
|
|
imports.at(module_name)
|
|
|
|
};
|
|
|
|
|
|
|
|
auto &sp
|
|
|
|
{
|
|
|
|
static_cast<sym_ptr &>(*this)
|
|
|
|
};
|
|
|
|
|
2019-02-09 12:05:19 +01:00
|
|
|
const auto &symname
|
|
|
|
{
|
2019-02-10 02:38:46 +01:00
|
|
|
!empty(target_name)? target_name : symbol_name
|
2019-02-09 12:05:19 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
sp = { module, symname };
|
2018-09-14 03:02:10 +02:00
|
|
|
}
|
|
|
|
catch(const std::out_of_range &e)
|
|
|
|
{
|
|
|
|
throw unavailable
|
|
|
|
{
|
|
|
|
"Sorry, %s in %s is currently unavailable.",
|
|
|
|
symbol_name,
|
|
|
|
module_name
|
|
|
|
};
|
|
|
|
}
|