Overte C++ Documentation
DataSerializer Class Reference

Data serializer. More...

#include <SerDes.h>

Classes

class  SizeTracker
 RAII tracker of advance position. More...
 

Public Member Functions

 DataSerializer ()
 Construct a dynamically allocated serializer. More...
 
 DataSerializer (char *externalStore, size_t storeLength)
 Construct a statically allocated serializer. More...
 
 DataSerializer (uint8_t *externalStore, size_t storeLength)
 Construct a statically allocated serializer. More...
 
void addPadding (size_t bytes)
 Adds padding to the output. More...
 
DataSerializeroperator<< (uint8_t c)
 Add an uint8_t to the output. More...
 
DataSerializeroperator<< (int8_t c)
 Add an int8_t to the output. More...
 
DataSerializeroperator<< (uint16_t val)
 Add an uint16_t to the output. More...
 
DataSerializeroperator<< (int16_t val)
 Add an int16_t to the output. More...
 
DataSerializeroperator<< (uint32_t val)
 Add an uint32_t to the output. More...
 
DataSerializeroperator<< (int32_t val)
 Add an int32_t to the output. More...
 
DataSerializeroperator<< (uint64_t val)
 Add an uint64_t to the output. More...
 
DataSerializeroperator<< (int64_t val)
 Add an int64_t to the output. More...
 
DataSerializeroperator<< (float val)
 Add an float to the output. More...
 
DataSerializeroperator<< (glm::vec3 val)
 Add an glm::vec3 to the output. More...
 
DataSerializeroperator<< (glm::vec4 val)
 Add a glm::vec4 to the output. More...
 
DataSerializeroperator<< (glm::ivec2 val)
 Add a glm::ivec2 to the output. More...
 
DataSerializeroperator<< (const char *val)
 Write a null-terminated string into the buffer. More...
 
DataSerializeroperator<< (const QString &val)
 Write a QString into the buffer. More...
 
char * buffer () const
 Pointer to the start of the internal buffer. More...
 
size_t pos () const
 Current position in the buffer. Starts at 0. More...
 
size_t length () const
 Last position that was written to in the buffer. Starts at 0. More...
 
size_t capacity () const
 Current capacity of the buffer. More...
 
bool isEmpty () const
 Whether there's any data in the buffer. More...
 
bool isOverflow () const
 The buffer size limit has been reached. More...
 
void rewind ()
 Reset the serializer to the start, clear overflow bit.
 
size_t lastAdvance () const
 Size of the last advance. More...
 

Static Public Attributes

static const int DEFAULT_SIZE = 1500
 Default size for a dynamically allocated buffer. More...
 
static const char PADDING_CHAR = (char)0xAA
 Character to use for padding. More...
 

Friends

QDebug operator<< (QDebug debug, const DataSerializer &ds)
 Dump the contents of this object into QDebug. More...
 

Detailed Description

Data serializer.

When encoding, this class takes in data and encodes it into a buffer. No attempt is made to store version numbers, lengths, or any other metadata. It's entirely up to the user to use the class in such a way that the process can be correctly reversed if variable-length or optional fields are used.

It can operate both on an internal, dynamically-allocated buffer, or an externally provided, fixed-size one. If an external store is used, the class will refuse to add data once capacity is reached and set the overflow flag. When decoding, this class operates on a fixed size buffer. If an attempt to read past the end is made, the read fails, and the overflow flag is set.

The class was written for the maximum simplicity possible and inline friendliness.

Example of encoding:

uint8_t version = 1;
uint16_t count = 1;
glm::vec3 pos{1.5, 2.0, 9.0};
Serializer ser;
ser << version;
ser << count;
ser << pos;
// Serialized data is in ser.buffer(), ser.length() long.
size_t pos() const
Current position in the buffer. Starts at 0.
Definition: SerDes.h:438

This object should be modified directly to add support for any primitive and common datatypes in the code. To support serializing/deserializing classes and structures, implement a operator<< and operator>> functions for that object, eg:

DataSerializer &operator<<(DataSerializer &ser, const Object &o) {
ser << o._borderColor;
ser << o._maxAnisotropy;
ser << o._filter;
return ser;
}
Data serializer.
Definition: SerDes.h:61
DataSerializer & operator<<(uint8_t c)
Add an uint8_t to the output.
Definition: SerDes.h:203

Constructor & Destructor Documentation

◆ DataSerializer() [1/3]

DataSerializer::DataSerializer ( )
inline

Construct a dynamically allocated serializer.

If constructed this way, an internal buffer will be dynamically allocated and grown as needed.

The buffer is SerDes::DEFAULT_SIZE bytes by default, and doubles in size every time the limit is reached.

◆ DataSerializer() [2/3]

DataSerializer::DataSerializer ( char *  externalStore,
size_t  storeLength 
)
inline

Construct a statically allocated serializer.

If constructed this way, the external buffer will be used to store data. The class will refuse to keep adding data if the maximum length is reached, write a critical message to the log, and set the overflow flag.

The flag can be read with isOverflow()

Parameters
externalStoreExternal data store
storeLengthLength of the data store

◆ DataSerializer() [3/3]

DataSerializer::DataSerializer ( uint8_t *  externalStore,
size_t  storeLength 
)
inline

Construct a statically allocated serializer.

If constructed this way, the external buffer will be used to store data. The class will refuse to keep adding data if the maximum length is reached, and set the overflow flag.

The flag can be read with isOverflow()

Parameters
externalStoreExternal data store
storeLengthLength of the data store

Member Function Documentation

◆ addPadding()

void DataSerializer::addPadding ( size_t  bytes)
inline

Adds padding to the output.

The bytes will be set to SerDes::PADDING_CHAR, which is a constant in the source code. Since padding isn't supposed to be read, it can be any value and is intended to be set to something that can be easily recognized in a dump.

Parameters
bytesNumber of bytes to add

◆ buffer()

char* DataSerializer::buffer ( ) const
inline

Pointer to the start of the internal buffer.

The allocated amount can be found with capacity().

The end of the stored data can be found with length().

Returns
Pointer to buffer

◆ capacity()

size_t DataSerializer::capacity ( ) const
inline

Current capacity of the buffer.

If the buffer is dynamically allocated, it can grow.

If the buffer is static, this is a fixed limit.

Returns
size_t

◆ isEmpty()

bool DataSerializer::isEmpty ( ) const
inline

Whether there's any data in the buffer.

Returns
true Something has been written
false The buffer is empty

◆ isOverflow()

bool DataSerializer::isOverflow ( ) const
inline

The buffer size limit has been reached.

This can only return true for a statically allocated buffer.

Returns
true Limit reached
false There is still room

◆ lastAdvance()

size_t DataSerializer::lastAdvance ( ) const
inline

Size of the last advance.

This can be used to get how many bytes were added in the last operation. It is reset on rewind()

Returns
size_t

◆ length()

size_t DataSerializer::length ( ) const
inline

Last position that was written to in the buffer. Starts at 0.

Returns
size_t

◆ operator<<() [1/14]

DataSerializer& DataSerializer::operator<< ( const char *  val)
inline

Write a null-terminated string into the buffer.

The \0 at the end of the string is also written.

Parameters
valValue to write
Returns
SerDes& This object

◆ operator<<() [2/14]

DataSerializer& DataSerializer::operator<< ( const QString &  val)
inline

Write a QString into the buffer.

The string is encoded in UTF-8 and the \0 at the end of the string is also written.

Parameters
valValue to write
Returns
SerDes& This object

◆ operator<<() [3/14]

DataSerializer& DataSerializer::operator<< ( float  val)
inline

Add an float to the output.

Parameters
valValue to add
Returns
SerDes& This object

◆ operator<<() [4/14]

DataSerializer& DataSerializer::operator<< ( glm::ivec2  val)
inline

Add a glm::ivec2 to the output.

Parameters
valValue to add
Returns
SerDes& This object

◆ operator<<() [5/14]

DataSerializer& DataSerializer::operator<< ( glm::vec3  val)
inline

Add an glm::vec3 to the output.

Parameters
valValue to add
Returns
SerDes& This object

◆ operator<<() [6/14]

DataSerializer& DataSerializer::operator<< ( glm::vec4  val)
inline

Add a glm::vec4 to the output.

Parameters
valValue to add
Returns
SerDes& This object

◆ operator<<() [7/14]

DataSerializer& DataSerializer::operator<< ( int16_t  val)
inline

Add an int16_t to the output.

Parameters
valValue to add
Returns
SerDes& This object

◆ operator<<() [8/14]

DataSerializer& DataSerializer::operator<< ( int32_t  val)
inline

Add an int32_t to the output.

Parameters
valValue to add
Returns
SerDes& This object

◆ operator<<() [9/14]

DataSerializer& DataSerializer::operator<< ( int64_t  val)
inline

Add an int64_t to the output.

Parameters
valValue to add
Returns
SerDes& This object

◆ operator<<() [10/14]

DataSerializer& DataSerializer::operator<< ( int8_t  c)
inline

Add an int8_t to the output.

Parameters
cCharacter to add
Returns
SerDes& This object

◆ operator<<() [11/14]

DataSerializer& DataSerializer::operator<< ( uint16_t  val)
inline

Add an uint16_t to the output.

Parameters
valValue to add
Returns
SerDes& This object

◆ operator<<() [12/14]

DataSerializer& DataSerializer::operator<< ( uint32_t  val)
inline

Add an uint32_t to the output.

Parameters
valValue to add
Returns
SerDes& This object

◆ operator<<() [13/14]

DataSerializer& DataSerializer::operator<< ( uint64_t  val)
inline

Add an uint64_t to the output.

Parameters
valValue to add
Returns
SerDes& This object

◆ operator<<() [14/14]

DataSerializer& DataSerializer::operator<< ( uint8_t  c)
inline

Add an uint8_t to the output.

Parameters
cCharacter to add
Returns
SerDes& This object

◆ pos()

size_t DataSerializer::pos ( ) const
inline

Current position in the buffer. Starts at 0.

Returns
size_t

Friends And Related Function Documentation

◆ operator<<

QDebug operator<< ( QDebug  debug,
const DataSerializer ds 
)
friend

Dump the contents of this object into QDebug.

This produces a dump of the internal state, and an ASCII/hex dump of the contents, for debugging.

Parameters
debugQt QDebug stream
dsThis object
Returns
QDebug

Member Data Documentation

◆ DEFAULT_SIZE

const int DataSerializer::DEFAULT_SIZE = 1500
static

Default size for a dynamically allocated buffer.

Since this is mostly intended to be used for networking, we default to the largest probable MTU here.

◆ PADDING_CHAR

const char DataSerializer::PADDING_CHAR = (char)0xAA
static

Character to use for padding.

Padding should be ignored, so it doesn't matter what we go with here, but it can be useful to set it to something that would be distinctive in a dump.


The documentation for this class was generated from the following files: