Public Member Functions

kNet::DataSerializer Class Reference

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>

List of all members.

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].

Detailed Description

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.


Constructor & Destructor Documentation

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.

Todo:
Support dynamic resizing.
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.

Todo:
Support dynamic resizing.
kNet::DataSerializer::DataSerializer ( std::vector< char > &  data,
size_t  maxBytes 
) [explicit]

Instantiates a new DataSerializer that writes to the given vector.

Parameters:
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.

Parameters:
maxBytes The maximum number of bytes that the message can take up space.

Member Function Documentation

template<typename T >
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.

template<typename VLEType >
void kNet::DataSerializer::AddVLE ( u32  value  ) 

Todo:
To support other than 1/2/4-byte VLE's, convert to using AddBits().

void kNet::DataSerializer::AppendBits ( u32  value,
int  amount 
)

Appends the given number of bits to the stream.

Parameters:
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.

Todo:
Check out-of-bounds.

size_t kNet::DataSerializer::BytesFilled (  )  const [inline]
Returns:
The number of bytes filled so far. Partial bits at the end are rounded up to constitute a full byte.

Referenced by kNet::MessageConnection::Send(), and kNet::MessageConnection::SendStruct().

size_t kNet::DataSerializer::BitsFilled (  )  const [inline]
Returns:
The number of bits filled so far total.
size_t kNet::DataSerializer::Capacity (  )  const [inline]
Returns:
The total capacity of the buffer we are filling into.

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines