37 if (library_handle_ ==
nullptr) {
41#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
42 FreeLibrary(
static_cast<HINSTANCE
>(library_handle_));
43#elif defined(__GNUC__)
44 dlclose(library_handle_);
49 library_name_ = std::string(library_name);
50#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
51 library_handle_ =
static_cast<void*
>(LoadLibraryA(library_name.c_str()));
52#elif defined(__GNUC__)
53 library_handle_ = dlopen(library_name.c_str(), RTLD_NOW);
55 return library_handle_ !=
nullptr;
62#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
64 FARPROC function_address =
65 GetProcAddress(
static_cast<HINSTANCE
>(library_handle_), function_name);
67 const void* function_address = dlsym(library_handle_, function_name);
70 CHECK(function_address)
71 <<
"Error: could not find function " << std::string(function_name)
72 <<
" in " << library_name_;
74 return TypeParser<T>::CreateFunction(function_address);
78 std::function<T>
GetFunction(
const std::string& function_name) {
83 void GetFunction(std::function<T>* function,
const char* function_name) {
89 const std::string function_name) {
94 void* library_handle_ =
nullptr;
95 std::string library_name_;
100 template <
typename Ret,
typename... Args>
101 struct TypeParser<Ret(Args...)> {
102#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
104 static std::function<Ret(Args...)> CreateFunction(
105 const FARPROC function_address) {
106 return std::function<Ret(Args...)>(
107 reinterpret_cast<Ret (*)(Args...)
>(function_address));
111 static std::function<Ret(Args...)> CreateFunction(
112 const void* function_address) {
113 return std::function<Ret(Args...)>(
reinterpret_cast<Ret (*)(Args...)
>(
114 const_cast<void*
>(function_address)));