116 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			116 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
 | 
						|
#pragma once
 | 
						|
 | 
						|
#if !defined(MIJIN_UTIL_OS_HPP_INCLUDED)
 | 
						|
#define MIJIN_UTIL_OS_HPP_INCLUDED 1
 | 
						|
 | 
						|
#include <csignal>
 | 
						|
#include <string>
 | 
						|
#include <string_view>
 | 
						|
#include <utility>
 | 
						|
#include "../detect.hpp"
 | 
						|
#include "../internal/common.hpp"
 | 
						|
#include "../types/result.hpp"
 | 
						|
 | 
						|
namespace mijin
 | 
						|
{
 | 
						|
 | 
						|
//
 | 
						|
// public defines
 | 
						|
//
 | 
						|
 | 
						|
//
 | 
						|
// public constants
 | 
						|
//
 | 
						|
 | 
						|
#if MIJIN_TARGET_OS == MIJIN_OS_LINUX
 | 
						|
inline const std::string_view LIBRARY_EXT = "so";
 | 
						|
#elif MIJIN_TARGET_OS == MIJIN_OS_WINDOWS
 | 
						|
inline const std::string_view LIBRARY_EXT = "dll";
 | 
						|
#else
 | 
						|
[[deprecated("OS not supported.")]]
 | 
						|
inline const int LIBRARY_EXT = 0;
 | 
						|
#endif
 | 
						|
 | 
						|
//
 | 
						|
// public types
 | 
						|
//
 | 
						|
 | 
						|
struct LibraryHandle
 | 
						|
{
 | 
						|
    void* data = nullptr;
 | 
						|
 | 
						|
    constexpr operator bool() const MIJIN_NOEXCEPT { return data != nullptr; }
 | 
						|
    constexpr bool operator!() const MIJIN_NOEXCEPT { return data == nullptr; }
 | 
						|
};
 | 
						|
 | 
						|
class SharedLibrary
 | 
						|
{
 | 
						|
private:
 | 
						|
    LibraryHandle mHandle;
 | 
						|
public:
 | 
						|
    SharedLibrary() MIJIN_NOEXCEPT = default;
 | 
						|
    SharedLibrary(const SharedLibrary&) = delete;
 | 
						|
    SharedLibrary(SharedLibrary&& other) MIJIN_NOEXCEPT : mHandle(std::exchange(other.mHandle, {})) {}
 | 
						|
    inline ~SharedLibrary() MIJIN_NOEXCEPT;
 | 
						|
 | 
						|
    SharedLibrary& operator=(const SharedLibrary&) = delete;
 | 
						|
    SharedLibrary& operator=(SharedLibrary&& other) MIJIN_NOEXCEPT
 | 
						|
    {
 | 
						|
        mHandle = std::exchange(other.mHandle, {});
 | 
						|
        return *this;
 | 
						|
    }
 | 
						|
 | 
						|
    constexpr operator bool() const MIJIN_NOEXCEPT { return static_cast<bool>(mHandle); }
 | 
						|
    constexpr bool operator!() const MIJIN_NOEXCEPT { return !mHandle; }
 | 
						|
 | 
						|
    [[nodiscard]] inline Result<bool> open(std::string_view libraryFile) MIJIN_NOEXCEPT;
 | 
						|
    inline bool close() MIJIN_NOEXCEPT;
 | 
						|
    [[nodiscard]] inline void* loadSymbol(const char* symbolName) MIJIN_NOEXCEPT;
 | 
						|
};
 | 
						|
 | 
						|
//
 | 
						|
// public functions
 | 
						|
//
 | 
						|
 | 
						|
[[nodiscard]] Result<LibraryHandle> openSharedLibrary(std::string_view libraryFile) MIJIN_NOEXCEPT;
 | 
						|
bool closeSharedLibrary(const LibraryHandle library) MIJIN_NOEXCEPT;
 | 
						|
[[nodiscard]] void* loadSymbolFromLibrary(const LibraryHandle library, const char* symbolName) MIJIN_NOEXCEPT;
 | 
						|
void setCurrentThreadName(const char* threadName) MIJIN_NOEXCEPT;
 | 
						|
 | 
						|
[[nodiscard]] std::string makeLibraryFilename(std::string_view libraryName) MIJIN_NOEXCEPT;
 | 
						|
 | 
						|
SharedLibrary::~SharedLibrary() MIJIN_NOEXCEPT
 | 
						|
{
 | 
						|
    close();
 | 
						|
}
 | 
						|
 | 
						|
Result<bool> SharedLibrary::open(std::string_view libraryFile) MIJIN_NOEXCEPT
 | 
						|
{
 | 
						|
    close();
 | 
						|
    Result<LibraryHandle> openResult = openSharedLibrary(libraryFile);
 | 
						|
    if (openResult.isSuccess())
 | 
						|
    {
 | 
						|
        mHandle = openResult.getValue();
 | 
						|
        return true;
 | 
						|
    }
 | 
						|
    return ResultError(openResult.getError());
 | 
						|
}
 | 
						|
 | 
						|
bool SharedLibrary::close() MIJIN_NOEXCEPT
 | 
						|
{
 | 
						|
    if (mHandle)
 | 
						|
    {
 | 
						|
        return closeSharedLibrary(std::exchange(mHandle, {}));
 | 
						|
    }
 | 
						|
    return false;
 | 
						|
}
 | 
						|
 | 
						|
void* SharedLibrary::loadSymbol(const char* symbolName) MIJIN_NOEXCEPT
 | 
						|
{
 | 
						|
    return loadSymbolFromLibrary(mHandle, symbolName);
 | 
						|
}
 | 
						|
} // namespace mijin
 | 
						|
 | 
						|
#endif // !defined(MIJIN_UTIL_OS_HPP_INCLUDED)
 |