OutputArchive

An OutputArchive allows for serialisation of objects to a buffer in memory, regarded as a sequence of octets.

An efficient binary format is used (i.e. not using printable text). See serialisation.

OutputArchive provides extremely high performance.

An OutputArchive is essentially a octet_t* which represents a pointer to the next octet in a memory buffer to be written. There are implicit conversions to and from a octet_t*.


class OutputArchive
{
public:
    // Allow implicit conversions to/from a octet_t*
    OutputArchive(octet_t* p) : p_(p) {}
    operator octet_t*() const { return p_; }
private:
    octet_t* p_;
};

An OutputArchive can only be used to write to a pre-allocated contiguous block of memory. The implementation is designed for the highest possible performance. There are no output buffer overflow checks, it is assumed the buffer is large enough.

Usually an SizeArchive is used to measure the required size of the output buffer before serialisation begins.

Serialisation of user defined types

For every type T that supports serialisation, the following function is implemented.


void Serialise(OutputArchive& ar, const T& x)

Therefore there can be many overloads of Serialise (i.e. adhoc polymorphism).

The cxUtils library implements the Serialise function for the following types:

bool, 
char, signed char, unsigned char, char16_t, char32_t, wchar_t, 
short, unsigned short,
int, unsigned int,
long, unsigned long,
long long, unsigned long long,
float, double, long double,
std::pair, std::array, std::basic_string, std::vector, std::deque,
std::forward_list, std::list,
std::map, std::multimap, std::unordered_map, std::unordered_multimap,
std::set, std::multiset, std::unordered_set, std::unordered_multiset,
ceda::xdeque, ceda::VectorOfByte, ceda::xvector, ceda::CompressedInt, ceda::schema_t
ceda::HPTime, ceda::HPTimeStamp, ceda::HPTimeSpan

Insertion operation on an OutputArchive

For convenience clients can use operator<< to serialise variables/objects. E.g.


ar << x << y << z;

is shorthand for


Serialise(ar,x);
Serialise(ar,y);
Serialise(ar,z);

This is achieved with a single implementation of operator<< for an OutputArchive:


template<typename T>
inline OutputArchive& operator<<(OutputArchive& ar, const T& x) 
{ 
    Serialise(ar,x);
    return ar; 
}

Note that the intention is to have many overloads of Serialise rather than operator<<.