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... | |
| DataSerializer & | operator<< (uint8_t c) |
| Add an uint8_t to the output. More... | |
| DataSerializer & | operator<< (int8_t c) |
| Add an int8_t to the output. More... | |
| DataSerializer & | operator<< (uint16_t val) |
| Add an uint16_t to the output. More... | |
| DataSerializer & | operator<< (int16_t val) |
| Add an int16_t to the output. More... | |
| DataSerializer & | operator<< (uint32_t val) |
| Add an uint32_t to the output. More... | |
| DataSerializer & | operator<< (int32_t val) |
| Add an int32_t to the output. More... | |
| DataSerializer & | operator<< (uint64_t val) |
| Add an uint64_t to the output. More... | |
| DataSerializer & | operator<< (int64_t val) |
| Add an int64_t to the output. More... | |
| DataSerializer & | operator<< (float val) |
| Add an float to the output. More... | |
| DataSerializer & | operator<< (glm::vec3 val) |
| Add an glm::vec3 to the output. More... | |
| DataSerializer & | operator<< (glm::vec4 val) |
| Add a glm::vec4 to the output. More... | |
| DataSerializer & | operator<< (glm::ivec2 val) |
| Add a glm::ivec2 to the output. More... | |
| DataSerializer & | operator<< (const char *val) |
| Write a null-terminated string into the buffer. More... | |
| DataSerializer & | operator<< (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... | |
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:
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:
|
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.
|
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()
| externalStore | External data store |
| storeLength | Length of the data store |
|
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()
| externalStore | External data store |
| storeLength | Length of the data store |
|
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.
| bytes | Number of bytes to add |
|
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().
|
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.
|
inline |
Whether there's any data in the buffer.
|
inline |
The buffer size limit has been reached.
This can only return true for a statically allocated buffer.
|
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()
|
inline |
Last position that was written to in the buffer. Starts at 0.
|
inline |
Write a null-terminated string into the buffer.
The \0 at the end of the string is also written.
| val | Value to write |
|
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.
| val | Value to write |
|
inline |
Add an float to the output.
| val | Value to add |
|
inline |
Add a glm::ivec2 to the output.
| val | Value to add |
|
inline |
Add an glm::vec3 to the output.
| val | Value to add |
|
inline |
Add a glm::vec4 to the output.
| val | Value to add |
|
inline |
Add an int16_t to the output.
| val | Value to add |
|
inline |
Add an int32_t to the output.
| val | Value to add |
|
inline |
Add an int64_t to the output.
| val | Value to add |
|
inline |
Add an int8_t to the output.
| c | Character to add |
|
inline |
Add an uint16_t to the output.
| val | Value to add |
|
inline |
Add an uint32_t to the output.
| val | Value to add |
|
inline |
Add an uint64_t to the output.
| val | Value to add |
|
inline |
Add an uint8_t to the output.
| c | Character to add |
|
inline |
Current position in the buffer. Starts at 0.
|
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.
| debug | Qt QDebug stream |
| ds | This object |
|
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.
|
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.