Core/SerializationContext: Replaced currentBitPos and currentByte by [read|write][BitPos][Byte] to handle properly bit reading/writing

This commit is contained in:
Jérôme Leclercq 2018-05-31 16:10:58 +02:00
parent 33fb70b65b
commit 550176e198
5 changed files with 36 additions and 24 deletions

View File

@ -107,6 +107,7 @@ Nazara Engine:
- Added AbstractViewer::Project and AbstractViewer::Unproject methods
- Added AbstractViewer::ProjectDepth method
- Fixed SocketPoller not be able to recover from some errors (like invalid sockets and such)
- ⚠️ Replaced currentBitPos and currentByte fields by [read|write][BitPos][Byte] to handle properly bit reading/writing.
Nazara Development Kit:
- Added ImageWidget (#139)

View File

@ -219,17 +219,17 @@ namespace Nz
*/
inline bool Serialize(SerializationContext& context, bool value, TypeTag<bool>)
{
if (context.currentBitPos == 8)
if (context.writeBitPos == 8)
{
context.currentBitPos = 0;
context.currentByte = 0;
context.writeBitPos = 0;
context.writeByte = 0;
}
if (value)
context.currentByte |= 1 << context.currentBitPos;
context.writeByte |= 1 << context.writeBitPos;
if (++context.currentBitPos >= 8)
return Serialize(context, context.currentByte, TypeTag<UInt8>());
if (++context.writeBitPos >= 8)
return Serialize(context, context.writeByte, TypeTag<UInt8>());
else
return true;
}
@ -291,18 +291,18 @@ namespace Nz
*/
inline bool Unserialize(SerializationContext& context, bool* value, TypeTag<bool>)
{
if (context.currentBitPos == 8)
if (context.readBitPos == 8)
{
if (!Unserialize(context, &context.currentByte, TypeTag<UInt8>()))
if (!Unserialize(context, &context.readByte, TypeTag<UInt8>()))
return false;
context.currentBitPos = 0;
context.readBitPos = 0;
}
if (value)
*value = (context.currentByte & (1 << context.currentBitPos)) != 0;
*value = (context.readByte & (1 << context.readBitPos)) != 0;
context.currentBitPos++;
context.readBitPos++;
return true;
}
@ -341,7 +341,7 @@ namespace Nz
{
NazaraAssert(value, "Invalid data pointer");
context.ResetBitPosition();
context.ResetReadBitPosition();
if (context.stream->Read(value, sizeof(T)) == sizeof(T))
{

View File

@ -20,11 +20,14 @@ namespace Nz
{
Stream* stream;
Endianness endianness = Endianness_BigEndian; //< Default to Big Endian encoding
UInt8 currentBitPos = 8; //< 8 means no bit is currently wrote
UInt8 currentByte; //< Undefined value, will be initialized at the first bit write
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();
inline void ResetBitPosition();
inline void ResetReadBitPosition();
inline void ResetWriteBitPosition();
};
}

View File

@ -7,16 +7,24 @@
namespace Nz
{
/*!
* \brief Reset the current bit cursor
* \brief Reset the current read bit cursor
*/
inline void SerializationContext::ResetReadBitPosition()
{
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::ResetBitPosition()
inline void SerializationContext::ResetWriteBitPosition()
{
currentBitPos = 8;
writeBitPos = 8;
}
}

View File

@ -19,16 +19,16 @@ namespace Nz
/*!
* Write bits to the stream (if any) and reset the current bit cursor
* \see ResetBitPosition
* \see ResetWriteBitPosition
*/
void SerializationContext::FlushBits()
{
if (currentBitPos != 8)
if (writeBitPos != 8)
{
ResetBitPosition();
ResetWriteBitPosition();
// Serialize will reset the bit position
if (!Serialize(*this, currentByte))
if (!Serialize(*this, writeByte))
NazaraWarning("Failed to flush bits");
}
}