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;
62 const void* function_address =
64 static_cast<void*
>(GetProcAddress(
65 static_cast<HINSTANCE
>(library_handle_), function_name));
67 dlsym(library_handle_, function_name);
70 CHECK(function_address !=
nullptr)
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) {
79 return GetFunction<T>(function_name.c_str());
83 void GetFunction(std::function<T>*
function,
const char* function_name) {
84 *
function = GetFunction<T>(function_name);
89 const std::string function_name) {
90 GetFunction<T>(
function, function_name.c_str());
94 void* library_handle_ =
nullptr;
95 std::string library_name_;
100 template <
typename Ret,
typename... Args>
101 struct TypeParser<Ret(Args...)> {
102 static std::function<Ret(Args...)> CreateFunction(
103 const void* function_address) {
104 return std::function<Ret(Args...)>(
reinterpret_cast<Ret (*)(Args...)
>(
105 const_cast<void*
>(function_address)));
110 #endif // OR_TOOLS_BASE_DYNAMIC_LIBRARY_H_