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_SYM_PTR_H
|
|
|
|
|
|
|
|
namespace ircd::mods
|
|
|
|
{
|
|
|
|
struct sym_ptr;
|
2019-02-11 22:15:17 +01:00
|
|
|
|
|
|
|
template<class F,
|
|
|
|
class... args,
|
|
|
|
typename std::enable_if<!std::is_member_pointer<F>::value>::type * = nullptr>
|
|
|
|
decltype(auto) invoke(F *const &f, args&&... a);
|
|
|
|
|
|
|
|
template<class F,
|
|
|
|
class O,
|
|
|
|
class... args,
|
|
|
|
typename std::enable_if<std::is_member_pointer<F>::value>::type * = nullptr>
|
|
|
|
decltype(auto) invoke(F *const &f, O *const &o, args&&... a);
|
2018-03-24 05:40:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Representation of a symbol in a loaded library (non-template; low level).
|
|
|
|
///
|
|
|
|
class ircd::mods::sym_ptr
|
|
|
|
:std::weak_ptr<mod>
|
|
|
|
{
|
|
|
|
void *ptr {nullptr};
|
|
|
|
|
|
|
|
public:
|
|
|
|
bool operator!() const;
|
2018-05-26 07:41:17 +02:00
|
|
|
explicit operator bool() const;
|
2018-03-24 05:40:20 +01:00
|
|
|
|
|
|
|
template<class T> const T *get() const;
|
|
|
|
template<class T> const T *operator->() const;
|
|
|
|
template<class T> const T &operator*() const;
|
2019-02-11 22:15:17 +01:00
|
|
|
template<class T, class... args> decltype(auto) operator()(args&&... a) const;
|
2018-03-24 05:40:20 +01:00
|
|
|
|
2019-02-11 22:15:17 +01:00
|
|
|
void *&get();
|
2018-03-24 05:40:20 +01:00
|
|
|
template<class T> T *get();
|
|
|
|
template<class T> T *operator->();
|
|
|
|
template<class T> T &operator*();
|
|
|
|
|
2018-03-26 00:25:32 +02:00
|
|
|
explicit sym_ptr(mod &, const string_view &symname);
|
2018-03-25 23:32:24 +02:00
|
|
|
sym_ptr(module, const string_view &symname);
|
|
|
|
sym_ptr(const string_view &modname, const string_view &symname);
|
2018-03-26 00:25:32 +02:00
|
|
|
sym_ptr() = default;
|
2018-03-24 05:40:20 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
T &
|
|
|
|
ircd::mods::sym_ptr::operator*()
|
|
|
|
{
|
|
|
|
if(unlikely(expired()))
|
2018-08-17 19:46:28 +02:00
|
|
|
throw expired_symbol
|
|
|
|
{
|
|
|
|
"The reference to a symbol in another module is no longer valid"
|
|
|
|
};
|
2018-03-24 05:40:20 +01:00
|
|
|
|
2019-02-10 02:38:46 +01:00
|
|
|
assert(ptr);
|
2018-11-14 09:11:26 +01:00
|
|
|
return *operator-><T>();
|
2018-03-24 05:40:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
T *
|
|
|
|
ircd::mods::sym_ptr::operator->()
|
|
|
|
{
|
|
|
|
return get<T>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
T *
|
|
|
|
ircd::mods::sym_ptr::get()
|
|
|
|
{
|
|
|
|
return reinterpret_cast<T *>(ptr);
|
|
|
|
}
|
|
|
|
|
2019-02-11 22:15:17 +01:00
|
|
|
inline void *&
|
|
|
|
ircd::mods::sym_ptr::get()
|
|
|
|
{
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2018-03-24 05:40:20 +01:00
|
|
|
template<class T,
|
|
|
|
class... args>
|
2019-07-17 23:31:52 +02:00
|
|
|
__attribute__((always_inline, artificial))
|
|
|
|
inline decltype(auto)
|
2018-03-24 05:40:20 +01:00
|
|
|
ircd::mods::sym_ptr::operator()(args&&... a)
|
|
|
|
const
|
|
|
|
{
|
2019-02-11 22:15:17 +01:00
|
|
|
return invoke(get<T>(), std::forward<args>(a)...);
|
2018-03-24 05:40:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
const T &
|
|
|
|
ircd::mods::sym_ptr::operator*()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
if(unlikely(expired()))
|
2018-08-17 19:46:28 +02:00
|
|
|
throw expired_symbol
|
|
|
|
{
|
|
|
|
"The const reference to a symbol in another module is no longer valid"
|
|
|
|
};
|
2018-03-24 05:40:20 +01:00
|
|
|
|
2019-02-10 02:38:46 +01:00
|
|
|
assert(ptr);
|
2018-11-14 09:11:26 +01:00
|
|
|
return *operator-><T>();
|
2018-03-24 05:40:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
const T *
|
|
|
|
ircd::mods::sym_ptr::operator->()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return get<T>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
const T *
|
|
|
|
ircd::mods::sym_ptr::get()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return reinterpret_cast<const T *>(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ircd::mods::sym_ptr::operator
|
|
|
|
bool()
|
|
|
|
const
|
|
|
|
{
|
2018-05-26 07:41:17 +02:00
|
|
|
return !operator!();
|
2018-03-24 05:40:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::mods::sym_ptr::operator!()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return !ptr || expired();
|
|
|
|
}
|
2019-02-11 22:15:17 +01:00
|
|
|
|
|
|
|
template<class F,
|
|
|
|
class O,
|
|
|
|
class... args,
|
|
|
|
typename std::enable_if<std::is_member_pointer<F>::value>::type *>
|
2019-07-17 23:31:52 +02:00
|
|
|
__attribute__((always_inline, gnu_inline, artificial))
|
|
|
|
inline extern decltype(auto)
|
2019-02-11 22:15:17 +01:00
|
|
|
ircd::mods::invoke(F *const &f,
|
|
|
|
O *const &o,
|
|
|
|
args&&... a)
|
|
|
|
{
|
|
|
|
const void *const nv(f);
|
|
|
|
const F mfp(*reinterpret_cast<const F *>(&nv));
|
|
|
|
O *const volatile that(o);
|
|
|
|
return std::invoke(mfp, that, std::forward<args>(a)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class F,
|
|
|
|
class... args,
|
|
|
|
typename std::enable_if<!std::is_member_pointer<F>::value>::type *>
|
2019-07-17 23:31:52 +02:00
|
|
|
__attribute__((always_inline, gnu_inline, artificial))
|
|
|
|
inline extern decltype(auto)
|
2019-02-11 22:15:17 +01:00
|
|
|
ircd::mods::invoke(F *const &f,
|
|
|
|
args&&... a)
|
|
|
|
{
|
|
|
|
return std::invoke(f, std::forward<args>(a)...);
|
|
|
|
}
|