SizeArchive
is used to calculate the number of octets which will be written to an OutputArchive
.
This allows for pre-allocating the buffer before using the OutputArchive
.
SizeArchive
is essentially a ssize_t
(i.e. a signed version of a std::size_t
).
There are implicit conversions to and from a ssize_t
.
class SizeArchive
{
public:
// Allow implicit conversions to/from a ssize_t
SizeArchive(ssize_t size) : size_(size) {}
operator ssize_t() const { return size_; }
private:
ssize_t size_;
};
For every type T
that supports serialisation, the following function is implemented.
void Serialise(SizeArchive& 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
For convenience clients can use operator<<
to call Serialise
. 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 a SizeArchive
:
template<typename T>
inline SizeArchive& operator<<(SizeArchive& ar, const T& x)
{
Serialise(ar,x);
return ar;
}
Note that the intention is to have many overloads of Serialise
rather than operator<<
.
For example, the following code calculates the size for serialising a uint32
and a float64
:
uint32 a;
float64 b;
SizeArchive ar(0);
ar << a << b;
ssize_t size = ar;
assert(size == 12);