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
30{
32{
33 static constexpr size_t kMaxFunctionsNotFound = 10;
34
35public:
37 library_handle_(nullptr)
38 {
39 }
40
42 {
43 if (library_handle_ == nullptr)
44 {
45 return;
46 }
47
48#if defined(_MSC_VER)
49 FreeLibrary(static_cast<HINSTANCE>(library_handle_));
50#elif defined(__GNUC__)
51 dlclose(library_handle_);
52#endif
53 }
54
55 bool TryToLoad(const std::string& library_name)
56 {
57 library_name_ = std::string(library_name);
58#if defined(_MSC_VER)
59 library_handle_ = static_cast<void*>(LoadLibraryA(library_name.c_str()));
60#elif defined(__GNUC__)
61 library_handle_ = dlopen(library_name.c_str(), RTLD_NOW);
62#endif
63 return library_handle_ != nullptr;
64 }
65
66 bool LibraryIsLoaded() const
67 {
68 return library_handle_ != nullptr;
69 }
70
71 const std::vector<std::string>& FunctionsNotFound() const
72 {
73 return functions_not_found_;
74 }
75
76 template<typename T>
77 std::function<T> GetFunction(const char* function_name)
78 {
79 const void* function_address =
80#if defined(_MSC_VER)
81 static_cast<void*>(
82 GetProcAddress(static_cast<HINSTANCE>(library_handle_), function_name));
83#else
84 dlsym(library_handle_, function_name);
85#endif
86 // We don't really need the full list of missing functions,
87 // just a few are enough.
88 if (!function_address && functions_not_found_.size() < kMaxFunctionsNotFound)
89 {
90 functions_not_found_.push_back(function_name);
91 }
92
93 return TypeParser<T>::CreateFunction(function_address);
94 }
95
96 template<typename T>
97 std::function<T> GetFunction(const std::string& function_name)
98 {
99 return GetFunction<T>(function_name.c_str());
100 }
101
102 template<typename T>
103 void GetFunction(std::function<T>* function, const char* function_name)
104 {
105 *function = GetFunction<T>(function_name);
106 }
107
108 template<typename T>
109 void GetFunction(std::function<T>* function, const std::string function_name)
110 {
111 GetFunction<T>(function, function_name.c_str());
112 }
113
114private:
115 void* library_handle_ = nullptr;
116 std::string library_name_;
117 std::vector<std::string> functions_not_found_;
118
119 template<typename T>
120 struct TypeParser
121 {
122 };
123
124 template<typename Ret, typename... Args>
125 struct TypeParser<Ret(Args...)>
126 {
127 static std::function<Ret(Args...)> CreateFunction(const void* function_address)
128 {
129 return std::function<Ret(Args...)>(
130 reinterpret_cast<Ret (*)(Args...)>(const_cast<void*>(function_address)));
131 }
132 };
133};
134} // namespace Solver
135
136#endif // OR_TOOLS_BASE_DYNAMIC_LIBRARY_H_
Definition dynamic_library.h:32