On Monday, December 17, 2012 6:49:21 AM UTC-7, Jeff Flinn wrote:
> How does this compare to boost serialization with portable_binary archive?
>
>
>
> Jeff
I've never used that module - boost's Serialization was of course
examined as an alternative initially, however it did not appear to meet
my needs at the time. I took a look through some of the portable_binary
examples and correct me if I'm wrong but it looks like portable_binary
archive specifies the underlying format in Archive and uses that when
serializing data - ie:
struct Object
{
....
int x;
int y;
int z;
template <typename Archive>
void serialize (Archive& ar, const unsigned int)
{
ar & x & y & z;
}
};
Specifically, ar above would serialize x and y using the format dictated
by Archive - be that big endian, little endian, or some other scheme. In
my use case x and y could use dramatically different packing schemes - for
example, x may need to be serialized as a little endain while y and z may
need to be packed into a short using some bit packing scheme. A scheme
similar to the above could, in principal, still be used by wrapping x, y,
and z in structures that alter how they're serialized (I don't know if boost's
Serialization supports this, but it's clearly possible):
struct Object
{
...
as_little_endian <int> x;
...
// not clear how definitions extending
// across multiple variables should be handled
template <typename Archive>
void serialize (Archive& ar, const unsigned int)
{
ar & x & ...;
}
};
The reason why I did not end up pursuing that design is that fact that
definitions similar to the above fundamentally either split or duplicate
information in the protocol specification. In both examples above a
variable's type is first defined as part of the structure definition, then
the order in which the variables are serialized is defined as part of the
serialize function. That probably seems trivial

- it was actually the
original motivation behind the design. The serialization of certain
complex structures was defined in methods similar to serialize - they
contained non-trivial control paths. This turned debugging and updating
the protocol into an absolute nightmare as the protocol definition was
effectively split into three parts; the structure definition, the read
function, and the write function. Clearly moving to a syntax similar to
serialize would have mitigated that to a degree, however fundamentally it
wasn't possible eliminate the division entirely under that design.
In addition to the above, there are serious advantages to defining the
layout of structures inside an object's type information. Specifically, a
kind of comile-time introspection is possible using this approach - if you
examine comparable.hpp you'll note that it defines methods for member-wise
comparisons between an arbitrary pair of serializable objects. Similar
wrappers may be written naturally for generic data member visitors, such
as print methods or similar for the purpose of data visualization. Other
very neat possible uses exist - for example, as the base class order is
mutable, one could solve a bin-packing problem at compile-time to
optimally construct an arbitrary object to minimize space. Containers
could be written that mutate the serialization order of a set of variables
based on the value of a compile-time constant, allowing build-specific
protocols to be trivially written. The list is endless