diff --git a/source/mijin/types/result.hpp b/source/mijin/types/result.hpp index 5dcef34..1f07975 100644 --- a/source/mijin/types/result.hpp +++ b/source/mijin/types/result.hpp @@ -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 diff --git a/source/mijin/util/os.cpp b/source/mijin/util/os.cpp index f483d3e..997b0d0 100644 --- a/source/mijin/util/os.cpp +++ b/source/mijin/util/os.cpp @@ -39,11 +39,18 @@ namespace mijin // public functions // -LibraryHandle openSharedLibrary(std::string_view libraryFile) noexcept +Result 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; diff --git a/source/mijin/util/os.hpp b/source/mijin/util/os.hpp index e02f0cd..b080e39 100644 --- a/source/mijin/util/os.hpp +++ b/source/mijin/util/os.hpp @@ -9,6 +9,7 @@ #include #include #include "../detect.hpp" +#include "../types/result.hpp" namespace mijin { @@ -62,7 +63,7 @@ public: constexpr operator bool() const noexcept { return static_cast(mHandle); } constexpr bool operator!() const noexcept { return !mHandle; } - [[nodiscard]] inline bool open(std::string_view libraryFile) noexcept; + [[nodiscard]] inline Result 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 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 SharedLibrary::open(std::string_view libraryFile) noexcept { close(); - mHandle = openSharedLibrary(libraryFile); - return static_cast(mHandle); + Result openResult = openSharedLibrary(libraryFile); + if (openResult.isSuccess()) + { + mHandle = openResult.getValue(); + return true; + } + return ResultError(openResult.getError()); } bool SharedLibrary::close() noexcept diff --git a/source/mijin/util/property.hpp b/source/mijin/util/property.hpp index b48731c..87d352f 100644 --- a/source/mijin/util/property.hpp +++ b/source/mijin/util/property.hpp @@ -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* operator->() const noexcept { return &get(); } [[nodiscard]] TGet get() const noexcept