![]() |
PIP 5.6.1
Platform-Independent Primitives
|
Class for binary de/serialization. More...
#include <pichunkstream.h>
Classes | |
| struct | Chunk |
| Nested template struct for chunk data. More... | |
| struct | ChunkConst |
| Nested template struct for constant chunk data. More... | |
Public Types | |
| enum | Version { Version_1 , Version_2 = 2 } |
| Version of data packing. More... | |
Public Member Functions | |
| PIChunkStream (const PIByteArray &data) | |
| Constructs stream for read from "data". | |
| PIChunkStream (PIByteArray *data=0, Version v=Version_2) | |
| Constructs stream for read or write to/from "data", or empty stream for write if "data" = 0. | |
| PIChunkStream (Version v) | |
| Constructs empty stream for write with version "v". | |
| ~PIChunkStream () | |
| Destructor. | |
| template<typename T > | |
| PIChunkStream & | add (int id, const T &data) |
| Add to this stream chunk with ID "id" and value "data". More... | |
| template<typename T > | |
| bool | extract (PIBinaryStream< T > &stream, bool read_all=false) |
| Extract PIByteArray from "stream" and set it current stream. If "read_all" then call readAll() after extract. Returns if has data to read. More... | |
| void | setSource (const PIByteArray &data) |
| Sets source buffer for read from "data". | |
| void | setSource (PIByteArray *data) |
| Sets source buffer for read or write to/from "data", or empty stream for write if "data" = 0. | |
| PIByteArray | data () const |
| Returns internal buffer with written data. | |
| bool | atEnd () const |
| Returns if there is end of stream. | |
| Version | version () const |
| Returns stream version. | |
| int | read () |
| Reads one chunk from stream and returns its ID. More... | |
| void | readAll () |
| Read all chunks from stream. This function just index input data. More... | |
| int | getID () |
| Returns last read chunk ID. More... | |
| template<typename T > | |
| T | getData () const |
| Returns value of last read chunk. More... | |
| template<typename T > | |
| T | getData (int id) const |
| Returns value of chunk with ID "id". More... | |
| template<typename T > | |
| void | get (T &v) const |
| Places value of last read chunk into "v". More... | |
| template<typename T > | |
| const PIChunkStream & | get (int id, T &v) const |
| Places value of chunk with ID "id" into "v". More... | |
| template<typename T > | |
| PIChunkStream & | set (int id, const T &v) |
| Replaces value of chunk with ID "id" to "v". More... | |
Static Public Member Functions | |
| template<typename T > | |
| static ChunkConst< T > | chunk (int id, const T &data) |
| Returns chunk with ID "id" and value "data" for write to stream. More... | |
Class for binary de/serialization.
PIChunkStream provides a binary markup serialization stream that allows reading and writing structured data chunks with IDs.
This class provides very handly mechanism to store and restore values to and from PIByteArray. The main advantage of using this class is that your binary data become independent from order and collection of your values.
PIChunkStream works with items called "chunk". Chunk is an ID, size and any value that can be stored and restored to/from PIChunkStream with stream operators << and >>.
To construct PIChunkStream for writing data use any non-default constructor. Empty constructor creates internal buffer that can be accessed by function data(). Non-empty constructor works with given byte array.
To read chunks from byte array use function read() that returns ID of readed chunk. Then you can get value of this chunk with functions getData() or get(), but you should definitely know type of this value. You can read from byte array while atEnd() if false.
Using simple operator and cascade serialization: Prepare your structs to work with PIChunkStream:
Old-style writing to PIChunkStream:
Fastest reading from PIChunkStream:
And next code show how to serialize your struct with PIChunkStream:
... and deserialize:
|
inlinestatic |
Returns chunk with ID "id" and value "data" for write to stream.
|
inline |
Add to this stream chunk with ID "id" and value "data".
|
inline |
| int PIChunkStream::read | ( | ) |
| void PIChunkStream::readAll | ( | ) |
Read all chunks from stream. This function just index input data.
This function indexes input data to allow random access via getData(int id) and get(int id, T & v)
|
inline |
Returns last read chunk ID.
Returns the ID of the last chunk read by read() method
|
inline |
Returns value of last read chunk.
|
inline |
Returns value of chunk with ID "id".
|
inline |
Places value of last read chunk into "v".
|
inline |
Places value of chunk with ID "id" into "v".
|
inline |
Replaces value of chunk with ID "id" to "v".