The number of bytes required to represent a serialized object cannot be easily known in advance. It is possible to calculate it, but it's an expensive process. The object gets serialized twice: the first time to count the number of bytes, and the second time for real. This is expensive, but can be convenient sometimes:
#include <x/serialize.H> std::vector<std::string> strarray; // ... x::serialize::sizeof_iterator cnt; cnt(strarray); std::vector<char> buffer; buffer.resize(cnt.counter()); char *ptr=&buffer[0]; x::serialize::iterator<char *> o_iter(ptr); o_iter(strarray);
sizeof_iterator
is a subclass of
iterator
that throws
away the serialized bytes, after counting them.
Its counter
()
method returns the resulting byte count.
This example allocates
a suitable buffer, then the object gets serialized
for real, using a plain char *
as an output iterator.
It goes without saying that the serialized objects cannot be modified between the first and the second serialization pass in a manner that affects their serialized representation.