Core: Add serialization interface

Former-commit-id: cfa749dba1b6f23ef8f38519e0bc9ad9492e3db3
This commit is contained in:
Lynix
2015-11-18 18:29:20 +01:00
parent 167f3e4a27
commit be01b6f3b4
10 changed files with 124 additions and 2 deletions

View File

@@ -7,6 +7,7 @@
// Merci aussi à Freedom de siteduzero.com
#include <Nazara/Core/AbstractHash.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/Debug.hpp>
namespace Nz
@@ -75,6 +76,60 @@ namespace Nz
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>