Core: Add serialization interface
Former-commit-id: cfa749dba1b6f23ef8f38519e0bc9ad9492e3db3
This commit is contained in:
parent
167f3e4a27
commit
be01b6f3b4
|
|
@ -9,8 +9,11 @@
|
||||||
|
|
||||||
#include <Nazara/Prerequesites.hpp>
|
#include <Nazara/Prerequesites.hpp>
|
||||||
#include <Nazara/Core/Enums.hpp>
|
#include <Nazara/Core/Enums.hpp>
|
||||||
|
#include <Nazara/Core/InputStream.hpp>
|
||||||
|
#include <Nazara/Core/OutputStream.hpp>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
namespace Nz
|
namespace Nz
|
||||||
{
|
{
|
||||||
|
|
@ -26,8 +29,15 @@ namespace Nz
|
||||||
template<typename T>
|
template<typename T>
|
||||||
struct TypeTag {};
|
struct TypeTag {};
|
||||||
|
|
||||||
|
inline bool Serialize(OutputStream* output, bool value);
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
std::enable_if_t<std::is_arithmetic<T>::value, bool> Serialize(OutputStream* output, T value);
|
||||||
|
|
||||||
|
inline bool Unserialize(InputStream* input, bool* value);
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
std::enable_if_t<std::is_arithmetic<T>::value, bool> Unserialize(InputStream* input, T* value);
|
||||||
}
|
}
|
||||||
|
|
||||||
#include <Nazara/Core/Algorithm.inl>
|
#include <Nazara/Core/Algorithm.inl>
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@
|
||||||
// Merci aussi à Freedom de siteduzero.com
|
// Merci aussi à Freedom de siteduzero.com
|
||||||
|
|
||||||
#include <Nazara/Core/AbstractHash.hpp>
|
#include <Nazara/Core/AbstractHash.hpp>
|
||||||
|
#include <Nazara/Core/Error.hpp>
|
||||||
#include <Nazara/Core/Debug.hpp>
|
#include <Nazara/Core/Debug.hpp>
|
||||||
|
|
||||||
namespace Nz
|
namespace Nz
|
||||||
|
|
@ -75,6 +76,60 @@ namespace Nz
|
||||||
|
|
||||||
seed = static_cast<std::size_t>(b * kMul);
|
seed = static_cast<std::size_t>(b * kMul);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline bool Serialize(OutputStream* output, bool value)
|
||||||
|
{
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
if (output->Write(&value, sizeof(T)) == sizeof(T))
|
||||||
|
{
|
||||||
|
// Write down everything as little endian
|
||||||
|
#if NAZARA_BIG_ENDIAN
|
||||||
|
SwapBytes(&value, sizeof(T));
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool Unserialize(InputStream* input, bool* value)
|
||||||
|
{
|
||||||
|
NazaraAssert(value, "Invalid pointer");
|
||||||
|
|
||||||
|
UInt8 buffer;
|
||||||
|
if (input->Read(&buffer, 1) == 1)
|
||||||
|
{
|
||||||
|
*value = (buffer == 1);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
std::enable_if_t<std::is_arithmetic<T>::value, bool> Unserialize(InputStream* input, T* value)
|
||||||
|
{
|
||||||
|
NazaraAssert(value, "Invalid pointer");
|
||||||
|
|
||||||
|
if (input->Read(value, sizeof(T)) == sizeof(T))
|
||||||
|
{
|
||||||
|
// Write down everything as little endian
|
||||||
|
#if NAZARA_BIG_ENDIAN
|
||||||
|
SwapBytes(&value, sizeof(T));
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#include <Nazara/Core/DebugOff.hpp>
|
#include <Nazara/Core/DebugOff.hpp>
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,9 @@ namespace Nz
|
||||||
virtual std::size_t Read(void* buffer, std::size_t size) = 0;
|
virtual std::size_t Read(void* buffer, std::size_t size) = 0;
|
||||||
virtual String ReadLine(unsigned int lineSize = 0);
|
virtual String ReadLine(unsigned int lineSize = 0);
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
InputStream& operator>>(T& value);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
inline InputStream();
|
inline InputStream();
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,21 @@
|
||||||
// This file is part of the "Nazara Engine - Core module"
|
// This file is part of the "Nazara Engine - Core module"
|
||||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
|
#include <Nazara/Core/Error.hpp>
|
||||||
|
|
||||||
namespace Nz
|
namespace Nz
|
||||||
{
|
{
|
||||||
inline InputStream::InputStream() :
|
inline InputStream::InputStream() :
|
||||||
Stream(OpenMode_Current)
|
Stream(OpenMode_Current)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
InputStream& InputStream::operator>>(T& value)
|
||||||
|
{
|
||||||
|
if (!Unserialize(this, &value))
|
||||||
|
NazaraError("Failed to unserialize value");
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,9 @@
|
||||||
|
|
||||||
namespace Nz
|
namespace Nz
|
||||||
{
|
{
|
||||||
|
class ByteArray;
|
||||||
|
class String;
|
||||||
|
|
||||||
class NAZARA_CORE_API OutputStream : virtual public Stream
|
class NAZARA_CORE_API OutputStream : virtual public Stream
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
@ -23,6 +26,9 @@ namespace Nz
|
||||||
bool Write(const String& string);
|
bool Write(const String& string);
|
||||||
virtual std::size_t Write(const void* buffer, std::size_t size) = 0;
|
virtual std::size_t Write(const void* buffer, std::size_t size) = 0;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
OutputStream& operator<<(const T& value);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
inline OutputStream();
|
inline OutputStream();
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,21 @@
|
||||||
// This file is part of the "Nazara Engine - Core module"
|
// This file is part of the "Nazara Engine - Core module"
|
||||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
|
#include <Nazara/Core/Error.hpp>
|
||||||
|
|
||||||
namespace Nz
|
namespace Nz
|
||||||
{
|
{
|
||||||
inline OutputStream::OutputStream() :
|
inline OutputStream::OutputStream() :
|
||||||
Stream(OpenMode_Current)
|
Stream(OpenMode_Current)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
OutputStream& OutputStream::operator<<(const T& value)
|
||||||
|
{
|
||||||
|
if (!Serialize(this, value))
|
||||||
|
NazaraError("Failed to serialize value");
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,10 +9,11 @@
|
||||||
|
|
||||||
#include <Nazara/Prerequesites.hpp>
|
#include <Nazara/Prerequesites.hpp>
|
||||||
#include <Nazara/Core/Enums.hpp>
|
#include <Nazara/Core/Enums.hpp>
|
||||||
#include <Nazara/Core/String.hpp>
|
|
||||||
|
|
||||||
namespace Nz
|
namespace Nz
|
||||||
{
|
{
|
||||||
|
class String; //< Do not include String.hpp in this file
|
||||||
|
|
||||||
class NAZARA_CORE_API Stream
|
class NAZARA_CORE_API Stream
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@
|
||||||
#define NAZARA_STRING_HPP
|
#define NAZARA_STRING_HPP
|
||||||
|
|
||||||
#include <Nazara/Prerequesites.hpp>
|
#include <Nazara/Prerequesites.hpp>
|
||||||
#include <Nazara/Core/Algorithm.hpp>
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <iosfwd>
|
#include <iosfwd>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
@ -19,6 +18,8 @@ namespace Nz
|
||||||
{
|
{
|
||||||
class AbstractHash;
|
class AbstractHash;
|
||||||
class HashDigest;
|
class HashDigest;
|
||||||
|
class InputStream;
|
||||||
|
class OutputStream;
|
||||||
|
|
||||||
class NAZARA_CORE_API String
|
class NAZARA_CORE_API String
|
||||||
{
|
{
|
||||||
|
|
@ -325,6 +326,8 @@ namespace Nz
|
||||||
};
|
};
|
||||||
|
|
||||||
inline bool HashAppend(AbstractHash* hash, const String& string);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace std
|
namespace std
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
#include <Nazara/Core/Stream.hpp>
|
#include <Nazara/Core/Stream.hpp>
|
||||||
|
#include <Nazara/Core/String.hpp>
|
||||||
#include <Nazara/Core/Debug.hpp>
|
#include <Nazara/Core/Debug.hpp>
|
||||||
|
|
||||||
namespace Nz
|
namespace Nz
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
///TODO: Réécrire une bonne partie des algorithmes employés (Relu jusqu'à 3538)
|
///TODO: Réécrire une bonne partie des algorithmes employés (Relu jusqu'à 3538)
|
||||||
|
|
||||||
#include <Nazara/Core/String.hpp>
|
#include <Nazara/Core/String.hpp>
|
||||||
|
#include <Nazara/Core/Algorithm.hpp>
|
||||||
#include <Nazara/Core/Config.hpp>
|
#include <Nazara/Core/Config.hpp>
|
||||||
#include <Nazara/Core/Error.hpp>
|
#include <Nazara/Core/Error.hpp>
|
||||||
#include <Nazara/Core/Unicode.hpp>
|
#include <Nazara/Core/Unicode.hpp>
|
||||||
|
|
@ -4208,6 +4209,26 @@ namespace Nz
|
||||||
return emptyString;
|
return emptyString;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Serialize(OutputStream* output, const String& string)
|
||||||
|
{
|
||||||
|
if (!Serialize<UInt32>(output, string.GetSize()))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
output->Write(string.GetConstBuffer(), string.GetSize());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Unserialize(InputStream* input, String* string)
|
||||||
|
{
|
||||||
|
UInt32 size;
|
||||||
|
if (!Unserialize(input, &size))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
string->Resize(size);
|
||||||
|
input->Read(string->GetBuffer(), size);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
const unsigned int String::npos(std::numeric_limits<unsigned int>::max());
|
const unsigned int String::npos(std::numeric_limits<unsigned int>::max());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue