Boost logo

Boost Users :

Subject: Re: [Boost-users] serilalization of union
From: Andrew Holden (aholden_at_[hidden])
Date: 2009-01-08 09:50:37


Robert Ramey wrote:
>
> This is what boost variant serialization does.
>
> Robert Ramey

I suspected as much. Probably a bit more elegant than my brute force implementation though.

I also suspect that OP already has something similar to that bool I added. If so, he doesn't need to add another bool to serialize the union; just make sure he serializes enough members to know which union member is valid before serializing that member.

Either that or he uses the union to directly access the bits in the float. If that's the case, he can just serialize the float and be done with it.

Andrew Holden wrote:
>>> niranjan bangera wrote:
>>>> union myunion
>>>> {
>>>>
>>>> uint32_t a;
>>>> float t;
> The best solutions would become more obvious if you know which type
> is valid at a higher level. For example, if you have a structure
> like this:
>
> struct UnionWrapper
> {
> myunion the_union;
> bool is_float; /* Set to true if and only if the float member
> is used */
>
> template <class Archive>
> void serialize (Archive &ar, unsigned int const version)
> {
> ar & is_float; /*Do this first so deserialization knows which
> union member to get. */
>
> if (is_float)
> {
> ar & the_union.t;
> }
> else
> {
> ar & the_union.a;
> }
> }
> };

_______________________________________________
Boost-users mailing list
Boost-users_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net