Encoding

Data can be encoded using different formats, for example in a binary format (the way it is stored locally in a computer's memory) or in a text format (familiar through printf() function). It can be encoded with XML, which has a few variants of encoding, etc.

Often we need to use a few different formats of encoding for the same data, for example one to store data locally and the other to transmit it over a wire. In a first case we need a compact encoding, which can be non-portable, and in the second case we need a portable way of encoding - in ASCI, Unicode, etc.

To achieve this flexibility, we deploy an object factory, which allows adding new methods of encoding with minimal changes in software.

The buffer for encoding is CommBuffer. There are two functions for each object, which we can encode and decode:

int pack(CommBuffer& d,const T& t); and
int unpack(T& t,const CommBuffer& s,ReadControl& c);

These functions can be defined for a particular class T, or a class T can be derived from a DataHook class and appropriate member functions can be defined:

struct DataHook
{
virtual int pack(CommBuffer& d)const=0;
virtual int unpack(const CommBuffer& s,ReadControl& c)=0;
};

int pack(CommBuffer& d,const DataHook& h)
{return h.pack(d);}
int unpack(DataHook& h,const CommBuffer& s,ReadControl& c)
{return h.unpack(s,c);}

The DataCoder class and related functions ( "datacoder.h", "datacoder.cpp" and "datacodervar.h" ) choose an encoding format automatically.

Types of supported formats are stored in "encoding.h"

An object factory is in "encoding.cpp"

Actual encoding in a local format is done by a LocalCoder: "localcoder.h" and "localcoder.cpp"

Encoding in a text (ASCI) format with comma delimited fields is done by an ASCICoder: "ascicoder.h" and "ascicoder.cpp"

Encoding of some of STL's classes: "codestl.h" and "codestl.cpp".

For transmission across networks a special encoding is needed (big endian, with known number of bytes for known types of values): "wirepack.h" and "wirepack.cpp"

Back to Group List