Antares Xpansion
Investment simulations for Antares studies
Loading...
Searching...
No Matches
dynamic_library.h
1// Copyright 2010-2022 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#include <vector>
21
22#if defined(_MSC_VER)
23#define WIN32_LEAN_AND_MEAN // disables several conflicting macros
24#include <windows.h>
25#elif defined(__GNUC__)
26#include <dlfcn.h>
27#endif
28
29namespace Solver {
31 static constexpr size_t kMaxFunctionsNotFound = 10;
32
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*>(LoadLibraryA(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 bool LibraryIsLoaded() const { return library_handle_ != nullptr; }
59
60 const std::vector<std::string>& FunctionsNotFound() const {
61 return functions_not_found_;
62 }
63
64 template <typename T>
65 std::function<T> GetFunction(const char* function_name) {
66 const void* function_address =
67#if defined(_MSC_VER)
68 static_cast<void*>(GetProcAddress(
69 static_cast<HINSTANCE>(library_handle_), function_name));
70#else
71 dlsym(library_handle_, function_name);
72#endif
73 // We don't really need the full list of missing functions,
74 // just a few are enough.
75 if (!function_address &&
76 functions_not_found_.size() < kMaxFunctionsNotFound)
77 functions_not_found_.push_back(function_name);
78
79 return TypeParser<T>::CreateFunction(function_address);
80 }
81
82 template <typename T>
83 std::function<T> GetFunction(const std::string& function_name) {
84 return GetFunction<T>(function_name.c_str());
85 }
86
87 template <typename T>
88 void GetFunction(std::function<T>* function, const char* function_name) {
89 *function = GetFunction<T>(function_name);
90 }
91
92 template <typename T>
93 void GetFunction(std::function<T>* function,
94 const std::string function_name) {
95 GetFunction<T>(function, function_name.c_str());
96 }
97
98 private:
99 void* library_handle_ = nullptr;
100 std::string library_name_;
101 std::vector<std::string> functions_not_found_;
102
103 template <typename T>
104 struct TypeParser {};
105
106 template <typename Ret, typename... Args>
107 struct TypeParser<Ret(Args...)> {
108 static std::function<Ret(Args...)> CreateFunction(
109 const void* function_address) {
110 return std::function<Ret(Args...)>(reinterpret_cast<Ret (*)(Args...)>(
111 const_cast<void*>(function_address)));
112 }
113 };
114};
115}
116
117#endif // OR_TOOLS_BASE_DYNAMIC_LIBRARY_H_
Definition dynamic_library.h:30