IgANet
IGAnets - Isogeometric Analysis Networks
Loading...
Searching...
No Matches
dlloader.hpp
Go to the documentation of this file.
1
15#pragma once
16
17#include <exception>
18#include <filesystem>
19#include <map>
20
21#if defined(_WIN32)
22#include <direct.h>
23#include <windows.h>
24#else
25#include <dlfcn.h>
26#include <sys/stat.h>
27#endif
28
29namespace iganet {
30
34class DLHandler {
35public:
37 DLHandler() = delete;
38 DLHandler(DLHandler &&) = delete;
39 DLHandler(const DLHandler &) = delete;
40
45 explicit DLHandler(const char *filename, int flags = RTLD_NOW) {
46#if defined(_WIN32)
47 static_cast<void>(flags);
48 HMODULE dl = LoadLibrary(filename);
49 if (!dl)
50 throw std::runtime_error("LoadLibrary - error: " + GetLastError());
51 handle.reset(dl, FreeLibrary);
52#elif defined(__APPLE__) || defined(__linux__) || defined(__unix)
53 void *dl = ::dlopen(filename, flags);
54 if (!dl)
55 throw std::runtime_error(::dlerror());
56 handle.reset(dl, ::dlclose);
57#else
58#error("Unsupported operating system")
59#endif
60 }
61
65 explicit DLHandler(const std::string &filename, int flags = RTLD_NOW)
66 : DLHandler(filename.c_str(), flags) {}
67
71 explicit DLHandler(const std::filesystem::path &filename,
72 int flags = RTLD_NOW)
73 : DLHandler(filename.string(), flags) {}
75
79 void *getSymbol(const char *name) const {
80 if (!handle)
81 throw std::runtime_error(
82 "An error occurred while accessing the dynamic library");
83
84 void *symbol = NULL;
85#if defined(_WIN32)
86 *(void **)(&symbol) = (void *)GetProcAddress(handle.get(), name);
87#elif defined(__APPLE__) || defined(__linux__) || defined(__unix)
88 *(void **)(&symbol) = ::dlsym(handle.get(), name);
89#endif
90 if (!symbol)
91 throw std::runtime_error(
92 "An error occurred while getting the symbol from the dynamic library");
93
94 return symbol;
95 }
96
98 operator bool() const { return (bool)handle; }
99
100private:
102#if defined(_WIN32)
103 std::shared_ptr<std::remove_pointer<HMODULE>::type> handle;
104#elif defined(__APPLE__) || defined(__linux__) || defined(__unix)
105 std::shared_ptr<void> handle;
106#endif
107};
108
113private:
115 std::map<std::string, std::shared_ptr<DLHandler>> libraries;
116
117public:
118 // /// @brief Default constructor deleted
119 // DLLoader() = delete;
120
121 // /// @brief Constructor from filesystem
122 // /// @{
123 // DLLoader(const std::string &path)
124 // : DLLoader(std::vector<std::string>({path})) {}
125
126 // DLLoader(const std::vector<std::string> &paths) { addLibraryPath(paths); }
127 // /// @}
128
129 // /// @brief Adds librariess from given path
130 // inline void addLibraryPath(const std::string &path) {
131 // addLibraryPath(std::vector<std::string>({path}));
132 // }
133
134 // /// @brief Adds librariess from list of directories
135 // inline void addLibraryPath(const std::vector<std::string> &paths) {
136 // for (const auto &path : paths) {
137 // try {
138 // const std::filesystem::path fspath{path};
139 // if (std::filesystem::exists(fspath)) {
140 // for (const auto &entry :
141 // std::filesystem::directory_iterator{fspath}) {
142 // if (entry.path().extension() == ".dll" ||
143 // entry.path().extension() == ".dylib" ||
144 // entry.path().extension() == ".so") {
145 // try {
146 // auto handler =
147 // std::make_shared<DLHandler>(entry.path().c_str());
148 // std::shared_ptr<Model<iganet::real_t>> (*create)(
149 // const nlohmann::json &);
150 // create =
151 // reinterpret_cast<std::shared_ptr<Model<iganet::real_t>> (*)(
152 // const nlohmann::json &)>(handler->getSymbol("create"));
153 // models[create({})->getName()] = handler;
154 // } catch (...) {
155 // std::clog << "Unable to create model " << entry << std::endl;
156 // }
157 // }
158 // }
159 // }
160 // } catch (...) {
161 // std::clog << "Unable to open path " << path << std::endl;
162 // }
163 // }
164 // }
165
166 // /// @brief Returns a new instance of the requested model and
167 // /// throws an exception if model cannot be found
168 // inline std::shared_ptr<Model<iganet::real_t>>
169 // create(const std::string &name, const nlohmann::json &json = NULL) const {
170 // try {
171 // auto model = models.find(name);
172 // if (model == models.end())
173 // throw InvalidModelException();
174 // std::shared_ptr<Model<iganet::real_t>> (*create)(const nlohmann::json &);
175 // create = reinterpret_cast<std::shared_ptr<Model<iganet::real_t>> (*)(
176 // const nlohmann::json &)>(model->second->getSymbol("create"));
177 // return create(json);
178 // } catch (...) {
179 // throw InvalidModelException();
180 // }
181 // }
182
183 // /// @brief Returns a new model instance from binary data stream
184 // /// throws an exception if model cannot be created
185 // inline std::shared_ptr<Model<iganet::real_t>>
186 // load(const nlohmann::json &json) const {
187
188 // for (auto &model : models) {
189 // try {
190 // std::shared_ptr<Model<iganet::real_t>> (*load)(const nlohmann::json &);
191 // load = reinterpret_cast<std::shared_ptr<Model<iganet::real_t>> (*)(
192 // const nlohmann::json &)>(model.second->getSymbol("load"));
193 // return load(json);
194 // } catch (...) { /* try next model */
195 // }
196 // }
197
198 // // No working model found, through exception and quit load request
199 // throw InvalidModelException();
200 // }
201
202 // /// @brief Serializes the list of models to JSON
203 // inline nlohmann::json getModels() const {
204 // auto data = nlohmann::json::array();
205 // for (auto const &model : models)
206 // data.push_back(create(model.first)->getModel());
207 // return data;
208 // }
209
210 // /// @brief Returns a string representation of the model manager object
211 // inline virtual void
212 // pretty_print(std::ostream &os = std::cout) const noexcept override {
213 // os << name();
214 // }
215};
216
217// /// @brief Print (as string) a model manager object
218// inline std::ostream &operator<<(std::ostream &os, const DLLoader &obj) {
219// obj.pretty_print(os);
220// return os;
221// }
222
223} // namespace iganet
Dynamic library handler.
Definition dlloader.hpp:34
DLHandler(const char *filename, int flags=RTLD_NOW)
Constructor from file.
Definition dlloader.hpp:45
DLHandler()=delete
Default constructor deleted.
DLHandler(const DLHandler &)=delete
DLHandler(const std::filesystem::path &filename, int flags=RTLD_NOW)
Provides the DLHandler operation.
Definition dlloader.hpp:71
DLHandler(const std::string &filename, int flags=RTLD_NOW)
Provides the DLHandler operation.
Definition dlloader.hpp:65
void * getSymbol(const char *name) const
Gets symbol from dynamic library.
Definition dlloader.hpp:79
DLHandler(DLHandler &&)=delete
Dynamic library loader.
Definition dlloader.hpp:112
std::map< std::string, std::shared_ptr< DLHandler > > libraries
List of dynamic libraries.
Definition dlloader.hpp:115
Full qualified name descriptor.
Definition fqn.hpp:22
Definition core.hpp:73