Skip to content

Commit

Permalink
you can now do bits [1,64] in serialize_bits
Browse files Browse the repository at this point in the history
  • Loading branch information
gafferongames committed Dec 26, 2023
1 parent 80edd33 commit 534fb65
Showing 1 changed file with 38 additions and 58 deletions.
96 changes: 38 additions & 58 deletions serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -1266,19 +1266,43 @@ namespace serialize
do \
{ \
serialize_assert( bits > 0 ); \
serialize_assert( bits <= 32 ); \
uint32_t uint32_value = 0; \
if ( Stream::IsWriting ) \
{ \
uint32_value = (uint32_t) value; \
} \
if ( !stream.SerializeBits( uint32_value, bits ) ) \
serialize_assert( bits <= 64 ); \
if ( bits <= 32 ) \
{ \
return false; \
uint32_t uint32_value = 0; \
if ( Stream::IsWriting ) \
{ \
uint32_value = (uint32_t) value; \
} \
if ( !stream.SerializeBits( uint32_value, bits ) ) \
{ \
return false; \
} \
if ( Stream::IsReading ) \
{ \
value = uint32_value; \
} \
} \
if ( Stream::IsReading ) \
else \
{ \
value = uint32_value; \
uint32_t hi = 0, lo = 0; \
if ( Stream::IsWriting ) \
{ \
lo = uint32_t( uint64_t(value) & 0xFFFFFFFF ); \
hi = uint32_t( uint64_t(value) >> 32 ); \
} \
if ( !stream.SerializeBits( lo, 32 ) ) \
{ \
return false; \
} \
if ( !stream.SerializeBits( hi, bits - 32 ) ) \
{ \
return false; \
} \
if ( Stream::IsReading ) \
{ \
value = ( uint64_t(hi) << 32 ) | lo; \
} \
} \
} while (0)

Expand Down Expand Up @@ -1383,7 +1407,7 @@ namespace serialize
@param stream The stream object. May be a read, write or measure stream.
@param value The float value to serialize.
*/
#define serialize_compressed_float(stream, value, min, max, res) \
#define serialize_compressed_float(stream, value, min, max, res) \
do \
{ \
if (!serialize::serialize_compressed_float_internal(stream, value, min, max, res)) \
Expand All @@ -1392,50 +1416,6 @@ namespace serialize
} \
} while (0)

/**
Serialize a 32 bit unsigned integer to the stream (read/write/measure).
This is a helper macro to make unified serialize functions easier.
Serialize macros returns false on error so we don't need to use exceptions for error handling on read. This is an important safety measure because packet data comes from the network and may be malicious.
IMPORTANT: This macro must be called inside a templated serialize function with template \<typename Stream\>. The serialize method must have a bool return value.
@param stream The stream object. May be a read, write or measure stream.
@param value The unsigned 32 bit integer value to serialize.
*/

#define serialize_uint32( stream, value ) serialize_bits( stream, value, 32 );

template <typename Stream> bool serialize_uint64_internal( Stream & stream, uint64_t & value )
{
uint32_t hi = 0, lo = 0;
if ( Stream::IsWriting )
{
lo = value & 0xFFFFFFFF;
hi = value >> 32;
}
serialize_bits( stream, lo, 32 );
serialize_bits( stream, hi, 32 );
if ( Stream::IsReading )
{
value = ( uint64_t(hi) << 32 ) | lo;
}
return true;
}

/**
Serialize a 64 bit unsigned integer to the stream (read/write/measure).
This is a helper macro to make unified serialize functions easier.
Serialize macros returns false on error so we don't need to use exceptions for error handling on read. This is an important safety measure because packet data comes from the network and may be malicious.
IMPORTANT: This macro must be called inside a templated serialize function with template \<typename Stream\>. The serialize method must have a bool return value.
@param stream The stream object. May be a read, write or measure stream.
@param value The unsigned 64 bit integer value to serialize.
*/

#define serialize_uint64( stream, value ) \
do \
{ \
if ( !serialize::serialize_uint64_internal( stream, value ) ) \
return false; \
} while (0)

template <typename Stream> bool serialize_double_internal( Stream & stream, double & value )
{
union DoubleInt
Expand All @@ -1448,7 +1428,7 @@ namespace serialize
{
tmp.double_value = value;
}
serialize_uint64( stream, tmp.int_value );
serialize_bits( stream, tmp.int_value, 64 );
if ( Stream::IsReading )
{
value = tmp.double_value;
Expand Down Expand Up @@ -1675,7 +1655,7 @@ namespace serialize
}

uint32_t value = current;
serialize_uint32( stream, value );
serialize_bits( stream, value, 32 );
if ( Stream::IsReading )
{
current = value;
Expand Down Expand Up @@ -2048,7 +2028,7 @@ struct TestObject

serialize_double( stream, data.double_value );

serialize_uint64( stream, data.uint64_value );
serialize_bits( stream, data.uint64_value, 64 );

serialize_int_relative( stream, data.a, data.int_relative );

Expand Down

0 comments on commit 534fb65

Please sign in to comment.