Skip to content

Commit

Permalink
dylib: fix ambiguity for library ctor
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Olivier <[email protected]>
  • Loading branch information
martin-olivier committed Jan 5, 2025
1 parent 40a4590 commit 93714c7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
3 changes: 2 additions & 1 deletion include/dylib.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ class library {
* @param lib_path the path to the dynamic library to load
* @param decorations os decorations to append to the library name
*/
explicit library(std::string lib_path, decorations decorations = decorations::none());
explicit library(const char *lib_path, decorations decorations = decorations::none());
explicit library(const std::string &lib_path, decorations decorations = decorations::none());
#if ((defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) || __cplusplus >= 201703L)
explicit library(const std::filesystem::path &lib_path, decorations decorations = decorations::none());
#endif
Expand Down
35 changes: 22 additions & 13 deletions src/dylib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,35 +91,44 @@ library &library::operator=(library &&other) noexcept {
return *this;
}

library::library(std::string lib_path, dylib::decorations decorations) {
library::library(const char *lib_path, dylib::decorations decorations) {
std::string lib_name;
std::string lib_dir;
std::string lib;

if (lib_path.empty())
throw std::invalid_argument("The library to lookup is an empty string");
if (lib_path.find('/') == std::string::npos)
throw std::invalid_argument("Could not load library '" + lib_path + "': invalid path");
if (!lib_path)
throw std::invalid_argument("The library path to lookup is null");

lib_name = lib_path.substr(lib_path.find_last_of('/') + 1);
lib_dir = lib_path.substr(0, lib_path.find_last_of('/'));
lib = lib_path;

if (lib.empty())
throw std::invalid_argument("The library path to lookup is an empty string");
if (lib.find('/') == std::string::npos)
throw std::invalid_argument("Could not load library '" + lib + "': invalid path");

lib_name = lib.substr(lib.find_last_of('/') + 1);
lib_dir = lib.substr(0, lib.find_last_of('/'));

if (lib_name.empty())
throw std::invalid_argument("Could not load library '" + lib_path + "': invalid path");
throw std::invalid_argument("Could not load library '" + lib + "': invalid path");

lib_name = decorations.decorate(lib_name);
lib_path = lib_dir + '/' + lib_name;
lib = lib_dir + '/' + lib_name;

m_handle = open_lib(lib_path.c_str());
m_handle = open_lib(lib.c_str());
if (!m_handle)
throw load_error("Could not load library '" + lib_path + "'\n" + get_error_description());
throw load_error("Could not load library '" + lib + "'\n" + get_error_description());

#if !(defined(_WIN32) || defined(_WIN64))
m_fd = open(lib_path.c_str(), O_RDONLY);
m_fd = open(lib.c_str(), O_RDONLY);
if (m_fd < 0)
throw load_error("Could not open file '" + lib_path + "': " + strerror(errno));
throw load_error("Could not open file '" + lib + "': " + strerror(errno));
#endif
}

library::library(const std::string &lib_path, decorations decorations)
: library(lib_path.c_str(), decorations) {}

#if ((defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) || __cplusplus >= 201703L)
library::library(const std::filesystem::path &lib_path, decorations decorations)
: library(lib_path.string(), decorations) {}
Expand Down

0 comments on commit 93714c7

Please sign in to comment.