Random code cleanup

This commit is contained in:
SirLynix
2023-06-05 18:05:16 +02:00
parent 1a55b550fb
commit b9c1559d97
10 changed files with 31 additions and 46 deletions

View File

@@ -73,7 +73,7 @@ namespace Nz
if (libraryPath.extension() != NAZARA_DYNLIB_EXTENSION)
libraryPath += NAZARA_DYNLIB_EXTENSION;
auto impl = std::make_unique<DynLibImpl>(this);
auto impl = std::make_unique<DynLibImpl>();
if (!impl->Load(libraryPath, &m_lastError))
{
NazaraError("Failed to load library: " + m_lastError);

View File

@@ -3,17 +3,11 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Posix/DynLibImpl.hpp>
#include <Nazara/Core/DynLib.hpp>
#include <dlfcn.h>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
DynLibImpl::DynLibImpl(DynLib*) :
m_handle(nullptr)
{
}
DynLibImpl::~DynLibImpl()
{
if (m_handle)
@@ -22,25 +16,19 @@ namespace Nz
DynLibFunc DynLibImpl::GetSymbol(const char* symbol, std::string* errorMessage) const
{
/*
Il n'est pas standard de cast un pointeur d'objet vers un pointeur de fonction.
Nous devons donc utiliser des techniques diaboliques venant du malin lui-même.. :D
Au moins ce n'est pas aussi terrible qu'un const_cast
-Lynix
*/
union
{
DynLibFunc func;
void* pointer;
} converter;
dlerror(); // Clear error flag
converter.pointer = dlsym(m_handle, symbol);
if (!converter.pointer)
void* ptr = dlsym(m_handle, symbol);
if (!ptr)
*errorMessage = dlerror();
return converter.func;
static_assert(sizeof(DynLibFunc) == sizeof(void*));
// poor man's std::bit_cast
DynLibFunc funcPtr;
std::memcpy(&funcPtr, &ptr, sizeof(funcPtr));
return funcPtr;
}
bool DynLibImpl::Load(const std::filesystem::path& libraryPath, std::string* errorMessage)
@@ -48,12 +36,12 @@ namespace Nz
dlerror(); // Clear error flag
m_handle = dlopen(libraryPath.generic_u8string().data(), RTLD_LAZY | RTLD_GLOBAL);
if (m_handle)
return true;
else
if (!m_handle)
{
*errorMessage = dlerror();
return false;
}
return true;
}
}

View File

@@ -8,6 +8,7 @@
#define NAZARA_CORE_POSIX_DYNLIBIMPL_HPP
#include <Nazara/Core/DynLib.hpp>
#include <NazaraUtils/MovablePtr.hpp>
#include <filesystem>
#include <string>
@@ -18,14 +19,19 @@ namespace Nz
class DynLibImpl
{
public:
DynLibImpl(DynLib* m_parent);
DynLibImpl() = default;
DynLibImpl(const DynLibImpl&) = delete;
DynLibImpl(DynLibImpl&&) noexcept = default;
~DynLibImpl();
DynLibFunc GetSymbol(const char* symbol, std::string* errorMessage) const;
bool Load(const std::filesystem::path& libraryPath, std::string* errorMessage);
DynLibImpl& operator=(const DynLibImpl&) = delete;
DynLibImpl& operator=(DynLibImpl&&) noexcept = default;
private:
void* m_handle;
MovablePtr<void> m_handle;
};
}

View File

@@ -3,7 +3,6 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Win32/DynLibImpl.hpp>
#include <Nazara/Core/DynLib.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/StringExt.hpp>
#include <memory>
@@ -11,11 +10,6 @@
namespace Nz
{
DynLibImpl::DynLibImpl(DynLib*) :
m_handle(nullptr)
{
}
DynLibImpl::~DynLibImpl()
{
if (m_handle)
@@ -24,7 +18,7 @@ namespace Nz
DynLibFunc DynLibImpl::GetSymbol(const char* symbol, std::string* errorMessage) const
{
DynLibFunc sym = reinterpret_cast<DynLibFunc>(GetProcAddress(m_handle, symbol));
DynLibFunc sym = reinterpret_cast<DynLibFunc>(::GetProcAddress(m_handle, symbol));
if (!sym)
*errorMessage = Error::GetLastSystemError();

View File

@@ -9,27 +9,28 @@
#include <NazaraUtils/Prerequisites.hpp>
#include <Nazara/Core/DynLib.hpp>
#include <NazaraUtils/MovablePtr.hpp>
#include <filesystem>
#include <windows.h>
#include <Windows.h>
namespace Nz
{
class DynLibImpl
{
public:
DynLibImpl(DynLib* m_parent);
DynLibImpl() = default;
DynLibImpl(const DynLibImpl&) = delete;
DynLibImpl(DynLibImpl&&) = delete; ///TODO?
DynLibImpl(DynLibImpl&&) noexcept = default;
~DynLibImpl();
DynLibFunc GetSymbol(const char* symbol, std::string* errorMessage) const;
bool Load(const std::filesystem::path& libraryPath, std::string* errorMessage);
DynLibImpl& operator=(const DynLibImpl&) = delete;
DynLibImpl& operator=(DynLibImpl&&) = delete; ///TODO?
DynLibImpl& operator=(DynLibImpl&&) noexcept = default;
private:
HMODULE m_handle;
MovablePtr<std::remove_pointer_t<HMODULE>> m_handle;
};
}