Rework Serialization functions
add name and handle more types
This commit is contained in:
@@ -38,9 +38,6 @@ namespace Nz
|
||||
|
||||
inline bool HashAppend(AbstractHash* hash, std::string_view v);
|
||||
|
||||
template<typename T>
|
||||
std::enable_if_t<std::is_arithmetic<T>::value, bool> Unserialize(SerializationContext& context, T* value, TypeTag<T>);
|
||||
|
||||
// Vertex processing
|
||||
class Joint;
|
||||
struct VertexStruct_XYZ_Normal_UV_Tangent;
|
||||
|
||||
54
include/Nazara/Core/BinarySerialization.hpp
Normal file
54
include/Nazara/Core/BinarySerialization.hpp
Normal file
@@ -0,0 +1,54 @@
|
||||
#pragma once
|
||||
|
||||
#include <Nazara/Core/Serialization.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
|
||||
struct NAZARA_CORE_API BinarySerializationContext final
|
||||
: public SerializationContext
|
||||
{
|
||||
public:
|
||||
bool PushObject(std::string_view name) override;
|
||||
bool PopObject() override;
|
||||
bool PushArray(std::string_view name) override;
|
||||
bool PopArray() override;
|
||||
|
||||
bool Write(std::string_view name, bool value) override;
|
||||
bool Write(std::string_view name, Nz::Int8 value) override;
|
||||
bool Write(std::string_view name, Nz::Int16 value) override;
|
||||
bool Write(std::string_view name, Nz::Int32 value) override;
|
||||
bool Write(std::string_view name, Nz::Int64 value) override;
|
||||
bool Write(std::string_view name, Nz::UInt8 value) override;
|
||||
bool Write(std::string_view name, Nz::UInt16 value) override;
|
||||
bool Write(std::string_view name, Nz::UInt32 value) override;
|
||||
bool Write(std::string_view name, Nz::UInt64 value) override;
|
||||
bool Write(std::string_view name, float value) override;
|
||||
bool Write(std::string_view name, double value) override;
|
||||
bool Write(std::string_view name, const std::string& value) override;
|
||||
|
||||
bool Read(std::string_view name, bool* value) override;
|
||||
bool Read(std::string_view name, Nz::Int8* value) override;
|
||||
bool Read(std::string_view name, Nz::Int16* value) override;
|
||||
bool Read(std::string_view name, Nz::Int32* value) override;
|
||||
bool Read(std::string_view name, Nz::Int64* value) override;
|
||||
bool Read(std::string_view name, Nz::UInt8* value) override;
|
||||
bool Read(std::string_view name, Nz::UInt16* value) override;
|
||||
bool Read(std::string_view name, Nz::UInt32* value) override;
|
||||
bool Read(std::string_view name, Nz::UInt64* value) override;
|
||||
bool Read(std::string_view name, float* value) override;
|
||||
bool Read(std::string_view name, double* value) override;
|
||||
bool Read(std::string_view name, std::string* value) override;
|
||||
|
||||
MovablePtr<Stream> stream;
|
||||
Endianness endianness = Endianness::BigEndian; //< Default to Big Endian encoding
|
||||
UInt8 readBitPos = 8; //< 8 means no bit is currently read
|
||||
UInt8 readByte; //< Undefined value, will be initialized at the first bit read
|
||||
UInt8 writeBitPos = 8; //< 8 means no bit is currently wrote
|
||||
UInt8 writeByte; //< Undefined value, will be initialized at the first bit write
|
||||
|
||||
void FlushBits();
|
||||
void ResetReadBitPosition();
|
||||
void ResetWriteBitPosition();
|
||||
};
|
||||
}
|
||||
@@ -10,7 +10,7 @@
|
||||
#include <NazaraUtils/Prerequisites.hpp>
|
||||
#include <Nazara/Core/Enums.hpp>
|
||||
#include <Nazara/Core/Export.hpp>
|
||||
#include <Nazara/Core/Serialization.hpp>
|
||||
#include <Nazara/Core/BinarySerialization.hpp>
|
||||
#include <memory>
|
||||
|
||||
namespace Nz
|
||||
@@ -60,7 +60,7 @@ namespace Nz
|
||||
virtual void OnEmptyStream();
|
||||
|
||||
std::unique_ptr<Stream> m_ownedStream;
|
||||
SerializationContext m_context;
|
||||
BinarySerializationContext m_context;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -87,7 +87,7 @@ namespace Nz
|
||||
{
|
||||
m_context.writeBitPos = 8; //< To prevent Serialize to flush bits itself
|
||||
|
||||
if (!Serialize(m_context, m_context.writeByte))
|
||||
if (!Serialize(m_context, "bytes", m_context.writeByte))
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -167,7 +167,7 @@ namespace Nz
|
||||
if (!m_context.stream)
|
||||
OnEmptyStream();
|
||||
|
||||
if (!Unserialize(m_context, &value))
|
||||
if (!Unserialize(m_context, "", &value))
|
||||
NazaraError("failed to serialize value");
|
||||
|
||||
return *this;
|
||||
@@ -188,7 +188,7 @@ namespace Nz
|
||||
if (!m_context.stream)
|
||||
OnEmptyStream();
|
||||
|
||||
if (!Serialize(m_context, value))
|
||||
if (!Serialize(m_context, "", value))
|
||||
NazaraError("failed to serialize value");
|
||||
|
||||
return *this;
|
||||
|
||||
@@ -711,16 +711,16 @@ namespace Nz
|
||||
*/
|
||||
inline bool Serialize(SerializationContext& context, const Color& color, TypeTag<Color>)
|
||||
{
|
||||
if (!Serialize(context, color.r))
|
||||
if (!Serialize(context, "R", color.r))
|
||||
return false;
|
||||
|
||||
if (!Serialize(context, color.g))
|
||||
if (!Serialize(context, "G", color.g))
|
||||
return false;
|
||||
|
||||
if (!Serialize(context, color.b))
|
||||
if (!Serialize(context, "B", color.b))
|
||||
return false;
|
||||
|
||||
if (!Serialize(context, color.a))
|
||||
if (!Serialize(context, "A", color.a))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
@@ -735,16 +735,16 @@ namespace Nz
|
||||
*/
|
||||
inline bool Unserialize(SerializationContext& context, Color* color, TypeTag<Color>)
|
||||
{
|
||||
if (!Unserialize(context, &color->r))
|
||||
if (!Unserialize(context, "R", &color->r))
|
||||
return false;
|
||||
|
||||
if (!Unserialize(context, &color->g))
|
||||
if (!Unserialize(context, "G", &color->g))
|
||||
return false;
|
||||
|
||||
if (!Unserialize(context, &color->b))
|
||||
if (!Unserialize(context, "B", &color->b))
|
||||
return false;
|
||||
|
||||
if (!Unserialize(context, &color->a))
|
||||
if (!Unserialize(context, "A", &color->a))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
|
||||
@@ -13,41 +13,106 @@
|
||||
#include <NazaraUtils/Endianness.hpp>
|
||||
#include <NazaraUtils/MovablePtr.hpp>
|
||||
#include <NazaraUtils/TypeTag.hpp>
|
||||
#include <NazaraUtils/EnumArray.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
template <typename T>
|
||||
concept Numeric = std::is_arithmetic<T>::value;
|
||||
|
||||
struct NAZARA_CORE_API SerializationContext
|
||||
{
|
||||
MovablePtr<Stream> stream;
|
||||
Endianness endianness = Endianness::BigEndian; //< Default to Big Endian encoding
|
||||
UInt8 readBitPos = 8; //< 8 means no bit is currently read
|
||||
UInt8 readByte; //< Undefined value, will be initialized at the first bit read
|
||||
UInt8 writeBitPos = 8; //< 8 means no bit is currently wrote
|
||||
UInt8 writeByte; //< Undefined value, will be initialized at the first bit write
|
||||
public:
|
||||
virtual bool PushObject(std::string_view name) = 0;
|
||||
virtual bool PopObject() = 0;
|
||||
virtual bool PushArray(std::string_view name) = 0;
|
||||
virtual bool PopArray() = 0;
|
||||
|
||||
void FlushBits();
|
||||
inline void ResetReadBitPosition();
|
||||
inline void ResetWriteBitPosition();
|
||||
virtual bool Write(std::string_view name, bool value) = 0;
|
||||
virtual bool Write(std::string_view name, Nz::Int8 value) = 0;
|
||||
virtual bool Write(std::string_view name, Nz::Int16 value) = 0;
|
||||
virtual bool Write(std::string_view name, Nz::Int32 value) = 0;
|
||||
virtual bool Write(std::string_view name, Nz::Int64 value) = 0;
|
||||
virtual bool Write(std::string_view name, Nz::UInt8 value) = 0;
|
||||
virtual bool Write(std::string_view name, Nz::UInt16 value) = 0;
|
||||
virtual bool Write(std::string_view name, Nz::UInt32 value) = 0;
|
||||
virtual bool Write(std::string_view name, Nz::UInt64 value) = 0;
|
||||
virtual bool Write(std::string_view name, float value) = 0;
|
||||
virtual bool Write(std::string_view name, double value) = 0;
|
||||
virtual bool Write(std::string_view name, const std::string& value) = 0;
|
||||
|
||||
virtual bool Read(std::string_view name, bool* value) = 0;
|
||||
virtual bool Read(std::string_view name, Nz::Int8* value) = 0;
|
||||
virtual bool Read(std::string_view name, Nz::Int16* value) = 0;
|
||||
virtual bool Read(std::string_view name, Nz::Int32* value) = 0;
|
||||
virtual bool Read(std::string_view name, Nz::Int64* value) = 0;
|
||||
virtual bool Read(std::string_view name, Nz::UInt8* value) = 0;
|
||||
virtual bool Read(std::string_view name, Nz::UInt16* value) = 0;
|
||||
virtual bool Read(std::string_view name, Nz::UInt32* value) = 0;
|
||||
virtual bool Read(std::string_view name, Nz::UInt64* value) = 0;
|
||||
virtual bool Read(std::string_view name, float* value) = 0;
|
||||
virtual bool Read(std::string_view name, double* value) = 0;
|
||||
virtual bool Read(std::string_view name, std::string* value) = 0;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
bool Serialize(SerializationContext& context, T&& value);
|
||||
|
||||
inline bool Serialize(SerializationContext& context, bool value, TypeTag<bool>);
|
||||
inline bool Serialize(SerializationContext& context, const std::string& value, TypeTag<std::string>);
|
||||
inline bool Serialize(SerializationContext&, T, TypeTag<T>) { return false; }
|
||||
|
||||
template<typename T>
|
||||
std::enable_if_t<std::is_arithmetic<T>::value, bool> Serialize(SerializationContext& context, T value, TypeTag<T>);
|
||||
bool Serialize(SerializationContext& context, std::string_view name, const T& value);
|
||||
|
||||
template<typename T>
|
||||
bool Unserialize(SerializationContext& context, T* value);
|
||||
inline bool Serialize(SerializationContext& context, std::string_view name, const T& value, TypeTag<T>);
|
||||
|
||||
inline bool Unserialize(SerializationContext& context, bool* value, TypeTag<bool>);
|
||||
inline bool Unserialize(SerializationContext& context, std::string* value, TypeTag<std::string>);
|
||||
inline bool Serialize(SerializationContext& context, std::string_view name, const bool& value, TypeTag<bool>);
|
||||
inline bool Serialize(SerializationContext& context, std::string_view name, const std::string& value, TypeTag<std::string>);
|
||||
|
||||
template<typename T, size_t N>
|
||||
bool Serialize(SerializationContext& context, std::string_view name, const T (&values)[N]);
|
||||
|
||||
template<typename T, size_t N>
|
||||
bool Serialize(SerializationContext& context, std::string_view name, const std::array<T, N>& values);
|
||||
|
||||
template<typename T>
|
||||
std::enable_if_t<std::is_arithmetic<T>::value, bool> Unserialize(SerializationContext& context, T* value, TypeTag<T>);
|
||||
bool Serialize(SerializationContext& context, std::string_view name, const std::vector<T>& values);
|
||||
|
||||
template<typename E, typename T>
|
||||
bool Serialize(SerializationContext& context, std::string_view name, const Nz::EnumArray<E, T>& values);
|
||||
|
||||
template<Numeric T>
|
||||
inline bool Serialize(SerializationContext& context, std::string_view name, const T& value, TypeTag<T>);
|
||||
|
||||
|
||||
template<typename T>
|
||||
inline bool Unserialize(SerializationContext&, T*, TypeTag<T>) { return false; }
|
||||
|
||||
template<typename T>
|
||||
inline bool Unserialize(SerializationContext& context, std::string_view name, T* value);
|
||||
|
||||
template<typename T>
|
||||
bool Unserialize(SerializationContext& context, std::string_view name, T* value, TypeTag<T>);
|
||||
|
||||
template <>
|
||||
inline bool Unserialize(SerializationContext& context, std::string_view name, bool* value, TypeTag<bool>);
|
||||
|
||||
template <>
|
||||
inline bool Unserialize(SerializationContext& context, std::string_view name, std::string* value, TypeTag<std::string>);
|
||||
|
||||
template<typename T, size_t N>
|
||||
bool Unserialize(SerializationContext& context, std::string_view name, T (*values)[N]);
|
||||
|
||||
template<typename T, size_t N>
|
||||
bool Unserialize(SerializationContext& context, std::string_view name, std::array<T, N>* values);
|
||||
|
||||
template<typename T>
|
||||
bool Unserialize(SerializationContext& context, std::string_view name, std::vector<T>* values);
|
||||
|
||||
template<typename E, typename T>
|
||||
bool Unserialize(SerializationContext& context, std::string_view name, Nz::EnumArray<E, T>* values);
|
||||
|
||||
template<Numeric T>
|
||||
inline bool Unserialize(SerializationContext& context, std::string_view name, T* value, TypeTag<T>);
|
||||
}
|
||||
|
||||
#include <Nazara/Core/Serialization.inl>
|
||||
|
||||
@@ -5,31 +5,22 @@
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
/*!
|
||||
* \brief Reset the current read bit cursor
|
||||
*/
|
||||
inline void SerializationContext::ResetReadBitPosition()
|
||||
template<typename T>
|
||||
inline bool Serialize(SerializationContext& context, std::string_view name, const T& value)
|
||||
{
|
||||
readBitPos = 8;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Reset the current read bit cursor
|
||||
|
||||
* \remark This function only reset the cursor position, it doesn't do any writing
|
||||
if you wish to write all bits and reset bit position, call FlushBits
|
||||
|
||||
\see FlushBits
|
||||
*/
|
||||
inline void SerializationContext::ResetWriteBitPosition()
|
||||
{
|
||||
writeBitPos = 8;
|
||||
return Serialize(context, name, value, TypeTag<std::decay_t<T>>());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool Serialize(SerializationContext& context, T&& value)
|
||||
bool Serialize(SerializationContext& context, std::string_view name, const T& value, TypeTag<T>)
|
||||
{
|
||||
return Serialize(context, std::forward<T>(value), TypeTag<std::decay_t<T>>());
|
||||
if (!context.PushObject(name))
|
||||
return false;
|
||||
|
||||
if (!Serialize(context, value, TypeTag<std::decay_t<T>>()))
|
||||
return false;
|
||||
|
||||
return context.PopObject();
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -42,21 +33,9 @@ namespace Nz
|
||||
*
|
||||
* \see Serialize, Unserialize
|
||||
*/
|
||||
inline bool Serialize(SerializationContext& context, bool value, TypeTag<bool>)
|
||||
inline bool Serialize(SerializationContext& context, std::string_view name, const bool& value, TypeTag<bool>)
|
||||
{
|
||||
if (context.writeBitPos == 8)
|
||||
{
|
||||
context.writeBitPos = 0;
|
||||
context.writeByte = 0;
|
||||
}
|
||||
|
||||
if (value)
|
||||
context.writeByte |= 1 << context.writeBitPos;
|
||||
|
||||
if (++context.writeBitPos >= 8)
|
||||
return Serialize(context, context.writeByte, TypeTag<UInt8>());
|
||||
else
|
||||
return true;
|
||||
return context.Write(name, value);
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -67,12 +46,69 @@ namespace Nz
|
||||
* \param context Context for the serialization
|
||||
* \param value String to serialize
|
||||
*/
|
||||
bool Serialize(SerializationContext& context, const std::string& value, TypeTag<std::string>)
|
||||
inline bool Serialize(SerializationContext& context, std::string_view name, const std::string& value, TypeTag<std::string>)
|
||||
{
|
||||
if (!Serialize(context, SafeCast<UInt32>(value.size()), TypeTag<UInt32>()))
|
||||
return context.Write(name, value);
|
||||
}
|
||||
|
||||
template<typename T, size_t N>
|
||||
bool Serialize(SerializationContext& context, std::string_view name, const T(&values)[N])
|
||||
{
|
||||
if (!context.PushArray(name))
|
||||
return false;
|
||||
|
||||
return context.stream->Write(value.data(), value.size()) == value.size();
|
||||
for (size_t i = 0; i < N; ++i)
|
||||
{
|
||||
if (!Serialize(context, "", values[i]))
|
||||
return false;
|
||||
}
|
||||
|
||||
return context.PopArray();
|
||||
}
|
||||
|
||||
template<typename T, size_t N>
|
||||
bool Serialize(SerializationContext& context, std::string_view name, const std::array<T, N>& values)
|
||||
{
|
||||
if (!context.PushArray(name))
|
||||
return false;
|
||||
|
||||
for (auto&& value : values)
|
||||
{
|
||||
if (!Serialize(context, "", value))
|
||||
return false;
|
||||
}
|
||||
|
||||
return context.PopArray();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool Serialize(SerializationContext& context, std::string_view name, const std::vector<T>& values)
|
||||
{
|
||||
if (!context.PushArray(name))
|
||||
return false;
|
||||
|
||||
for (auto&& value : values)
|
||||
{
|
||||
if (!Serialize(context, "", value))
|
||||
return false;
|
||||
}
|
||||
|
||||
return context.PopArray();
|
||||
}
|
||||
|
||||
template<typename E, typename T>
|
||||
bool Serialize(SerializationContext& context, std::string_view name, const Nz::EnumArray<E, T>& values)
|
||||
{
|
||||
if (!context.PushArray(name))
|
||||
return false;
|
||||
|
||||
for (auto&& value : values)
|
||||
{
|
||||
if (!Serialize(context, "", value))
|
||||
return false;
|
||||
}
|
||||
|
||||
return context.PopArray();
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -85,23 +121,29 @@ namespace Nz
|
||||
*
|
||||
* \see Serialize, Unserialize
|
||||
*/
|
||||
template<typename T>
|
||||
std::enable_if_t<std::is_arithmetic<T>::value, bool> Serialize(SerializationContext& context, T value, TypeTag<T>)
|
||||
template<Numeric T>
|
||||
inline bool Serialize(SerializationContext& context, std::string_view name, const T& value, TypeTag<T>)
|
||||
{
|
||||
// Flush bits in case a writing is in progress
|
||||
context.FlushBits();
|
||||
|
||||
if (context.endianness != Endianness::Unknown && context.endianness != PlatformEndianness)
|
||||
value = ByteSwap(value);
|
||||
|
||||
return context.stream->Write(&value, sizeof(T)) == sizeof(T);
|
||||
return context.Write(name, value);
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
bool Unserialize(SerializationContext& context, T* value)
|
||||
inline bool Unserialize(SerializationContext& context, std::string_view name, T* value)
|
||||
{
|
||||
return Unserialize(context, value, TypeTag<T>());
|
||||
return Unserialize(context, name, value, TypeTag<std::decay_t<T>>());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool Unserialize(SerializationContext& context, std::string_view name, T* value, TypeTag<T>)
|
||||
{
|
||||
if (!context.PushObject(name))
|
||||
return false;
|
||||
|
||||
if (!Unserialize(context, value, TypeTag<T>()))
|
||||
return false;
|
||||
|
||||
return context.PopObject();
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -114,22 +156,9 @@ namespace Nz
|
||||
*
|
||||
* \see Serialize, Unserialize
|
||||
*/
|
||||
inline bool Unserialize(SerializationContext& context, bool* value, TypeTag<bool>)
|
||||
inline bool Unserialize(SerializationContext& context, std::string_view name, bool* value, TypeTag<bool>)
|
||||
{
|
||||
if (context.readBitPos == 8)
|
||||
{
|
||||
if (!Unserialize(context, &context.readByte, TypeTag<UInt8>()))
|
||||
return false;
|
||||
|
||||
context.readBitPos = 0;
|
||||
}
|
||||
|
||||
if (value)
|
||||
*value = (context.readByte & (1 << context.readBitPos)) != 0;
|
||||
|
||||
context.readBitPos++;
|
||||
|
||||
return true;
|
||||
return context.Read(name, value);
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -139,14 +168,69 @@ namespace Nz
|
||||
* \param context Context of unserialization
|
||||
* \param string std::string to unserialize
|
||||
*/
|
||||
bool Unserialize(SerializationContext& context, std::string* string, TypeTag<std::string>)
|
||||
inline bool Unserialize(SerializationContext& context, std::string_view name, std::string* string, TypeTag<std::string>)
|
||||
{
|
||||
UInt32 size;
|
||||
if (!Unserialize(context, &size, TypeTag<UInt32>()))
|
||||
return context.Read(name, string);
|
||||
}
|
||||
|
||||
template<typename T, size_t N>
|
||||
bool Unserialize(SerializationContext& context, std::string_view name, T(*values)[N])
|
||||
{
|
||||
if (!context.PushArray(name))
|
||||
return false;
|
||||
|
||||
string->resize(size);
|
||||
return context.stream->Read(&(*string)[0], size) == size;
|
||||
for (size_t i = 0; i < N; ++i)
|
||||
{
|
||||
if (!Unserialize(context, "", &(*values[i])))
|
||||
return false;
|
||||
}
|
||||
|
||||
return context.PopArray();
|
||||
}
|
||||
|
||||
template<typename T, size_t N>
|
||||
bool Unserialize(SerializationContext& context, std::string_view name, std::array<T, N>* values)
|
||||
{
|
||||
if (!context.PushArray(name))
|
||||
return false;
|
||||
|
||||
for (auto&& value : *values)
|
||||
{
|
||||
if (!Unserialize(context, "", &value))
|
||||
return false;
|
||||
}
|
||||
|
||||
return context.PopArray();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool Unserialize(SerializationContext& context, std::string_view name, std::vector<T>* values)
|
||||
{
|
||||
if (!context.PushArray(name))
|
||||
return false;
|
||||
|
||||
for (auto&& value : *values)
|
||||
{
|
||||
if (!Unserialize(context, "", &value))
|
||||
return false;
|
||||
}
|
||||
|
||||
return context.PopArray();
|
||||
}
|
||||
|
||||
template<typename E, typename T>
|
||||
bool Unserialize(SerializationContext& context, std::string_view name, Nz::EnumArray<E, T>* values)
|
||||
{
|
||||
if (!context.PushArray(name))
|
||||
return false;
|
||||
|
||||
for (auto&& value : *values)
|
||||
{
|
||||
if (!Unserialize(context, "", &value))
|
||||
return false;
|
||||
}
|
||||
|
||||
return context.PopArray();
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -161,22 +245,10 @@ namespace Nz
|
||||
*
|
||||
* \see Serialize, Unserialize
|
||||
*/
|
||||
template<typename T>
|
||||
std::enable_if_t<std::is_arithmetic<T>::value, bool> Unserialize(SerializationContext& context, T* value, TypeTag<T>)
|
||||
template<Numeric T>
|
||||
inline bool Unserialize(SerializationContext& context, std::string_view name, T* value, TypeTag<T>)
|
||||
{
|
||||
NazaraAssert(value, "Invalid data pointer");
|
||||
|
||||
context.ResetReadBitPosition();
|
||||
|
||||
if (context.stream->Read(value, sizeof(T)) == sizeof(T))
|
||||
{
|
||||
if (context.endianness != Endianness::Unknown && context.endianness != PlatformEndianness)
|
||||
*value = ByteSwap(*value);
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
return context.Read(name, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -213,7 +213,7 @@ namespace Nz
|
||||
|
||||
inline bool Serialize(SerializationContext& context, Time time, TypeTag<Time>)
|
||||
{
|
||||
if (!Serialize(context, time.m_nanoseconds))
|
||||
if (!Serialize(context, "value", time.m_nanoseconds))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
@@ -221,7 +221,7 @@ namespace Nz
|
||||
|
||||
inline bool Unserialize(SerializationContext& context, Time* time, TypeTag<Time>)
|
||||
{
|
||||
if (!Unserialize(context, &time->m_nanoseconds))
|
||||
if (!Unserialize(context, "value", &time->m_nanoseconds))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
|
||||
@@ -132,16 +132,13 @@ namespace Nz
|
||||
bool Serialize(SerializationContext& context, const Uuid& value, TypeTag<Uuid>)
|
||||
{
|
||||
const std::array<Nz::UInt8, 16>& array = value.ToArray();
|
||||
if (context.stream->Write(array.data(), array.size()) != array.size())
|
||||
return false;
|
||||
|
||||
return true;
|
||||
return Serialize(context, "value", array);
|
||||
}
|
||||
|
||||
bool Unserialize(SerializationContext& context, Uuid* value, TypeTag<Uuid>)
|
||||
{
|
||||
std::array<Nz::UInt8, 16> array;
|
||||
if (context.stream->Read(array.data(), array.size()) != array.size())
|
||||
if (!Unserialize(context, "value", &array))
|
||||
return false;
|
||||
|
||||
*value = Uuid(array);
|
||||
|
||||
@@ -767,7 +767,7 @@ namespace Nz
|
||||
template<AngleUnit Unit, typename T>
|
||||
bool Serialize(SerializationContext& context, Angle<Unit, T> angle, TypeTag<Angle<Unit, T>>)
|
||||
{
|
||||
if (!Serialize(context, angle.value))
|
||||
if (!Serialize(context, "value", angle.value))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
@@ -783,7 +783,7 @@ namespace Nz
|
||||
template<AngleUnit Unit, typename T>
|
||||
bool Unserialize(SerializationContext& context, Angle<Unit, T>* angle, TypeTag<Angle<Unit, T>>)
|
||||
{
|
||||
if (!Unserialize(context, &angle->value))
|
||||
if (!Unserialize(context, "value", &angle->value))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
|
||||
@@ -421,13 +421,13 @@ namespace Nz
|
||||
template<typename T>
|
||||
bool Serialize(SerializationContext& context, const BoundingVolume<T>& boundingVolume, TypeTag<BoundingVolume<T>>)
|
||||
{
|
||||
if (!Serialize(context, static_cast<UInt8>(boundingVolume.extent)))
|
||||
if (!Serialize(context, "extent", static_cast<UInt8>(boundingVolume.extent)))
|
||||
return false;
|
||||
|
||||
if (!Serialize(context, boundingVolume.aabb))
|
||||
if (!Serialize(context, "aabb", boundingVolume.aabb))
|
||||
return false;
|
||||
|
||||
if (!Serialize(context, boundingVolume.obb))
|
||||
if (!Serialize(context, "obb", boundingVolume.obb))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
@@ -446,7 +446,7 @@ namespace Nz
|
||||
bool Unserialize(SerializationContext& context, BoundingVolume<T>* boundingVolume, TypeTag<BoundingVolume<T>>)
|
||||
{
|
||||
UInt8 extend;
|
||||
if (!Unserialize(context, &extend))
|
||||
if (!Unserialize(context, "extent", &extend))
|
||||
return false;
|
||||
|
||||
if (extend > UnderlyingCast(Extent::Max))
|
||||
@@ -454,10 +454,10 @@ namespace Nz
|
||||
|
||||
boundingVolume->extent = static_cast<Extent>(extend);
|
||||
|
||||
if (!Unserialize(context, &boundingVolume->aabb))
|
||||
if (!Unserialize(context, "aabb", &boundingVolume->aabb))
|
||||
return false;
|
||||
|
||||
if (!Unserialize(context, &boundingVolume->obb))
|
||||
if (!Unserialize(context, "obb", &boundingVolume->obb))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
|
||||
@@ -754,22 +754,22 @@ namespace Nz
|
||||
template<typename T>
|
||||
bool Serialize(SerializationContext& context, const Box<T>& box, TypeTag<Box<T>>)
|
||||
{
|
||||
if (!Serialize(context, box.x))
|
||||
if (!Serialize(context, "x", box.x))
|
||||
return false;
|
||||
|
||||
if (!Serialize(context, box.y))
|
||||
if (!Serialize(context, "y", box.y))
|
||||
return false;
|
||||
|
||||
if (!Serialize(context, box.z))
|
||||
if (!Serialize(context, "z", box.z))
|
||||
return false;
|
||||
|
||||
if (!Serialize(context, box.width))
|
||||
if (!Serialize(context, "width", box.width))
|
||||
return false;
|
||||
|
||||
if (!Serialize(context, box.height))
|
||||
if (!Serialize(context, "height", box.height))
|
||||
return false;
|
||||
|
||||
if (!Serialize(context, box.depth))
|
||||
if (!Serialize(context, "depth", box.depth))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
@@ -785,22 +785,22 @@ namespace Nz
|
||||
template<typename T>
|
||||
bool Unserialize(SerializationContext& context, Box<T>* box, TypeTag<Box<T>>)
|
||||
{
|
||||
if (!Unserialize(context, &box->x))
|
||||
if (!Unserialize(context, "x", &box->x))
|
||||
return false;
|
||||
|
||||
if (!Unserialize(context, &box->y))
|
||||
if (!Unserialize(context, "y", &box->y))
|
||||
return false;
|
||||
|
||||
if (!Unserialize(context, &box->z))
|
||||
if (!Unserialize(context, "z", &box->z))
|
||||
return false;
|
||||
|
||||
if (!Unserialize(context, &box->width))
|
||||
if (!Unserialize(context, "width", &box->width))
|
||||
return false;
|
||||
|
||||
if (!Unserialize(context, &box->height))
|
||||
if (!Unserialize(context, "height", &box->height))
|
||||
return false;
|
||||
|
||||
if (!Unserialize(context, &box->depth))
|
||||
if (!Unserialize(context, "depth", &box->depth))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
|
||||
@@ -294,13 +294,13 @@ namespace Nz
|
||||
template<typename T>
|
||||
bool Serialize(SerializationContext& context, const EulerAngles<T>& angles, TypeTag<EulerAngles<T>>)
|
||||
{
|
||||
if (!Serialize(context, angles.pitch))
|
||||
if (!Serialize(context, "pitch", angles.pitch))
|
||||
return false;
|
||||
|
||||
if (!Serialize(context, angles.yaw))
|
||||
if (!Serialize(context, "yaw", angles.yaw))
|
||||
return false;
|
||||
|
||||
if (!Serialize(context, angles.roll))
|
||||
if (!Serialize(context, "roll", angles.roll))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
@@ -316,13 +316,13 @@ namespace Nz
|
||||
template<typename T>
|
||||
bool Unserialize(SerializationContext& context, EulerAngles<T>* angles, TypeTag<EulerAngles<T>>)
|
||||
{
|
||||
if (!Unserialize(context, &angles->pitch))
|
||||
if (!Unserialize(context, "pitch", &angles->pitch))
|
||||
return false;
|
||||
|
||||
if (!Unserialize(context, &angles->yaw))
|
||||
if (!Unserialize(context, "yaw", &angles->yaw))
|
||||
return false;
|
||||
|
||||
if (!Unserialize(context, &angles->roll))
|
||||
if (!Unserialize(context, "roll", &angles->roll))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
|
||||
@@ -608,13 +608,7 @@ namespace Nz
|
||||
template<typename T>
|
||||
bool Serialize(SerializationContext& context, const Frustum<T>& frustum, TypeTag<Frustum<T>>)
|
||||
{
|
||||
for (const auto& plane : frustum.m_planes)
|
||||
{
|
||||
if (!Serialize(context, plane))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return Serialize(context, "planes", frustum.m_planes);
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -627,13 +621,7 @@ namespace Nz
|
||||
template<typename T>
|
||||
bool Unserialize(SerializationContext& context, Frustum<T>* frustum, TypeTag<Frustum<T>>)
|
||||
{
|
||||
for (auto& plane : frustum->m_planes)
|
||||
{
|
||||
if (!Unserialize(context, &plane))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return Unserialize(context, "planes", &frustum->m_planes);
|
||||
}
|
||||
|
||||
/*!
|
||||
|
||||
@@ -1368,7 +1368,7 @@ namespace Nz
|
||||
{
|
||||
for (unsigned int i = 0; i < 16; ++i)
|
||||
{
|
||||
if (!Serialize(context, matrix[i]))
|
||||
if (!Serialize(context, "", matrix[i]))
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1388,7 +1388,7 @@ namespace Nz
|
||||
T* head = &matrix->m11;
|
||||
for (unsigned int i = 0; i < 16; ++i)
|
||||
{
|
||||
if (!Unserialize(context, head + i))
|
||||
if (!Unserialize(context, "", head + i))
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -217,16 +217,10 @@ namespace Nz
|
||||
template<typename T>
|
||||
bool Serialize(SerializationContext& context, const OrientedBox<T>& obb, TypeTag<OrientedBox<T>>)
|
||||
{
|
||||
if (!Serialize(context, obb.localBox))
|
||||
if (!Serialize(context, "localBox", obb.localBox))
|
||||
return false;
|
||||
|
||||
for (auto&& corner : obb.m_corners)
|
||||
{
|
||||
if (!Serialize(context, corner))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return Serialize(context, "corners", obb.m_corners);
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -239,16 +233,10 @@ namespace Nz
|
||||
template<typename T>
|
||||
bool Unserialize(SerializationContext& context, OrientedBox<T>* obb, TypeTag<OrientedBox<T>>)
|
||||
{
|
||||
if (!Unserialize(context, &obb->localBox))
|
||||
if (!Unserialize(context, "localBox", &obb->localBox))
|
||||
return false;
|
||||
|
||||
for (auto&& corner : obb->m_corners)
|
||||
{
|
||||
if (!Unserialize(context, &corner))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return Unserialize(context, "corners", &obb->m_corners);
|
||||
}
|
||||
|
||||
/*!
|
||||
|
||||
@@ -282,10 +282,10 @@ namespace Nz
|
||||
template<typename T>
|
||||
bool Serialize(SerializationContext& context, const Plane<T>& plane, TypeTag<Plane<T>>)
|
||||
{
|
||||
if (!Serialize(context, plane.normal))
|
||||
if (!Serialize(context, "normal", plane.normal))
|
||||
return false;
|
||||
|
||||
if (!Serialize(context, plane.distance))
|
||||
if (!Serialize(context, "distance", plane.distance))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
@@ -301,10 +301,10 @@ namespace Nz
|
||||
template<typename T>
|
||||
bool Unserialize(SerializationContext& context, Plane<T>* plane, TypeTag<Plane<T>>)
|
||||
{
|
||||
if (!Unserialize(context, &plane->normal))
|
||||
if (!Unserialize(context, "normal", &plane->normal))
|
||||
return false;
|
||||
|
||||
if (!Unserialize(context, &plane->distance))
|
||||
if (!Unserialize(context, "distance", &plane->distance))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
|
||||
@@ -788,16 +788,16 @@ namespace Nz
|
||||
template<typename T>
|
||||
bool Serialize(SerializationContext& context, const Quaternion<T>& quat, TypeTag<Quaternion<T>>)
|
||||
{
|
||||
if (!Serialize(context, quat.x))
|
||||
if (!Serialize(context, "x", quat.x))
|
||||
return false;
|
||||
|
||||
if (!Serialize(context, quat.y))
|
||||
if (!Serialize(context, "y", quat.y))
|
||||
return false;
|
||||
|
||||
if (!Serialize(context, quat.z))
|
||||
if (!Serialize(context, "z", quat.z))
|
||||
return false;
|
||||
|
||||
if (!Serialize(context, quat.w))
|
||||
if (!Serialize(context, "w", quat.w))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
@@ -813,16 +813,16 @@ namespace Nz
|
||||
template<typename T>
|
||||
bool Unserialize(SerializationContext& context, Quaternion<T>* quat, TypeTag<Quaternion<T>>)
|
||||
{
|
||||
if (!Unserialize(context, &quat->x))
|
||||
if (!Unserialize(context, "x", &quat->x))
|
||||
return false;
|
||||
|
||||
if (!Unserialize(context, &quat->y))
|
||||
if (!Unserialize(context, "y", &quat->y))
|
||||
return false;
|
||||
|
||||
if (!Unserialize(context, &quat->z))
|
||||
if (!Unserialize(context, "z", &quat->z))
|
||||
return false;
|
||||
|
||||
if (!Unserialize(context, &quat->w))
|
||||
if (!Unserialize(context, "w", &quat->w))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
|
||||
@@ -575,10 +575,10 @@ namespace Nz
|
||||
template<typename T>
|
||||
bool Serialize(SerializationContext& context, const Ray<T>& ray, TypeTag<Ray<T>>)
|
||||
{
|
||||
if (!Serialize(context, ray.origin))
|
||||
if (!Serialize(context, "origin", ray.origin))
|
||||
return false;
|
||||
|
||||
if (!Serialize(context, ray.direction))
|
||||
if (!Serialize(context, "direction", ray.direction))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
@@ -594,10 +594,10 @@ namespace Nz
|
||||
template<typename T>
|
||||
bool Unserialize(SerializationContext& context, Ray<T>* ray, TypeTag<Ray<T>>)
|
||||
{
|
||||
if (!Unserialize(context, &ray->origin))
|
||||
if (!Unserialize(context, "origin", &ray->origin))
|
||||
return false;
|
||||
|
||||
if (!Unserialize(context, &ray->direction))
|
||||
if (!Unserialize(context, "direction", &ray->direction))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
|
||||
@@ -625,16 +625,16 @@ namespace Nz
|
||||
template<typename T>
|
||||
bool Serialize(SerializationContext& context, const Rect<T>& rect, TypeTag<Rect<T>>)
|
||||
{
|
||||
if (!Serialize(context, rect.x))
|
||||
if (!Serialize(context, "x", rect.x))
|
||||
return false;
|
||||
|
||||
if (!Serialize(context, rect.y))
|
||||
if (!Serialize(context, "y", rect.y))
|
||||
return false;
|
||||
|
||||
if (!Serialize(context, rect.width))
|
||||
if (!Serialize(context, "width", rect.width))
|
||||
return false;
|
||||
|
||||
if (!Serialize(context, rect.height))
|
||||
if (!Serialize(context, "height", rect.height))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
@@ -650,16 +650,16 @@ namespace Nz
|
||||
template<typename T>
|
||||
bool Unserialize(SerializationContext& context, Rect<T>* rect, TypeTag<Rect<T>>)
|
||||
{
|
||||
if (!Unserialize(context, &rect->x))
|
||||
if (!Unserialize(context, "x", &rect->x))
|
||||
return false;
|
||||
|
||||
if (!Unserialize(context, &rect->y))
|
||||
if (!Unserialize(context, "y", &rect->y))
|
||||
return false;
|
||||
|
||||
if (!Unserialize(context, &rect->width))
|
||||
if (!Unserialize(context, "width", &rect->width))
|
||||
return false;
|
||||
|
||||
if (!Unserialize(context, &rect->height))
|
||||
if (!Unserialize(context, "height", &rect->height))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
|
||||
@@ -507,16 +507,16 @@ namespace Nz
|
||||
template<typename T>
|
||||
bool Serialize(SerializationContext& context, const Sphere<T>& sphere, TypeTag<Sphere<T>>)
|
||||
{
|
||||
if (!Serialize(context, sphere.x))
|
||||
if (!Serialize(context, "x", sphere.x))
|
||||
return false;
|
||||
|
||||
if (!Serialize(context, sphere.y))
|
||||
if (!Serialize(context, "y", sphere.y))
|
||||
return false;
|
||||
|
||||
if (!Serialize(context, sphere.z))
|
||||
if (!Serialize(context, "z", sphere.z))
|
||||
return false;
|
||||
|
||||
if (!Serialize(context, sphere.radius))
|
||||
if (!Serialize(context, "radius", sphere.radius))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
@@ -532,16 +532,16 @@ namespace Nz
|
||||
template<typename T>
|
||||
bool Unserialize(SerializationContext& context, Sphere<T>* sphere, TypeTag<Sphere<T>>)
|
||||
{
|
||||
if (!Unserialize(context, &sphere->x))
|
||||
if (!Unserialize(context, "x", &sphere->x))
|
||||
return false;
|
||||
|
||||
if (!Unserialize(context, &sphere->y))
|
||||
if (!Unserialize(context, "y", &sphere->y))
|
||||
return false;
|
||||
|
||||
if (!Unserialize(context, &sphere->z))
|
||||
if (!Unserialize(context, "z", &sphere->z))
|
||||
return false;
|
||||
|
||||
if (!Unserialize(context, &sphere->radius))
|
||||
if (!Unserialize(context, "radius", &sphere->radius))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
|
||||
@@ -775,10 +775,10 @@ namespace Nz
|
||||
template<typename T>
|
||||
bool Serialize(SerializationContext& context, const Vector2<T>& vector, TypeTag<Vector2<T>>)
|
||||
{
|
||||
if (!Serialize(context, vector.x))
|
||||
if (!Serialize(context, "x", vector.x))
|
||||
return false;
|
||||
|
||||
if (!Serialize(context, vector.y))
|
||||
if (!Serialize(context, "y", vector.y))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
@@ -794,10 +794,10 @@ namespace Nz
|
||||
template<typename T>
|
||||
bool Unserialize(SerializationContext& context, Vector2<T>* vector, TypeTag<Vector2<T>>)
|
||||
{
|
||||
if (!Unserialize(context, &vector->x))
|
||||
if (!Unserialize(context, "x", &vector->x))
|
||||
return false;
|
||||
|
||||
if (!Unserialize(context, &vector->y))
|
||||
if (!Unserialize(context, "y", &vector->y))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
|
||||
@@ -956,13 +956,13 @@ namespace Nz
|
||||
template<typename T>
|
||||
bool Serialize(SerializationContext& context, const Vector3<T>& vector, TypeTag<Vector3<T>>)
|
||||
{
|
||||
if (!Serialize(context, vector.x))
|
||||
if (!Serialize(context, "x", vector.x))
|
||||
return false;
|
||||
|
||||
if (!Serialize(context, vector.y))
|
||||
if (!Serialize(context, "y", vector.y))
|
||||
return false;
|
||||
|
||||
if (!Serialize(context, vector.z))
|
||||
if (!Serialize(context, "z", vector.z))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
@@ -978,13 +978,13 @@ namespace Nz
|
||||
template<typename T>
|
||||
bool Unserialize(SerializationContext& context, Vector3<T>* vector, TypeTag<Vector3<T>>)
|
||||
{
|
||||
if (!Unserialize(context, &vector->x))
|
||||
if (!Unserialize(context, "x", &vector->x))
|
||||
return false;
|
||||
|
||||
if (!Unserialize(context, &vector->y))
|
||||
if (!Unserialize(context, "y", &vector->y))
|
||||
return false;
|
||||
|
||||
if (!Unserialize(context, &vector->z))
|
||||
if (!Unserialize(context, "z", &vector->z))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
|
||||
@@ -758,16 +758,16 @@ namespace Nz
|
||||
template<typename T>
|
||||
bool Serialize(SerializationContext& context, const Vector4<T>& vector, TypeTag<Vector4<T>>)
|
||||
{
|
||||
if (!Serialize(context, vector.x))
|
||||
if (!Serialize(context, "x", vector.x))
|
||||
return false;
|
||||
|
||||
if (!Serialize(context, vector.y))
|
||||
if (!Serialize(context, "y", vector.y))
|
||||
return false;
|
||||
|
||||
if (!Serialize(context, vector.z))
|
||||
if (!Serialize(context, "z", vector.z))
|
||||
return false;
|
||||
|
||||
if (!Serialize(context, vector.w))
|
||||
if (!Serialize(context, "w", vector.w))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
@@ -783,16 +783,16 @@ namespace Nz
|
||||
template<typename T>
|
||||
bool Unserialize(SerializationContext& context, Vector4<T>* vector, TypeTag<Vector4<T>>)
|
||||
{
|
||||
if (!Unserialize(context, &vector->x))
|
||||
if (!Unserialize(context, "x", &vector->x))
|
||||
return false;
|
||||
|
||||
if (!Unserialize(context, &vector->y))
|
||||
if (!Unserialize(context, "y", &vector->y))
|
||||
return false;
|
||||
|
||||
if (!Unserialize(context, &vector->z))
|
||||
if (!Unserialize(context, "z", &vector->z))
|
||||
return false;
|
||||
|
||||
if (!Unserialize(context, &vector->w))
|
||||
if (!Unserialize(context, "w", &vector->w))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user