Core: Add Serializer/Unserializer
Former-commit-id: 50fdf56da4fef46ec78995cb8fc277cbc9a11ebc
This commit is contained in:
parent
ed961f5ba8
commit
c93e4e901e
|
|
@ -1,4 +1,4 @@
|
|||
// This file was automatically generated on 17 Nov 2015 at 13:20:45
|
||||
// This file was automatically generated on 20 Nov 2015 at 14:22:32
|
||||
|
||||
/*
|
||||
Nazara Engine - Core module
|
||||
|
|
@ -55,7 +55,6 @@
|
|||
#include <Nazara/Core/MemoryHelper.hpp>
|
||||
#include <Nazara/Core/MemoryManager.hpp>
|
||||
#include <Nazara/Core/MemoryPool.hpp>
|
||||
#include <Nazara/Core/MemoryStream.hpp>
|
||||
#include <Nazara/Core/MemoryView.hpp>
|
||||
#include <Nazara/Core/Mutex.hpp>
|
||||
#include <Nazara/Core/ObjectLibrary.hpp>
|
||||
|
|
@ -70,6 +69,8 @@
|
|||
#include <Nazara/Core/ResourceLoader.hpp>
|
||||
#include <Nazara/Core/ResourceManager.hpp>
|
||||
#include <Nazara/Core/Semaphore.hpp>
|
||||
#include <Nazara/Core/Serialization.hpp>
|
||||
#include <Nazara/Core/Serializer.hpp>
|
||||
#include <Nazara/Core/Signal.hpp>
|
||||
#include <Nazara/Core/SparsePtr.hpp>
|
||||
#include <Nazara/Core/Stream.hpp>
|
||||
|
|
@ -78,6 +79,7 @@
|
|||
#include <Nazara/Core/TaskScheduler.hpp>
|
||||
#include <Nazara/Core/Thread.hpp>
|
||||
#include <Nazara/Core/Unicode.hpp>
|
||||
#include <Nazara/Core/Unserializer.hpp>
|
||||
#include <Nazara/Core/Updatable.hpp>
|
||||
|
||||
#endif // NAZARA_GLOBAL_CORE_HPP
|
||||
|
|
|
|||
|
|
@ -1,58 +0,0 @@
|
|||
// 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_MEMORYSTREAM_HPP
|
||||
#define NAZARA_MEMORYSTREAM_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/ByteArray.hpp>
|
||||
#include <Nazara/Core/Stream.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class NAZARA_CORE_API MemoryStream : public Stream
|
||||
{
|
||||
public:
|
||||
MemoryStream();
|
||||
MemoryStream(const void* ptr, unsigned int size);
|
||||
MemoryStream(const MemoryStream&) = default;
|
||||
MemoryStream(MemoryStream&&) = default;
|
||||
~MemoryStream() = default;
|
||||
|
||||
void Clear();
|
||||
|
||||
bool EndOfStream() const override;
|
||||
|
||||
|
||||
const ByteArray& GetBuffer() const;
|
||||
const UInt8* GetData() const;
|
||||
UInt64 GetCursorPos() const override;
|
||||
UInt64 GetSize() const override;
|
||||
|
||||
bool SetCursorPos(UInt64 offset) override;
|
||||
|
||||
MemoryStream& operator=(const MemoryStream&) = default;
|
||||
MemoryStream& operator=(MemoryStream&&) = default;
|
||||
|
||||
private:
|
||||
void FlushStream() override;
|
||||
std::size_t ReadBlock(void* buffer, std::size_t size) override;
|
||||
std::size_t WriteBlock(const void* buffer, std::size_t size) override;
|
||||
|
||||
ByteArray m_buffer;
|
||||
UInt64 m_pos;
|
||||
};
|
||||
|
||||
class AbstractHash;
|
||||
|
||||
inline bool HashAppend(AbstractHash* hash, const String& string);
|
||||
NAZARA_CORE_API bool Serialize(SerializationContext& context, const String& string);
|
||||
NAZARA_CORE_API bool Unserialize(UnserializationContext& context, String* string);
|
||||
}
|
||||
|
||||
#include <Nazara/Core/MemoryStream.inl>
|
||||
|
||||
#endif // NAZARA_MEMORYSTREAM_HPP
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
// 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
|
||||
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
inline bool HashAppend(AbstractHash* hash, const MemoryStream& stream)
|
||||
{
|
||||
return HashAppend(hash, stream.GetBuffer());
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/Core/DebugOff.hpp>
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
// 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_SERIALIZER_HPP
|
||||
#define NAZARA_SERIALIZER_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/Serialization.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class Stream;
|
||||
|
||||
class Serializer
|
||||
{
|
||||
public:
|
||||
inline Serializer(Stream& stream);
|
||||
Serializer(const Serializer&) = default;
|
||||
Serializer(Serializer&&) = default;
|
||||
~Serializer() = default;
|
||||
|
||||
inline Endianness GetDataEndianness() const;
|
||||
inline Stream& GetStream() const;
|
||||
|
||||
inline void SetDataEndianness(Endianness endiannes);
|
||||
inline void SetStream(Stream& stream);
|
||||
|
||||
template<typename T>
|
||||
Serializer& operator<<(const T& value);
|
||||
|
||||
Serializer& operator=(const Serializer&) = default;
|
||||
Serializer& operator=(Serializer&&) = default;
|
||||
|
||||
private:
|
||||
SerializationContext m_serializationContext;
|
||||
};
|
||||
}
|
||||
|
||||
#include <Nazara/Core/Serializer.inl>
|
||||
|
||||
#endif // NAZARA_SERIALIZER_HPP
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
// 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
|
||||
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
inline Serializer::Serializer(Stream& stream)
|
||||
{
|
||||
m_serializationContext.endianness = Endianness_BigEndian;
|
||||
m_serializationContext.stream = &stream;
|
||||
}
|
||||
|
||||
inline Endianness Serializer::GetDataEndianness() const
|
||||
{
|
||||
return m_serializationContext.endianness;
|
||||
}
|
||||
|
||||
inline Stream& Serializer::GetStream() const
|
||||
{
|
||||
return *m_serializationContext.stream;
|
||||
}
|
||||
|
||||
inline void Serializer::SetDataEndianness(Endianness endiannes)
|
||||
{
|
||||
m_serializationContext.endianness = endiannes;
|
||||
}
|
||||
|
||||
inline void Serializer::SetStream(Stream& stream)
|
||||
{
|
||||
m_serializationContext.stream = &stream;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Serializer& Serializer::operator<<(const T& value)
|
||||
{
|
||||
if (!Serialize(m_serializationContext, value))
|
||||
NazaraError("Failed to serialize value");
|
||||
|
||||
return *this;
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/Core/DebugOff.hpp>
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
// 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_UNSERIALIZER_HPP
|
||||
#define NAZARA_UNSERIALIZER_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/Serialization.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class Stream;
|
||||
|
||||
class Unserializer
|
||||
{
|
||||
public:
|
||||
inline Unserializer(Stream& stream);
|
||||
Unserializer(const Unserializer&) = default;
|
||||
Unserializer(Unserializer&&) = default;
|
||||
~Unserializer() = default;
|
||||
|
||||
inline Endianness GetDataEndianness() const;
|
||||
inline Stream& GetStream() const;
|
||||
|
||||
inline void SetDataEndianness(Endianness endiannes);
|
||||
inline void SetStream(Stream& stream);
|
||||
|
||||
template<typename T>
|
||||
Unserializer& operator>>(T& value);
|
||||
|
||||
Unserializer& operator=(const Unserializer&) = default;
|
||||
Unserializer& operator=(Unserializer&&) = default;
|
||||
|
||||
private:
|
||||
UnserializationContext m_unserializationContext;
|
||||
};
|
||||
}
|
||||
|
||||
#include <Nazara/Core/Unserializer.inl>
|
||||
|
||||
#endif // NAZARA_UNSERIALIZER_HPP
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
// 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
|
||||
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
inline Unserializer::Unserializer(Stream& stream)
|
||||
{
|
||||
m_unserializationContext.endianness = Endianness_BigEndian;
|
||||
m_unserializationContext.stream = &stream;
|
||||
}
|
||||
|
||||
inline Endianness Unserializer::GetDataEndianness() const
|
||||
{
|
||||
return m_unserializationContext.endianness;
|
||||
}
|
||||
|
||||
inline Stream& Unserializer::GetStream() const
|
||||
{
|
||||
return *m_unserializationContext.stream;
|
||||
}
|
||||
|
||||
inline void Unserializer::SetDataEndianness(Endianness endiannes)
|
||||
{
|
||||
m_unserializationContext.endianness = endiannes;
|
||||
}
|
||||
|
||||
inline void Unserializer::SetStream(Stream& stream)
|
||||
{
|
||||
m_unserializationContext.stream = &stream;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Unserializer& Unserializer::operator>>(T& value)
|
||||
{
|
||||
if (!Unserialize(m_unserializationContext, &value))
|
||||
NazaraError("Failed to serialize value");
|
||||
|
||||
return *this;
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/Core/DebugOff.hpp>
|
||||
|
|
@ -1,90 +0,0 @@
|
|||
// 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
|
||||
|
||||
#include <Nazara/Core/MemoryStream.hpp>
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
MemoryStream::MemoryStream() :
|
||||
Stream(OpenMode_ReadWrite),
|
||||
m_pos(0)
|
||||
{
|
||||
}
|
||||
|
||||
MemoryStream::MemoryStream(const void* ptr, unsigned int size) :
|
||||
MemoryStream()
|
||||
{
|
||||
m_buffer.Resize(size);
|
||||
std::memcpy(m_buffer.GetBuffer(), ptr, size);
|
||||
}
|
||||
|
||||
void MemoryStream::Clear()
|
||||
{
|
||||
m_buffer.Clear();
|
||||
m_pos = 0;
|
||||
}
|
||||
|
||||
bool MemoryStream::EndOfStream() const
|
||||
{
|
||||
return m_pos >= m_buffer.size();
|
||||
}
|
||||
|
||||
const ByteArray& MemoryStream::GetBuffer() const
|
||||
{
|
||||
return m_buffer;
|
||||
}
|
||||
|
||||
UInt64 MemoryStream::GetCursorPos() const
|
||||
{
|
||||
return m_pos;
|
||||
}
|
||||
|
||||
const UInt8* MemoryStream::GetData() const
|
||||
{
|
||||
return m_buffer.GetConstBuffer();
|
||||
}
|
||||
|
||||
UInt64 MemoryStream::GetSize() const
|
||||
{
|
||||
return m_buffer.size();
|
||||
}
|
||||
|
||||
bool MemoryStream::SetCursorPos(UInt64 offset)
|
||||
{
|
||||
m_pos = std::min<UInt64>(offset, m_buffer.size());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void MemoryStream::FlushStream()
|
||||
{
|
||||
// Nothing to flush
|
||||
}
|
||||
|
||||
std::size_t MemoryStream::ReadBlock(void* buffer, std::size_t size)
|
||||
{
|
||||
std::size_t readSize = std::min<std::size_t>(size, static_cast<std::size_t>(m_buffer.size() - m_pos));
|
||||
|
||||
if (buffer)
|
||||
std::memcpy(buffer, m_buffer.GetBuffer() + m_pos, readSize);
|
||||
|
||||
m_pos += readSize;
|
||||
return readSize;
|
||||
}
|
||||
|
||||
std::size_t MemoryStream::WriteBlock(const void* buffer, std::size_t size)
|
||||
{
|
||||
std::size_t endPos = static_cast<std::size_t>(m_pos + size);
|
||||
if (endPos > m_buffer.size())
|
||||
m_buffer.Resize(endPos);
|
||||
|
||||
std::memcpy(m_buffer.GetBuffer(), buffer, size);
|
||||
|
||||
m_pos = endPos;
|
||||
return size;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue