18#if ((defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) || __cplusplus >= 201703L)
23#if (defined(_WIN32) || defined(_WIN64))
24#ifndef WIN32_LEAN_AND_MEAN
25#define WIN32_LEAN_AND_MEAN
26#define DYLIB_UNDEFINE_LEAN_AND_MEAN
30#define DYLIB_UNDEFINE_NOMINMAX
33#ifdef DYLIB_UNDEFINE_LEAN_AND_MEAN
34#undef WIN32_LEAN_AND_MEAN
35#undef DYLIB_UNDEFINE_LEAN_AND_MEAN
37#ifdef DYLIB_UNDEFINE_NOMINMAX
39#undef DYLIB_UNDEFINE_NOMINMAX
45#if (defined(_WIN32) || defined(_WIN64))
46#define DYLIB_WIN_MAC_OTHER(win_def, mac_def, other_def) win_def
47#define DYLIB_WIN_OTHER(win_def, other_def) win_def
48#elif defined(__APPLE__)
49#define DYLIB_WIN_MAC_OTHER(win_def, mac_def, other_def) mac_def
50#define DYLIB_WIN_OTHER(win_def, other_def) other_def
52#define DYLIB_WIN_MAC_OTHER(win_def, mac_def, other_def) other_def
53#define DYLIB_WIN_OTHER(win_def, other_def) other_def
71 static_assert(std::is_pointer_v<native_handle_type>,
"Expecting HINSTANCE to be a pointer");
72 static_assert(std::is_pointer_v<native_symbol_type>,
"Expecting FARPROC to be a pointer");
81 using std::runtime_error::runtime_error;
88 using exception::exception;
95 using exception::exception;
104 if (
this != &other) {
105 std::swap(
m_handle, other.m_handle);
123 if (dir_path ==
nullptr) {
124 throw std::invalid_argument(
"The directory path is null");
126 if (lib_name ==
nullptr) {
127 throw std::invalid_argument(
"The library name is null");
130 std::string final_name = lib_name;
131 std::string final_path = dir_path;
137 if (!final_path.empty() && final_path.find_last_of(
'/') != final_path.size() - 1) {
145 "Could not load library \"" + final_path + final_name +
"\"\n" +
152 const std::string& dir_path,
const std::string& lib_name,
155 :
dylib(dir_path.c_str(), lib_name.c_str(), decorations) {}
158 const std::string& dir_path,
const char* lib_name,
161 :
dylib(dir_path.c_str(), lib_name, decorations) {}
164 const char* dir_path,
const std::string& lib_name,
167 :
dylib(dir_path, lib_name.c_str(), decorations) {}
170 :
dylib(
"", lib_name.c_str(), decorations) {}
173 :
dylib(
"", lib_name, decorations) {}
176 explicit dylib(
const std::filesystem::path& lib_path)
180 const std::filesystem::path& dir_path,
const std::string& lib_name,
183 : dylib(dir_path.string().c_str(), lib_name.c_str(), decorations) {}
186 const std::filesystem::path& dir_path,
const char* lib_name,
189 : dylib(dir_path.string().c_str(), lib_name, decorations) {}
210 if (symbol_name ==
nullptr) {
211 throw std::invalid_argument(
"The symbol name to lookup is null");
214 throw std::logic_error(
215 "The dynamic library handle is null. This object may have been moved from."
221 if (symbol ==
nullptr) {
223 "Could not get symbol \"" + std::string(symbol_name) +
"\"\n" +
245 template <
typename T>
247#if (defined(__GNUC__) && __GNUC__ >= 8)
248#pragma GCC diagnostic push
249#pragma GCC diagnostic ignored "-Wcast-function-type"
251 return reinterpret_cast<T*
>(
get_symbol(symbol_name));
252#if (defined(__GNUC__) && __GNUC__ >= 8)
253#pragma GCC diagnostic pop
257 template <
typename T>
259 return get_function<T>(symbol_name.c_str());
273 template <
typename T>
275 return *
reinterpret_cast<T*
>(
get_symbol(symbol_name));
278 template <
typename T>
280 return get_variable<T>(symbol_name.c_str());
292 [[nodiscard]]
bool has_symbol(
const char* symbol_name)
const noexcept {
293 if (
m_handle ==
nullptr || symbol_name ==
nullptr) {
299 [[nodiscard]]
bool has_symbol(
const std::string& symbol)
const noexcept {
312#if (defined(_WIN32) || defined(_WIN64))
313 return LoadLibraryA(path);
315 return dlopen(path, RTLD_NOW | RTLD_LOCAL);
328#if (defined(_WIN32) || defined(_WIN64))
329 constexpr const size_t BUF_SIZE = 512;
330 const auto error_code = GetLastError();
332 return "No error reported by GetLastError";
333 char description[BUF_SIZE];
334 const auto lang = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
335 const DWORD length = FormatMessageA(
336 FORMAT_MESSAGE_FROM_SYSTEM,
nullptr, error_code, lang, description, BUF_SIZE,
nullptr
338 return (length == 0) ?
"Unknown error (FormatMessage failed)" : description;
340 const auto*
const description = dlerror();
341 return (description ==
nullptr) ?
"No error reported by dlerror" : description;
346#undef DYLIB_WIN_MAC_OTHER
347#undef DYLIB_WIN_OTHER
T * get_function(const char *symbol_name) const
Definition dylib.hpp:246
bool has_symbol(const std::string &symbol) const noexcept
Definition dylib.hpp:299
~dylib()
Definition dylib.hpp:193
DYLIB_WIN_OTHER(FARPROC, void *) native_symbol_type
Definition dylib.hpp:69
dylib & operator=(dylib &&other) noexcept
Definition dylib.hpp:103
DYLIB_WIN_OTHER(HINSTANCE, void *) native_handle_type
Definition dylib.hpp:68
dylib(const std::string &lib_name, bool decorations=add_filename_decorations)
Definition dylib.hpp:169
dylib(const char *dir_path, const char *lib_name, bool decorations=add_filename_decorations)
Definition dylib.hpp:122
bool has_symbol(const char *symbol_name) const noexcept
Definition dylib.hpp:292
static constexpr bool add_filename_decorations
Definition dylib.hpp:74
dylib(const std::string &dir_path, const char *lib_name, bool decorations=add_filename_decorations)
Definition dylib.hpp:157
T & get_variable(const std::string &symbol_name) const
Definition dylib.hpp:279
dylib(const dylib &)=delete
dylib(const std::string &dir_path, const std::string &lib_name, bool decorations=add_filename_decorations)
Definition dylib.hpp:151
T & get_variable(const char *symbol_name) const
Definition dylib.hpp:274
native_handle_type m_handle
Definition dylib.hpp:309
dylib(dylib &&other) noexcept
Definition dylib.hpp:101
native_symbol_type get_symbol(const char *symbol_name) const
Definition dylib.hpp:209
native_handle_type native_handle() noexcept
Definition dylib.hpp:306
T * get_function(const std::string &symbol_name) const
Definition dylib.hpp:258
static constexpr bool no_filename_decorations
Definition dylib.hpp:75
static void close(native_handle_type lib) noexcept
Definition dylib.hpp:323
static native_handle_type open(const char *path) noexcept
Definition dylib.hpp:311
dylib & operator=(const dylib &)=delete
dylib(const char *dir_path, const std::string &lib_name, bool decorations=add_filename_decorations)
Definition dylib.hpp:163
native_symbol_type get_symbol(const std::string &symbol_name) const
Definition dylib.hpp:230
static std::string get_error_description()
Definition dylib.hpp:327
dylib(const char *lib_name, bool decorations=add_filename_decorations)
Definition dylib.hpp:172
static native_symbol_type locate_symbol(native_handle_type lib, const char *name) noexcept
Definition dylib.hpp:319
#define DYLIB_WIN_MAC_OTHER(win_def, mac_def, other_def)
Definition dylib.hpp:52
#define DYLIB_WIN_OTHER(win_def, other_def)
Definition dylib.hpp:53
Definition aerodyn_inflow.hpp:15
static constexpr const char * prefix
Definition dylib.hpp:65
static constexpr const char * suffix
Definition dylib.hpp:66