Return result with more error information when opening a shared library fails.

This commit is contained in:
2024-07-29 21:48:04 +02:00
parent a9a85aecdf
commit f6f77f6dc1
4 changed files with 29 additions and 8 deletions

View File

@@ -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