OR-Tools  8.0
dynamic_library.h
Go to the documentation of this file.
1 // Copyright 2010-2018 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 #ifndef OR_TOOLS_BASE_DYNAMIC_LIBRARY_H_
15 #define OR_TOOLS_BASE_DYNAMIC_LIBRARY_H_
16 
17 #include <functional>
18 #include <stdexcept>
19 #include <string>
20 
21 #include "ortools/base/logging.h"
22 
23 #if defined(_MSC_VER)
24 #define WIN32_LEAN_AND_MEAN // disables several conflicting macros
25 #include <windows.h>
26 #elif defined(__GNUC__)
27 #include <dlfcn.h>
28 #endif
29 
30 #define NAMEOF(x) #x
31 
33  public:
34  DynamicLibrary() : library_handle_(nullptr) {}
35 
37  if (library_handle_ == nullptr) {
38  return;
39  }
40 
41 #if defined(_MSC_VER)
42  FreeLibrary(static_cast<HINSTANCE>(library_handle_));
43 #elif defined(__GNUC__)
44  dlclose(library_handle_);
45 #endif
46  }
47 
48  bool TryToLoad(const std::string& library_name) {
49  library_name_ = std::string(library_name);
50 #if defined(_MSC_VER)
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);
54 #endif
55  return library_handle_ != nullptr;
56  }
57 
58 
59  bool LibraryIsLoaded() const { return library_handle_ != nullptr; }
60 
61  template <typename T>
62  std::function<T> GetFunction(const char* function_name) {
63  const void* function_address =
64 #if defined(_MSC_VER)
65  static_cast<void*>(GetProcAddress(
66  static_cast<HINSTANCE>(library_handle_), function_name));
67 #else
68  dlsym(library_handle_, function_name);
69 #endif
70 
71  CHECK(function_address != nullptr)
72  << "Error: could not find function " << std::string(function_name)
73  << " in " << library_name_;
74 
75  return TypeParser<T>::CreateFunction(function_address);
76  }
77 
78  template <typename T>
79  std::function<T> GetFunction(const std::string& function_name) {
80  return GetFunction<T>(function_name.c_str());
81  }
82 
83  template <typename T>
84  void GetFunction(std::function<T>* function, const char* function_name) {
85  *function = GetFunction<T>(function_name);
86  }
87 
88  template <typename T>
89  void GetFunction(std::function<T>* function,
90  const std::string function_name) {
91  GetFunction<T>(function, function_name.c_str());
92  }
93 
94  private:
95  void* library_handle_ = nullptr;
96  std::string library_name_;
97 
98  template <typename T>
99  struct TypeParser {};
100 
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)));
107  }
108  };
109 };
110 
111 #endif // OR_TOOLS_BASE_DYNAMIC_LIBRARY_H_
DynamicLibrary::DynamicLibrary
DynamicLibrary()
Definition: dynamic_library.h:34
logging.h
DynamicLibrary
Definition: dynamic_library.h:32
DynamicLibrary::GetFunction
void GetFunction(std::function< T > *function, const char *function_name)
Definition: dynamic_library.h:84
DynamicLibrary::TryToLoad
bool TryToLoad(const std::string &library_name)
Definition: dynamic_library.h:48
DynamicLibrary::GetFunction
std::function< T > GetFunction(const char *function_name)
Definition: dynamic_library.h:62
DynamicLibrary::~DynamicLibrary
~DynamicLibrary()
Definition: dynamic_library.h:36
DynamicLibrary::GetFunction
std::function< T > GetFunction(const std::string &function_name)
Definition: dynamic_library.h:79
DynamicLibrary::GetFunction
void GetFunction(std::function< T > *function, const std::string function_name)
Definition: dynamic_library.h:89
DynamicLibrary::LibraryIsLoaded
bool LibraryIsLoaded() const
Definition: dynamic_library.h:59