mirror of
https://github.com/RPCS3/rpcs3.git
synced 2024-11-16 23:17:29 +00:00
98fc131d47
Futex implementation
81 lines
1.5 KiB
C++
81 lines
1.5 KiB
C++
#include <string>
|
|
|
|
namespace utils
|
|
{
|
|
class dynamic_library
|
|
{
|
|
void *m_handle = nullptr;
|
|
|
|
public:
|
|
dynamic_library() = default;
|
|
dynamic_library(const std::string &path);
|
|
|
|
~dynamic_library();
|
|
|
|
bool load(const std::string &path);
|
|
void close();
|
|
|
|
private:
|
|
void *get_impl(const std::string &name) const;
|
|
|
|
public:
|
|
template<typename Type = void>
|
|
Type *get(const std::string &name) const
|
|
{
|
|
Type *result;
|
|
*(void **)(&result) = get_impl(name);
|
|
return result;
|
|
}
|
|
|
|
template<typename Type>
|
|
bool get(Type *&function, const std::string &name) const
|
|
{
|
|
*(void **)(&function) = get_impl(name);
|
|
|
|
return !!function;
|
|
}
|
|
|
|
bool loaded() const;
|
|
explicit operator bool() const;
|
|
};
|
|
|
|
// (assume the lib is always loaded)
|
|
void* get_proc_address(const char* lib, const char* name);
|
|
|
|
template <typename F>
|
|
struct dynamic_import
|
|
{
|
|
static_assert(sizeof(F) == 0, "Invalid function type");
|
|
};
|
|
|
|
template <typename R, typename... Args>
|
|
struct dynamic_import<R(Args...)>
|
|
{
|
|
R(*ptr)(Args...);
|
|
const char* const lib;
|
|
const char* const name;
|
|
|
|
// Constant initialization
|
|
constexpr dynamic_import(const char* lib, const char* name)
|
|
: ptr(nullptr)
|
|
, lib(lib)
|
|
, name(name)
|
|
{
|
|
}
|
|
|
|
// Caller
|
|
R operator()(Args... args)
|
|
{
|
|
if (!ptr)
|
|
{
|
|
// TODO: atomic
|
|
ptr = reinterpret_cast<R(*)(Args...)>(get_proc_address(lib, name));
|
|
}
|
|
|
|
return ptr(args...);
|
|
}
|
|
};
|
|
}
|
|
|
|
#define DYNAMIC_IMPORT(lib, name, ...) static utils::dynamic_import<__VA_ARGS__> name(lib, #name);
|