DataSerializer is a helper class that can be used to serialize data types to a stream of raw bits suitable for disk storage or network transfer. More...
#include <DataSerializer.h>
Public Member Functions | |
| DataSerializer (size_t maxBytes_=128 *1024) | |
| Instantiates a new DataSerializer that maintains its own buffer space for serialization. | |
| DataSerializer (size_t maxBytes_, const SerializedMessageDesc *msgTemplate) | |
| Instantiates a new DataSerializer that maintains its own buffer space for serialization and serializes using a template. | |
| DataSerializer (char *data_, size_t maxBytes_) | |
| Instantiates a new DataSerializer that writes its data into the given fixed-size buffer. | |
| DataSerializer (char *data_, size_t maxBytes_, const SerializedMessageDesc *msgTemplate) | |
| Instantiates a new DataSerializer that writes its data into the given fixed-size buffer, using a message template. | |
| DataSerializer (std::vector< char > &data, size_t maxBytes) | |
| Instantiates a new DataSerializer that writes to the given vector. | |
| DataSerializer (std::vector< char > &data, size_t maxBytes, const SerializedMessageDesc *msgTemplate) | |
| Instantiates a new DataSerializer that writes to the given vector, using a message template. | |
| template<typename T > | |
| void | Add (const T &value) |
| Appends a single element of the passed type. | |
| template<typename VLEType > | |
| void | AddVLE (u32 value) |
| void | AppendBits (u32 value, int amount) |
| Appends the given number of bits to the stream. | |
| void | AddString (const char *str) |
| Adds a given string as length-prepended (not zero-padded). | |
| void | AddString (const std::string &str) |
| See void kNet::DataSerializer::AddString(const char *str); "". | |
| template<typename T > | |
| void | AddArray (const T *data, u32 count) |
| Appends the given amount of elements from the passed array. | |
| void | AddAlignedByteArray (const void *data, u32 numBytes) |
| Adds an array of bytes to the stream. | |
| void | SetVaryingElemSize (u32 count) |
| Sets the number of instances in a varying element. | |
| void | SkipNumBytes (size_t numBytes) |
| Advances the stream pointer the given number of bytes. | |
| size_t | BytesFilled () const |
| size_t | BitsFilled () const |
| size_t | Capacity () const |
| size_t | ByteOffset () const |
| Returns the current byte offset the DataSerializer is writing to. | |
| size_t | BitOffset () const |
| Returns the current bit offset in the current byte this DataSerializer is writing to, [0, 7]. | |
DataSerializer is a helper class that can be used to serialize data types to a stream of raw bits suitable for disk storage or network transfer.
| kNet::DataSerializer::DataSerializer | ( | size_t | maxBytes_ = 128 * 1024 |
) | [explicit] |
Instantiates a new DataSerializer that maintains its own buffer space for serialization.
The size limit for the data to serialize can be specified as an optional parameter.
| kNet::DataSerializer::DataSerializer | ( | size_t | maxBytes_, | |
| const SerializedMessageDesc * | msgTemplate | |||
| ) |
Instantiates a new DataSerializer that maintains its own buffer space for serialization and serializes using a template.
| kNet::DataSerializer::DataSerializer | ( | std::vector< char > & | data, | |
| size_t | maxBytes | |||
| ) | [explicit] |
Instantiates a new DataSerializer that writes to the given vector.
| maxBytes | The maximum number of bytes that the message can take up space. |
| kNet::DataSerializer::DataSerializer | ( | std::vector< char > & | data, | |
| size_t | maxBytes, | |||
| const SerializedMessageDesc * | msgTemplate | |||
| ) |
Instantiates a new DataSerializer that writes to the given vector, using a message template.
| maxBytes | The maximum number of bytes that the message can take up space. |
| void kNet::DataSerializer::Add | ( | const T & | value | ) |
Appends a single element of the passed type.
If you are using a serialization template to aid in serialization, the type T may be any of the types bit, u8, s8, u16, s16, u32, s32, u64, s64, float, double, const char * or std::string. If you are not using a serialization template, you may pass in any type that is a POD type and can be reinterpret_casted to a u8 buffer and memcpy'd to a byte buffer.
| void kNet::DataSerializer::AddVLE | ( | u32 | value | ) |
| void kNet::DataSerializer::AppendBits | ( | u32 | value, | |
| int | amount | |||
| ) |
Appends the given number of bits to the stream.
| value | The variable where the bits are taken from. The bits are read from the LSB first, towards the MSB end of the value. | |
| amount | The number of bits to read, in the range [1, 32]. |
References kNet::LSB().
Referenced by SetVaryingElemSize().
| void kNet::DataSerializer::AddString | ( | const char * | str | ) |
Adds a given string as length-prepended (not zero-padded).
In the message template, use a parameter of type 's8' with dynamicCount field set to e.g. 8.
References SetVaryingElemSize().
| void kNet::DataSerializer::AddAlignedByteArray | ( | const void * | data, | |
| u32 | numBytes | |||
| ) |
Adds an array of bytes to the stream.
The contents in the stream must be byte-aligned when calling this function. A serialization template may not be used when calling this function.
| void kNet::DataSerializer::SetVaryingElemSize | ( | u32 | count | ) |
Sets the number of instances in a varying element.
Requires a template to be present to use this.
References AppendBits().
Referenced by AddString().
| void kNet::DataSerializer::SkipNumBytes | ( | size_t | numBytes | ) |
Advances the stream pointer the given number of bytes.
Use this method if you have used an external method of filling data to the stream.
| size_t kNet::DataSerializer::BytesFilled | ( | ) | const [inline] |
Referenced by kNet::MessageConnection::Send(), and kNet::MessageConnection::SendStruct().
| size_t kNet::DataSerializer::BitsFilled | ( | ) | const [inline] |
| size_t kNet::DataSerializer::Capacity | ( | ) | const [inline] |
1.7.1