Core: Replace serialization arguments by context structures
Also fixed some endianness errors Former-commit-id: 450849e681a9b002c3d501a856a8481470d08dea
This commit is contained in:
parent
bc4eb96af2
commit
1c8a09f90c
|
|
@ -9,8 +9,7 @@
|
|||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/Enums.hpp>
|
||||
#include <Nazara/Core/InputStream.hpp>
|
||||
#include <Nazara/Core/OutputStream.hpp>
|
||||
#include <Nazara/Core/Serialization.hpp>
|
||||
#include <functional>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
|
|
@ -29,15 +28,15 @@ namespace Nz
|
|||
template<typename T>
|
||||
struct TypeTag {};
|
||||
|
||||
inline bool Serialize(OutputStream* output, bool value, Endianness dataEndianness);
|
||||
inline bool Serialize(SerializationContext& context, bool value);
|
||||
|
||||
template<typename T>
|
||||
std::enable_if_t<std::is_arithmetic<T>::value, bool> Serialize(OutputStream* output, T value, Endianness dataEndianness);
|
||||
std::enable_if_t<std::is_arithmetic<T>::value, bool> Serialize(SerializationContext& context, T value);
|
||||
|
||||
inline bool Unserialize(InputStream* input, bool* value, Endianness dataEndianness);
|
||||
inline bool Unserialize(UnserializationContext& context, bool* value);
|
||||
|
||||
template<typename T>
|
||||
std::enable_if_t<std::is_arithmetic<T>::value, bool> Unserialize(InputStream* input, T* value, Endianness dataEndianness);
|
||||
std::enable_if_t<std::is_arithmetic<T>::value, bool> Unserialize(UnserializationContext& context, T* value);
|
||||
}
|
||||
|
||||
#include <Nazara/Core/Algorithm.inl>
|
||||
|
|
|
|||
|
|
@ -8,6 +8,8 @@
|
|||
|
||||
#include <Nazara/Core/AbstractHash.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Core/InputStream.hpp>
|
||||
#include <Nazara/Core/OutputStream.hpp>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
|
|
@ -77,40 +79,28 @@ namespace Nz
|
|||
seed = static_cast<std::size_t>(b * kMul);
|
||||
}
|
||||
|
||||
inline bool Serialize(OutputStream* output, bool value, Endianness dataEndianness)
|
||||
inline bool Serialize(SerializationContext& context, bool value)
|
||||
{
|
||||
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;
|
||||
return context.stream->Write(&buffer, 1) == 1;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::enable_if_t<std::is_arithmetic<T>::value, bool> Serialize(OutputStream* output, T value, Endianness dataEndianness)
|
||||
std::enable_if_t<std::is_arithmetic<T>::value, bool> Serialize(SerializationContext& context, T value)
|
||||
{
|
||||
NazaraAssert(output, "Invalid stream");
|
||||
|
||||
if (output->Write(&value, sizeof(T)) == sizeof(T))
|
||||
{
|
||||
if (dataEndianness != Endianness_Unknown && dataEndianness != GetPlatformEndianness())
|
||||
if (context.endianness != Endianness_Unknown && context.endianness != GetPlatformEndianness())
|
||||
SwapBytes(&value, sizeof(T));
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
return context.stream->Write(&value, sizeof(T)) == sizeof(T);
|
||||
}
|
||||
|
||||
inline bool Unserialize(InputStream* input, bool* value, Endianness dataEndianness)
|
||||
inline bool Unserialize(UnserializationContext& context, bool* value)
|
||||
{
|
||||
NazaraAssert(input, "Invalid stream");
|
||||
NazaraAssert(value, "Invalid data pointer");
|
||||
NazaraUnused(dataEndianness);
|
||||
|
||||
UInt8 buffer;
|
||||
if (input->Read(&buffer, 1) == 1)
|
||||
if (context.stream->Read(&buffer, 1) == 1)
|
||||
{
|
||||
*value = (buffer == 1);
|
||||
return true;
|
||||
|
|
@ -120,15 +110,14 @@ namespace Nz
|
|||
}
|
||||
|
||||
template<typename T>
|
||||
std::enable_if_t<std::is_arithmetic<T>::value, bool> Unserialize(InputStream* input, T* value, Endianness dataEndianness)
|
||||
std::enable_if_t<std::is_arithmetic<T>::value, bool> Unserialize(UnserializationContext& context, T* value)
|
||||
{
|
||||
NazaraAssert(input, "Invalid stream");
|
||||
NazaraAssert(value, "Invalid data pointer");
|
||||
|
||||
if (input->Read(value, sizeof(T)) == sizeof(T))
|
||||
if (context.stream->Read(value, sizeof(T)) == sizeof(T))
|
||||
{
|
||||
if (dataEndianness != Endianness_Unknown && dataEndianness != GetPlatformEndianness())
|
||||
SwapBytes(&value, sizeof(T));
|
||||
if (context.endianness != Endianness_Unknown && context.endianness != GetPlatformEndianness())
|
||||
SwapBytes(value, sizeof(T));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@
|
|||
#define NAZARA_INPUTSTREAM_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/Serialization.hpp>
|
||||
#include <Nazara/Core/Stream.hpp>
|
||||
|
||||
namespace Nz
|
||||
|
|
@ -15,6 +16,8 @@ namespace Nz
|
|||
class NAZARA_CORE_API InputStream : virtual public Stream
|
||||
{
|
||||
public:
|
||||
inline InputStream(const InputStream& stream);
|
||||
inline InputStream(InputStream&& stream) noexcept;
|
||||
virtual ~InputStream();
|
||||
|
||||
virtual bool EndOfStream() const = 0;
|
||||
|
|
@ -27,8 +30,13 @@ namespace Nz
|
|||
template<typename T>
|
||||
InputStream& operator>>(T& value);
|
||||
|
||||
inline InputStream& operator=(const InputStream& stream);
|
||||
inline InputStream& operator=(InputStream&& stream) noexcept;
|
||||
|
||||
protected:
|
||||
inline InputStream();
|
||||
|
||||
UnserializationContext m_unserializationContext;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,20 +3,61 @@
|
|||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <utility>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
inline InputStream::InputStream() :
|
||||
Stream(OpenMode_Current)
|
||||
{
|
||||
m_unserializationContext.stream = this;
|
||||
}
|
||||
|
||||
inline InputStream::InputStream(const InputStream& stream) :
|
||||
Stream(stream),
|
||||
m_unserializationContext(stream.m_unserializationContext)
|
||||
{
|
||||
m_unserializationContext.stream = this;
|
||||
}
|
||||
|
||||
inline InputStream::InputStream(InputStream&& stream) noexcept :
|
||||
Stream(std::move(stream)),
|
||||
m_unserializationContext(std::move(stream.m_unserializationContext))
|
||||
{
|
||||
m_unserializationContext.stream = this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
InputStream& InputStream::operator>>(T& value)
|
||||
{
|
||||
if (!Unserialize(this, &value, m_dataEndianness))
|
||||
m_unserializationContext.endianness = m_dataEndianness; //< In case m_dataEndianness changed
|
||||
|
||||
if (!Unserialize(m_unserializationContext, &value))
|
||||
NazaraError("Failed to unserialize value");
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline InputStream& InputStream::operator=(const InputStream& stream)
|
||||
{
|
||||
Stream::operator=(stream);
|
||||
|
||||
m_unserializationContext = stream.m_unserializationContext;
|
||||
m_unserializationContext.stream = this;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline InputStream& InputStream::operator=(InputStream&& stream) noexcept
|
||||
{
|
||||
Stream::operator=(std::move(stream));
|
||||
|
||||
m_unserializationContext = std::move(stream.m_unserializationContext);
|
||||
m_unserializationContext.stream = this;
|
||||
|
||||
return *this;
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/Core/DebugOff.hpp>
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@
|
|||
#define NAZARA_OUTPUTSTREAM_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/Serialization.hpp>
|
||||
#include <Nazara/Core/Stream.hpp>
|
||||
|
||||
namespace Nz
|
||||
|
|
@ -18,6 +19,8 @@ namespace Nz
|
|||
class NAZARA_CORE_API OutputStream : virtual public Stream
|
||||
{
|
||||
public:
|
||||
inline OutputStream(const OutputStream& stream);
|
||||
inline OutputStream(OutputStream&& stream) noexcept;
|
||||
virtual ~OutputStream();
|
||||
|
||||
virtual void Flush() = 0;
|
||||
|
|
@ -29,8 +32,13 @@ namespace Nz
|
|||
template<typename T>
|
||||
OutputStream& operator<<(const T& value);
|
||||
|
||||
inline OutputStream& operator=(const OutputStream& stream);
|
||||
inline OutputStream& operator=(OutputStream&& stream) noexcept;
|
||||
|
||||
protected:
|
||||
inline OutputStream();
|
||||
|
||||
SerializationContext m_serializationContext;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,20 +3,61 @@
|
|||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <utility>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
inline OutputStream::OutputStream() :
|
||||
Stream(OpenMode_Current)
|
||||
{
|
||||
m_serializationContext.stream = this;
|
||||
}
|
||||
|
||||
inline OutputStream::OutputStream(const OutputStream& stream) :
|
||||
Stream(stream),
|
||||
m_serializationContext(stream.m_serializationContext)
|
||||
{
|
||||
m_serializationContext.stream = this;
|
||||
}
|
||||
|
||||
inline OutputStream::OutputStream(OutputStream&& stream) noexcept :
|
||||
Stream(std::move(stream)),
|
||||
m_serializationContext(std::move(stream.m_serializationContext))
|
||||
{
|
||||
m_serializationContext.stream = this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
OutputStream& OutputStream::operator<<(const T& value)
|
||||
{
|
||||
if (!Serialize(this, value, m_dataEndianness))
|
||||
m_serializationContext.endianness = m_dataEndianness; //< In case m_dataEndianness changed
|
||||
|
||||
if (!Serialize(m_serializationContext, value))
|
||||
NazaraError("Failed to serialize value");
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline OutputStream& OutputStream::operator=(const OutputStream& stream)
|
||||
{
|
||||
Stream::operator=(stream);
|
||||
|
||||
m_serializationContext = stream.m_serializationContext;
|
||||
m_serializationContext.stream = this;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline OutputStream& OutputStream::operator=(OutputStream&& stream) noexcept
|
||||
{
|
||||
Stream::operator=(std::move(stream));
|
||||
|
||||
m_serializationContext = std::move(stream.m_serializationContext);
|
||||
m_serializationContext.stream = this;
|
||||
|
||||
return *this;
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/Core/DebugOff.hpp>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,34 @@
|
|||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Core module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_SERIALIZATION_HPP
|
||||
#define NAZARA_SERIALIZATION_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/Endianness.hpp>
|
||||
#include <functional>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class InputStream;
|
||||
class OutputStream;
|
||||
|
||||
struct SerializationContext
|
||||
{
|
||||
OutputStream* stream;
|
||||
Endianness endianness;
|
||||
};
|
||||
|
||||
struct UnserializationContext
|
||||
{
|
||||
InputStream* stream;
|
||||
Endianness endianness;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // NAZARA_SERIALIZATION_HPP
|
||||
|
|
@ -9,6 +9,7 @@
|
|||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/Endianness.hpp>
|
||||
#include <Nazara/Core/Serialization.hpp>
|
||||
#include <atomic>
|
||||
#include <iosfwd>
|
||||
#include <memory>
|
||||
|
|
@ -17,11 +18,6 @@
|
|||
|
||||
namespace Nz
|
||||
{
|
||||
class AbstractHash;
|
||||
class HashDigest;
|
||||
class InputStream;
|
||||
class OutputStream;
|
||||
|
||||
class NAZARA_CORE_API String
|
||||
{
|
||||
public:
|
||||
|
|
@ -326,9 +322,11 @@ namespace Nz
|
|||
};
|
||||
};
|
||||
|
||||
class AbstractHash;
|
||||
|
||||
inline bool HashAppend(AbstractHash* hash, const 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);
|
||||
NAZARA_CORE_API bool Serialize(SerializationContext& context, const String& string);
|
||||
NAZARA_CORE_API bool Unserialize(UnserializationContext& context, String* string);
|
||||
}
|
||||
|
||||
namespace std
|
||||
|
|
|
|||
|
|
@ -4209,23 +4209,23 @@ namespace Nz
|
|||
return emptyString;
|
||||
}
|
||||
|
||||
bool Serialize(OutputStream* output, const String& string, Endianness dataEndianness)
|
||||
bool Serialize(SerializationContext& context, const String& string)
|
||||
{
|
||||
if (!Serialize<UInt32>(output, string.GetSize(), dataEndianness))
|
||||
if (!Serialize<UInt32>(context, string.GetSize()))
|
||||
return false;
|
||||
|
||||
output->Write(string.GetConstBuffer(), string.GetSize());
|
||||
context.stream->Write(string.GetConstBuffer(), string.GetSize());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Unserialize(InputStream* input, String* string, Endianness dataEndianness)
|
||||
bool Unserialize(UnserializationContext& context, String* string)
|
||||
{
|
||||
UInt32 size;
|
||||
if (!Unserialize(input, &size, dataEndianness))
|
||||
if (!Unserialize(context, &size))
|
||||
return false;
|
||||
|
||||
string->Resize(size);
|
||||
input->Read(string->GetBuffer(), size);
|
||||
context.stream->Read(string->GetBuffer(), size);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue