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__) 35 if (library_handle_ ==
nullptr) {
40 FreeLibrary(static_cast<HINSTANCE>(library_handle_));
41 #elif defined(__GNUC__) 42 dlclose(library_handle_);
47 library_name_ = std::string(library_name);
49 library_handle_ = static_cast<void*>(LoadLibrary(library_name.c_str()));
50 #elif defined(__GNUC__) 51 library_handle_ = dlopen(library_name.c_str(), RTLD_NOW);
53 return library_handle_ !=
nullptr;
60 const void* function_address =
62 static_cast<void*>(GetProcAddress(
63 static_cast<HINSTANCE>(library_handle_), function_name));
65 dlsym(library_handle_, function_name);
68 CHECK(function_address !=
nullptr)
69 <<
"Error: could not find function " << std::string(function_name)
70 <<
" in " << library_name_;
72 return TypeParser<T>::CreateFunction(function_address);
76 std::function<T>
GetFunction(
const std::string& function_name) {
77 return GetFunction<T>(function_name.c_str());
81 void GetFunction(std::function<T>*
function,
const char* function_name) {
82 *
function = GetFunction<T>(function_name);
87 const std::string function_name) {
88 GetFunction<T>(
function, function_name.c_str());
92 void* library_handle_ =
nullptr;
93 std::string library_name_;
98 template <
typename Ret,
typename... Args>
99 struct TypeParser<Ret(Args...)> {
100 static std::function<Ret(Args...)> CreateFunction(
101 const void* function_address) {
102 return std::function<Ret(Args...)>(
reinterpret_cast<Ret (*)(Args...)
>(
103 const_cast<void*>(function_address)));
108 #endif // OR_TOOLS_BASE_DYNAMIC_LIBRARY_H_
bool LibraryIsLoaded() const
std::function< T > GetFunction(const char *function_name)
std::function< T > GetFunction(const std::string &function_name)
bool TryToLoad(const std::string &library_name)
void GetFunction(std::function< T > *function, const std::string function_name)
void GetFunction(std::function< T > *function, const char *function_name)