Core/Serialization: Add correct endianness handling
Core/File: Remove endianness handling Former-commit-id: 6f7bba52057f36c507a024f7a7ea873658a3cfd3
This commit is contained in:
@@ -29,15 +29,15 @@ namespace Nz
|
||||
template<typename T>
|
||||
struct TypeTag {};
|
||||
|
||||
inline bool Serialize(OutputStream* output, bool value);
|
||||
inline bool Serialize(OutputStream* output, bool value, Endianness dataEndianness);
|
||||
|
||||
template<typename T>
|
||||
std::enable_if_t<std::is_arithmetic<T>::value, bool> Serialize(OutputStream* output, T value);
|
||||
std::enable_if_t<std::is_arithmetic<T>::value, bool> Serialize(OutputStream* output, T value, Endianness dataEndianness);
|
||||
|
||||
inline bool Unserialize(InputStream* input, bool* value);
|
||||
inline bool Unserialize(InputStream* input, bool* value, Endianness dataEndianness);
|
||||
|
||||
template<typename T>
|
||||
std::enable_if_t<std::is_arithmetic<T>::value, bool> Unserialize(InputStream* input, T* value);
|
||||
std::enable_if_t<std::is_arithmetic<T>::value, bool> Unserialize(InputStream* input, T* value, Endianness dataEndianness);
|
||||
}
|
||||
|
||||
#include <Nazara/Core/Algorithm.inl>
|
||||
|
||||
@@ -77,21 +77,25 @@ namespace Nz
|
||||
seed = static_cast<std::size_t>(b * kMul);
|
||||
}
|
||||
|
||||
inline bool Serialize(OutputStream* output, bool value)
|
||||
inline bool Serialize(OutputStream* output, bool value, Endianness dataEndianness)
|
||||
{
|
||||
NazaraAssert(output, "Invalid stream");
|
||||
NazaraUnused(dataEndianness);
|
||||
|
||||
///TODO: Handle bits writing (Serializing 8 bits should only use one byte)
|
||||
UInt8 buffer = (value) ? 1 : 0;
|
||||
return output->Write(&buffer, 1) == 1;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::enable_if_t<std::is_arithmetic<T>::value, bool> Serialize(OutputStream* output, T value)
|
||||
std::enable_if_t<std::is_arithmetic<T>::value, bool> Serialize(OutputStream* output, T value, Endianness dataEndianness)
|
||||
{
|
||||
NazaraAssert(output, "Invalid stream");
|
||||
|
||||
if (output->Write(&value, sizeof(T)) == sizeof(T))
|
||||
{
|
||||
// Write down everything as little endian
|
||||
#if NAZARA_BIG_ENDIAN
|
||||
SwapBytes(&value, sizeof(T));
|
||||
#endif
|
||||
if (dataEndianness != Endianness_Unknown && dataEndianness != GetPlatformEndianness())
|
||||
SwapBytes(&value, sizeof(T));
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -99,9 +103,11 @@ namespace Nz
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool Unserialize(InputStream* input, bool* value)
|
||||
inline bool Unserialize(InputStream* input, bool* value, Endianness dataEndianness)
|
||||
{
|
||||
NazaraAssert(value, "Invalid pointer");
|
||||
NazaraAssert(input, "Invalid stream");
|
||||
NazaraAssert(value, "Invalid data pointer");
|
||||
NazaraUnused(dataEndianness);
|
||||
|
||||
UInt8 buffer;
|
||||
if (input->Read(&buffer, 1) == 1)
|
||||
@@ -114,16 +120,15 @@ namespace Nz
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::enable_if_t<std::is_arithmetic<T>::value, bool> Unserialize(InputStream* input, T* value)
|
||||
std::enable_if_t<std::is_arithmetic<T>::value, bool> Unserialize(InputStream* input, T* value, Endianness dataEndianness)
|
||||
{
|
||||
NazaraAssert(value, "Invalid pointer");
|
||||
NazaraAssert(input, "Invalid stream");
|
||||
NazaraAssert(value, "Invalid data pointer");
|
||||
|
||||
if (input->Read(value, sizeof(T)) == sizeof(T))
|
||||
{
|
||||
// Write down everything as little endian
|
||||
#if NAZARA_BIG_ENDIAN
|
||||
SwapBytes(&value, sizeof(T));
|
||||
#endif
|
||||
if (dataEndianness != Endianness_Unknown && dataEndianness != GetPlatformEndianness())
|
||||
SwapBytes(&value, sizeof(T));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
inline Endianness GetPlatformEndianness();
|
||||
inline constexpr Endianness GetPlatformEndianness();
|
||||
inline void SwapBytes(void* buffer, unsigned int size);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
inline Endianness GetPlatformEndianness()
|
||||
inline constexpr Endianness GetPlatformEndianness()
|
||||
{
|
||||
#if defined(NAZARA_BIG_ENDIAN)
|
||||
return Endianness_BigEndian;
|
||||
|
||||
@@ -64,7 +64,6 @@ namespace Nz
|
||||
bool Open(const String& filePath, unsigned int openMode = OpenMode_Current);
|
||||
|
||||
std::size_t Read(void* buffer, std::size_t size) override;
|
||||
std::size_t Read(void* buffer, std::size_t typeSize, unsigned int count);
|
||||
bool Rename(const String& newFilePath);
|
||||
|
||||
bool SetCursorPos(CursorPosition pos, Int64 offset = 0);
|
||||
@@ -74,7 +73,6 @@ namespace Nz
|
||||
|
||||
using OutputStream::Write;
|
||||
std::size_t Write(const void* buffer, std::size_t size) override;
|
||||
std::size_t Write(const void* buffer, std::size_t typeSize, unsigned int count);
|
||||
|
||||
File& operator=(const String& filePath);
|
||||
File& operator=(const File&) = delete;
|
||||
@@ -99,7 +97,6 @@ namespace Nz
|
||||
private:
|
||||
NazaraMutexAttrib(m_mutex, mutable)
|
||||
|
||||
Endianness m_endianness;
|
||||
String m_filePath;
|
||||
FileImpl* m_impl;
|
||||
};
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace Nz
|
||||
template<typename T>
|
||||
InputStream& InputStream::operator>>(T& value)
|
||||
{
|
||||
if (!Unserialize(this, &value))
|
||||
if (!Unserialize(this, &value, m_dataEndianness))
|
||||
NazaraError("Failed to unserialize value");
|
||||
|
||||
return *this;
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace Nz
|
||||
template<typename T>
|
||||
OutputStream& OutputStream::operator<<(const T& value)
|
||||
{
|
||||
if (!Serialize(this, value))
|
||||
if (!Serialize(this, value, m_dataEndianness))
|
||||
NazaraError("Failed to serialize value");
|
||||
|
||||
return *this;
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#define NAZARA_STREAM_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/Endianness.hpp>
|
||||
#include <Nazara/Core/Enums.hpp>
|
||||
|
||||
namespace Nz
|
||||
@@ -24,6 +25,7 @@ namespace Nz
|
||||
virtual UInt64 GetCursorPos() const = 0;
|
||||
virtual String GetDirectory() const;
|
||||
virtual String GetPath() const;
|
||||
inline Endianness GetDataEndianness() const;
|
||||
inline UInt32 GetOpenMode() const;
|
||||
inline UInt32 GetStreamOptions() const;
|
||||
|
||||
@@ -31,14 +33,16 @@ namespace Nz
|
||||
inline bool IsWritable() const;
|
||||
|
||||
virtual bool SetCursorPos(UInt64 offset) = 0;
|
||||
void SetStreamOptions(UInt32 options);
|
||||
inline void SetDataEndianness(Endianness endiannes);
|
||||
inline void SetStreamOptions(UInt32 options);
|
||||
|
||||
Stream& operator=(const Stream&) = default;
|
||||
Stream& operator=(Stream&&) = default;
|
||||
|
||||
protected:
|
||||
inline Stream(UInt32 openMode);
|
||||
|
||||
|
||||
Endianness m_dataEndianness;
|
||||
UInt32 m_openMode;
|
||||
UInt32 m_streamOptions;
|
||||
};
|
||||
|
||||
@@ -5,11 +5,17 @@
|
||||
namespace Nz
|
||||
{
|
||||
inline Stream::Stream(UInt32 openMode) :
|
||||
m_dataEndianness(Endianness_Unknown),
|
||||
m_openMode(openMode),
|
||||
m_streamOptions(0)
|
||||
{
|
||||
}
|
||||
|
||||
inline Endianness Stream::GetDataEndianness() const
|
||||
{
|
||||
return m_dataEndianness;
|
||||
}
|
||||
|
||||
inline UInt32 Stream::GetOpenMode() const
|
||||
{
|
||||
return m_openMode;
|
||||
@@ -30,6 +36,11 @@ namespace Nz
|
||||
return m_openMode & OpenMode_ReadWrite || m_openMode & OpenMode_WriteOnly;
|
||||
}
|
||||
|
||||
inline void Stream::SetDataEndianness(Endianness endiannes)
|
||||
{
|
||||
m_dataEndianness = endiannes;
|
||||
}
|
||||
|
||||
inline void Stream::SetStreamOptions(UInt32 options)
|
||||
{
|
||||
m_streamOptions = options;
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#define NAZARA_STRING_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/Endianness.hpp>
|
||||
#include <atomic>
|
||||
#include <iosfwd>
|
||||
#include <memory>
|
||||
@@ -326,8 +327,8 @@ namespace Nz
|
||||
};
|
||||
|
||||
inline bool HashAppend(AbstractHash* hash, const String& string);
|
||||
NAZARA_CORE_API bool Serialize(OutputStream* output, const String& string);
|
||||
NAZARA_CORE_API bool Unserialize(InputStream* input, String* string);
|
||||
NAZARA_CORE_API bool Serialize(OutputStream* output, const String& string, Endianness dataEndianness);
|
||||
NAZARA_CORE_API bool Unserialize(InputStream* input, String* string, Endianness dataEndianness);
|
||||
}
|
||||
|
||||
namespace std
|
||||
|
||||
Reference in New Issue
Block a user