Return result with more error information when opening a shared library fails.
This commit is contained in:
parent
a9a85aecdf
commit
f6f77f6dc1
@ -61,6 +61,14 @@ public:
|
||||
struct ResultError
|
||||
{
|
||||
std::string message;
|
||||
|
||||
ResultError() : message("Unknown error") {}
|
||||
ResultError(const ResultError&) = default;
|
||||
ResultError(ResultError&&) noexcept = default;
|
||||
ResultError(std::string msg) noexcept : message(std::move(msg)) {}
|
||||
|
||||
ResultError& operator=(const ResultError&) = default;
|
||||
ResultError& operator=(ResultError&&) noexcept = default;
|
||||
};
|
||||
|
||||
template<typename TSuccess>
|
||||
|
@ -39,11 +39,18 @@ namespace mijin
|
||||
// public functions
|
||||
//
|
||||
|
||||
LibraryHandle openSharedLibrary(std::string_view libraryFile) noexcept
|
||||
Result<LibraryHandle> openSharedLibrary(std::string_view libraryFile) noexcept
|
||||
{
|
||||
#if MIJIN_TARGET_OS == MIJIN_OS_LINUX
|
||||
dlerror();
|
||||
|
||||
const fs::path libraryPath = fs::absolute(libraryFile);
|
||||
return {.data = dlopen(libraryPath.c_str(), RTLD_NOW)};
|
||||
void* ptr = dlopen(libraryPath.c_str(), RTLD_NOW);
|
||||
if (ptr == nullptr)
|
||||
{
|
||||
return ResultError(dlerror());
|
||||
}
|
||||
return LibraryHandle{.data = dlopen(libraryPath.c_str(), RTLD_NOW)};
|
||||
#elif MIJIN_TARGET_OS == MIJIN_OS_WINDOWS
|
||||
// TODO
|
||||
(void) libraryFile;
|
||||
|
@ -9,6 +9,7 @@
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
#include "../detect.hpp"
|
||||
#include "../types/result.hpp"
|
||||
|
||||
namespace mijin
|
||||
{
|
||||
@ -62,7 +63,7 @@ public:
|
||||
constexpr operator bool() const noexcept { return static_cast<bool>(mHandle); }
|
||||
constexpr bool operator!() const noexcept { return !mHandle; }
|
||||
|
||||
[[nodiscard]] inline bool open(std::string_view libraryFile) noexcept;
|
||||
[[nodiscard]] inline Result<bool> open(std::string_view libraryFile) noexcept;
|
||||
inline bool close() noexcept;
|
||||
[[nodiscard]] inline void* loadSymbol(const char* symbolName) noexcept;
|
||||
};
|
||||
@ -71,7 +72,7 @@ public:
|
||||
// public functions
|
||||
//
|
||||
|
||||
[[nodiscard]] LibraryHandle openSharedLibrary(std::string_view libraryFile) noexcept;
|
||||
[[nodiscard]] Result<LibraryHandle> openSharedLibrary(std::string_view libraryFile) noexcept;
|
||||
bool closeSharedLibrary(const LibraryHandle library) noexcept;
|
||||
[[nodiscard]] void* loadSymbolFromLibrary(const LibraryHandle library, const char* symbolName) noexcept;
|
||||
void setCurrentThreadName(const char* threadName) noexcept;
|
||||
@ -83,11 +84,16 @@ SharedLibrary::~SharedLibrary() noexcept
|
||||
close();
|
||||
}
|
||||
|
||||
bool SharedLibrary::open(std::string_view libraryFile) noexcept
|
||||
Result<bool> SharedLibrary::open(std::string_view libraryFile) noexcept
|
||||
{
|
||||
close();
|
||||
mHandle = openSharedLibrary(libraryFile);
|
||||
return static_cast<bool>(mHandle);
|
||||
Result<LibraryHandle> openResult = openSharedLibrary(libraryFile);
|
||||
if (openResult.isSuccess())
|
||||
{
|
||||
mHandle = openResult.getValue();
|
||||
return true;
|
||||
}
|
||||
return ResultError(openResult.getError());
|
||||
}
|
||||
|
||||
bool SharedLibrary::close() noexcept
|
||||
|
@ -80,7 +80,7 @@ public:
|
||||
Property(Property&&) noexcept = default;
|
||||
|
||||
Property& operator=(Property&&) noexcept = default;
|
||||
PropertyProxy operator*() const noexcept { return PropertyProxy(this); }
|
||||
PropertyProxy operator*() noexcept { return PropertyProxy(this); }
|
||||
std::remove_reference_t<TGet>* operator->() const noexcept { return &get(); }
|
||||
|
||||
[[nodiscard]] TGet get() const noexcept
|
||||
|
Loading…
x
Reference in New Issue
Block a user