14 #ifndef OR_TOOLS_BASE_DYNAMIC_LIBRARY_H_
15 #define OR_TOOLS_BASE_DYNAMIC_LIBRARY_H_
24 #define WIN32_LEAN_AND_MEAN // disables several conflicting macros
26 #elif defined(__GNUC__)
37 if (library_handle_ ==
nullptr) {
42 FreeLibrary(
static_cast<HINSTANCE
>(library_handle_));
43 #elif defined(__GNUC__)
44 dlclose(library_handle_);
49 library_name_ = std::string(library_name);
51 library_handle_ =
static_cast<void *
>(LoadLibrary(library_name.c_str()));
52 #elif defined(__GNUC__)
53 library_handle_ = dlopen(library_name.c_str(), RTLD_NOW);
55 return library_handle_ !=
nullptr;
63 const void* function_address =
65 static_cast<void*
>(GetProcAddress(
66 static_cast<HINSTANCE
>(library_handle_), function_name));
68 dlsym(library_handle_, function_name);
71 CHECK(function_address !=
nullptr)
72 <<
"Error: could not find function " << std::string(function_name)
73 <<
" in " << library_name_;
75 return TypeParser<T>::CreateFunction(function_address);
79 std::function<T>
GetFunction(
const std::string& function_name) {
80 return GetFunction<T>(function_name.c_str());
84 void GetFunction(std::function<T>*
function,
const char* function_name) {
85 *
function = GetFunction<T>(function_name);
90 const std::string function_name) {
91 GetFunction<T>(
function, function_name.c_str());
95 void* library_handle_ =
nullptr;
96 std::string library_name_;
101 template <
typename Ret,
typename... Args>
102 struct TypeParser<Ret(Args...)> {
103 static std::function<Ret(Args...)> CreateFunction(
104 const void* function_address) {
105 return std::function<Ret(Args...)>(
106 reinterpret_cast<Ret (*)(Args...)
>(
const_cast<void*
>(function_address)));
111 #endif // OR_TOOLS_BASE_DYNAMIC_LIBRARY_H_