Core: Move platform-specific code to PlatformImpl namespace

This commit is contained in:
SirLynix 2024-01-22 16:49:21 +01:00
parent 3557040246
commit 373309d6d9
22 changed files with 45 additions and 42 deletions

View File

@ -28,7 +28,10 @@ namespace Nz
{ {
using DynLibFunc = void (*)(void); // "Generic" type of pointer to function using DynLibFunc = void (*)(void); // "Generic" type of pointer to function
class DynLibImpl; namespace PlatformImpl
{
class DynLibImpl;
}
class NAZARA_CORE_API DynLib class NAZARA_CORE_API DynLib
{ {
@ -51,7 +54,7 @@ namespace Nz
private: private:
mutable std::string m_lastError; mutable std::string m_lastError;
std::unique_ptr<DynLibImpl> m_impl; std::unique_ptr<PlatformImpl::DynLibImpl> m_impl;
}; };
} }

View File

@ -19,7 +19,10 @@
namespace Nz namespace Nz
{ {
class FileImpl; namespace PlatformImpl
{
class FileImpl;
}
class NAZARA_CORE_API File : public Stream class NAZARA_CORE_API File : public Stream
{ {
@ -70,7 +73,7 @@ namespace Nz
std::size_t WriteBlock(const void* buffer, std::size_t size) override; std::size_t WriteBlock(const void* buffer, std::size_t size) override;
std::filesystem::path m_filePath; std::filesystem::path m_filePath;
std::unique_ptr<FileImpl> m_impl; std::unique_ptr<PlatformImpl::FileImpl> m_impl;
}; };
NAZARA_CORE_API bool HashAppend(AbstractHash& hash, const File& originalFile); NAZARA_CORE_API bool HashAppend(AbstractHash& hash, const File& originalFile);

View File

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

View File

@ -188,7 +188,7 @@ namespace Nz
return true; return true;
} }
std::unique_ptr<FileImpl> impl = std::make_unique<FileImpl>(this); std::unique_ptr<PlatformImpl::FileImpl> impl = std::make_unique<PlatformImpl::FileImpl>(this);
if (!impl->Open(m_filePath, openMode)) if (!impl->Open(m_filePath, openMode))
{ {
ErrorFlags flags(ErrorMode::Silent); // Silent by default ErrorFlags flags(ErrorMode::Silent); // Silent by default
@ -240,7 +240,7 @@ namespace Nz
if (filePath.empty()) if (filePath.empty())
return false; return false;
std::unique_ptr<FileImpl> impl = std::make_unique<FileImpl>(this); std::unique_ptr<PlatformImpl::FileImpl> impl = std::make_unique<PlatformImpl::FileImpl>(this);
if (!impl->Open(filePath, m_openMode)) if (!impl->Open(filePath, m_openMode))
{ {
NazaraErrorFmt("failed to open new file; {0}", Error::GetLastSystemError()); NazaraErrorFmt("failed to open new file; {0}", Error::GetLastSystemError());

View File

@ -117,31 +117,31 @@ namespace Nz
void HardwareInfo::Cpuid(UInt32 functionId, UInt32 subFunctionId, UInt32 result[4]) void HardwareInfo::Cpuid(UInt32 functionId, UInt32 subFunctionId, UInt32 result[4])
{ {
return HardwareInfoImpl::Cpuid(functionId, subFunctionId, result); return PlatformImpl::HardwareInfoImpl::Cpuid(functionId, subFunctionId, result);
} }
bool HardwareInfo::IsCpuidSupported() bool HardwareInfo::IsCpuidSupported()
{ {
return HardwareInfoImpl::IsCpuidSupported(); return PlatformImpl::HardwareInfoImpl::IsCpuidSupported();
} }
void HardwareInfo::FetchCPUInfo() void HardwareInfo::FetchCPUInfo()
{ {
NAZARA_USE_ANONYMOUS_NAMESPACE NAZARA_USE_ANONYMOUS_NAMESPACE
m_cpuThreadCount = std::max(HardwareInfoImpl::GetProcessorCount(), 1U); m_cpuThreadCount = std::max(PlatformImpl::HardwareInfoImpl::GetProcessorCount(), 1U);
m_cpuCapabilities.fill(false); m_cpuCapabilities.fill(false);
m_cpuVendor = ProcessorVendor::Unknown; m_cpuVendor = ProcessorVendor::Unknown;
std::strcpy(m_cpuBrandString.data(), "CPU from unknown vendor - cpuid not supported"); std::strcpy(m_cpuBrandString.data(), "CPU from unknown vendor - cpuid not supported");
if (!HardwareInfoImpl::IsCpuidSupported()) if (!PlatformImpl::HardwareInfoImpl::IsCpuidSupported())
return; return;
// To begin, we get the id of the constructor and the id of maximal functions supported by the CPUID // To begin, we get the id of the constructor and the id of maximal functions supported by the CPUID
std::array<UInt32, 4> registers; std::array<UInt32, 4> registers;
HardwareInfoImpl::Cpuid(0, 0, registers.data()); PlatformImpl::HardwareInfoImpl::Cpuid(0, 0, registers.data());
UInt32& eax = registers[0]; UInt32& eax = registers[0];
UInt32& ebx = registers[1]; UInt32& ebx = registers[1];
@ -159,7 +159,7 @@ namespace Nz
if (eax >= 1) if (eax >= 1)
{ {
// Retrieval of certain capacities of the processor (ECX and EDX, function 1) // Retrieval of certain capacities of the processor (ECX and EDX, function 1)
HardwareInfoImpl::Cpuid(1, 0, registers.data()); PlatformImpl::HardwareInfoImpl::Cpuid(1, 0, registers.data());
m_cpuCapabilities[ProcessorCap::AES] = (ecx & (1U << 25)) != 0; m_cpuCapabilities[ProcessorCap::AES] = (ecx & (1U << 25)) != 0;
m_cpuCapabilities[ProcessorCap::AVX] = (ecx & (1U << 28)) != 0; m_cpuCapabilities[ProcessorCap::AVX] = (ecx & (1U << 28)) != 0;
@ -176,13 +176,13 @@ namespace Nz
} }
// Retrieval of biggest extended function handled (EAX, function 0x80000000) // Retrieval of biggest extended function handled (EAX, function 0x80000000)
HardwareInfoImpl::Cpuid(0x80000000, 0, registers.data()); PlatformImpl::HardwareInfoImpl::Cpuid(0x80000000, 0, registers.data());
UInt32 maxSupportedExtendedFunction = eax; UInt32 maxSupportedExtendedFunction = eax;
if (maxSupportedExtendedFunction >= 0x80000001) if (maxSupportedExtendedFunction >= 0x80000001)
{ {
// Retrieval of extended capabilities of the processor (ECX and EDX, function 0x80000001) // Retrieval of extended capabilities of the processor (ECX and EDX, function 0x80000001)
HardwareInfoImpl::Cpuid(0x80000001, 0, registers.data()); PlatformImpl::HardwareInfoImpl::Cpuid(0x80000001, 0, registers.data());
m_cpuCapabilities[ProcessorCap::x64] = (edx & (1U << 29)) != 0; // Support of 64bits, doesn't mean executable is 64bits m_cpuCapabilities[ProcessorCap::x64] = (edx & (1U << 29)) != 0; // Support of 64bits, doesn't mean executable is 64bits
m_cpuCapabilities[ProcessorCap::FMA4] = (ecx & (1U << 16)) != 0; m_cpuCapabilities[ProcessorCap::FMA4] = (ecx & (1U << 16)) != 0;
@ -195,7 +195,7 @@ namespace Nz
char* ptr = &m_cpuBrandString[0]; char* ptr = &m_cpuBrandString[0];
for (UInt32 code = 0x80000002; code <= 0x80000004; ++code) for (UInt32 code = 0x80000002; code <= 0x80000004; ++code)
{ {
HardwareInfoImpl::Cpuid(code, 0, registers.data()); PlatformImpl::HardwareInfoImpl::Cpuid(code, 0, registers.data());
std::memcpy(ptr, &registers[0], 4*sizeof(UInt32)); // We add the 16 bytes to the string std::memcpy(ptr, &registers[0], 4*sizeof(UInt32)); // We add the 16 bytes to the string
ptr += 4 * sizeof(UInt32); ptr += 4 * sizeof(UInt32);
@ -208,6 +208,6 @@ namespace Nz
void HardwareInfo::FetchMemoryInfo() void HardwareInfo::FetchMemoryInfo()
{ {
m_systemTotalMemory = HardwareInfoImpl::GetTotalMemory(); m_systemTotalMemory = PlatformImpl::HardwareInfoImpl::GetTotalMemory();
} }
} }

View File

@ -9,7 +9,7 @@
#include <cstring> #include <cstring>
#include <Nazara/Core/Debug.hpp> #include <Nazara/Core/Debug.hpp>
namespace Nz namespace Nz::PlatformImpl
{ {
DynLibImpl::~DynLibImpl() DynLibImpl::~DynLibImpl()
{ {

View File

@ -12,10 +12,8 @@
#include <filesystem> #include <filesystem>
#include <string> #include <string>
namespace Nz namespace Nz::PlatformImpl
{ {
class String;
class DynLibImpl class DynLibImpl
{ {
public: public:

View File

@ -14,7 +14,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <Nazara/Core/Debug.hpp> #include <Nazara/Core/Debug.hpp>
namespace Nz namespace Nz::PlatformImpl
{ {
FileImpl::FileImpl(const File* parent) : FileImpl::FileImpl(const File* parent) :
m_fileDescriptor(-1), m_fileDescriptor(-1),

View File

@ -37,8 +37,10 @@
namespace Nz namespace Nz
{ {
class File; class File;
class String; }
namespace Nz::PlatformImpl
{
class FileImpl class FileImpl
{ {
public: public:

View File

@ -7,7 +7,7 @@
#include <unistd.h> #include <unistd.h>
#include <Nazara/Core/Debug.hpp> #include <Nazara/Core/Debug.hpp>
namespace Nz namespace Nz::PlatformImpl
{ {
void HardwareInfoImpl::Cpuid(UInt32 functionId, UInt32 subFunctionId, UInt32 registers[4]) void HardwareInfoImpl::Cpuid(UInt32 functionId, UInt32 subFunctionId, UInt32 registers[4])
{ {

View File

@ -9,7 +9,7 @@
#include <NazaraUtils/Prerequisites.hpp> #include <NazaraUtils/Prerequisites.hpp>
namespace Nz namespace Nz::PlatformImpl
{ {
class HardwareInfoImpl class HardwareInfoImpl
{ {

View File

@ -6,7 +6,7 @@
#include <time.h> #include <time.h>
#include <Nazara/Core/Debug.hpp> #include <Nazara/Core/Debug.hpp>
namespace Nz namespace Nz::PlatformImpl
{ {
bool InitializeHighPrecisionTimer() bool InitializeHighPrecisionTimer()
{ {

View File

@ -10,7 +10,7 @@
#include <NazaraUtils/Prerequisites.hpp> #include <NazaraUtils/Prerequisites.hpp>
#include <Nazara/Core/Time.hpp> #include <Nazara/Core/Time.hpp>
namespace Nz namespace Nz::PlatformImpl
{ {
bool InitializeHighPrecisionTimer(); bool InitializeHighPrecisionTimer();
Time GetElapsedNanosecondsImpl(); Time GetElapsedNanosecondsImpl();

View File

@ -22,15 +22,15 @@ namespace Nz
{ {
Time GetElapsedNanosecondsFirstRun() Time GetElapsedNanosecondsFirstRun()
{ {
if (InitializeHighPrecisionTimer()) if (PlatformImpl::InitializeHighPrecisionTimer())
GetElapsedNanoseconds = GetElapsedNanosecondsImpl; GetElapsedNanoseconds = PlatformImpl::GetElapsedNanosecondsImpl;
else else
GetElapsedNanoseconds = GetElapsedMillisecondsImpl; GetElapsedNanoseconds = PlatformImpl::GetElapsedMillisecondsImpl;
return GetElapsedNanoseconds(); return GetElapsedNanoseconds();
} }
} }
GetElapsedTimeFunction GetElapsedMilliseconds = GetElapsedMillisecondsImpl; GetElapsedTimeFunction GetElapsedMilliseconds = PlatformImpl::GetElapsedMillisecondsImpl;
GetElapsedTimeFunction GetElapsedNanoseconds = NAZARA_ANONYMOUS_NAMESPACE_PREFIX(GetElapsedNanosecondsFirstRun); GetElapsedTimeFunction GetElapsedNanoseconds = NAZARA_ANONYMOUS_NAMESPACE_PREFIX(GetElapsedNanosecondsFirstRun);
} }

View File

@ -9,7 +9,7 @@
#include <memory> #include <memory>
#include <Nazara/Core/Debug.hpp> #include <Nazara/Core/Debug.hpp>
namespace Nz namespace Nz::PlatformImpl
{ {
DynLibImpl::~DynLibImpl() DynLibImpl::~DynLibImpl()
{ {

View File

@ -13,7 +13,7 @@
#include <filesystem> #include <filesystem>
#include <Windows.h> #include <Windows.h>
namespace Nz namespace Nz::PlatformImpl
{ {
class DynLibImpl class DynLibImpl
{ {

View File

@ -11,7 +11,7 @@
#include <memory> #include <memory>
#include <Nazara/Core/Debug.hpp> #include <Nazara/Core/Debug.hpp>
namespace Nz namespace Nz::PlatformImpl
{ {
FileImpl::FileImpl(const File* parent) : FileImpl::FileImpl(const File* parent) :
m_endOfFile(false), m_endOfFile(false),

View File

@ -12,11 +12,8 @@
#include <ctime> #include <ctime>
#include <Windows.h> #include <Windows.h>
namespace Nz namespace Nz::PlatformImpl
{ {
class File;
class String;
class FileImpl class FileImpl
{ {
public: public:

View File

@ -12,7 +12,7 @@
#include <Nazara/Core/Debug.hpp> #include <Nazara/Core/Debug.hpp>
namespace Nz namespace Nz::PlatformImpl
{ {
void HardwareInfoImpl::Cpuid(UInt32 functionId, UInt32 subFunctionId, UInt32 registers[4]) void HardwareInfoImpl::Cpuid(UInt32 functionId, UInt32 subFunctionId, UInt32 registers[4])
{ {

View File

@ -9,7 +9,7 @@
#include <NazaraUtils/Prerequisites.hpp> #include <NazaraUtils/Prerequisites.hpp>
namespace Nz namespace Nz::PlatformImpl
{ {
class HardwareInfoImpl class HardwareInfoImpl
{ {

View File

@ -8,7 +8,7 @@
#include <Windows.h> #include <Windows.h>
#include <Nazara/Core/Debug.hpp> #include <Nazara/Core/Debug.hpp>
namespace Nz namespace Nz::PlatformImpl
{ {
namespace NAZARA_ANONYMOUS_NAMESPACE namespace NAZARA_ANONYMOUS_NAMESPACE
{ {

View File

@ -10,7 +10,7 @@
#include <NazaraUtils/Prerequisites.hpp> #include <NazaraUtils/Prerequisites.hpp>
#include <Nazara/Core/Time.hpp> #include <Nazara/Core/Time.hpp>
namespace Nz namespace Nz::PlatformImpl
{ {
bool InitializeHighPrecisionTimer(); bool InitializeHighPrecisionTimer();
Time GetElapsedNanosecondsImpl(); Time GetElapsedNanosecondsImpl();