Boost logo

Boost Users :

Subject: Re: [Boost-users] [serialization] How to deal with bit fields?
From: Robert Ramey (ramey_at_[hidden])
Date: 2008-09-10 12:10:43


Use split to change into save/load.

Robert Ramey

Sebastian Knopp wrote:
> Hello,
>
> I am a new user of the serialization library and find it is a very
> comfortable solution. But I met a problem concerning the
> serialization of bit fields. For example, look at the following:
>
> struct A
> {
> int a : 1;
> int b : 7;
> };
>
>
> Serialization using the following code does not work, because
> bitfields can not be passed by reference.
>
> template<class Archive>
> void serialize(Archive& ar, A& theA, const unsigned int version)
> {
> ar & make_nvp("a", theA.a);
> ar & make_nvp("b", theA.b);
> }
>
>
> So, I use this workaround:
>
> template<class Archive>
> void serialize(Archive& ar, A& theA.a, const unsigned int version)
> {
> bool myA;
> int myB;
> if (!Archive::is_loading::value)
> {
> myA = (theA.a == 1);
> myB = theA.b;
> }
> ar & make_nvp("a", myA);
> ar & make_nvp("b", myB);
> if (Archive::is_loading::value)
> {
> theA.a = myA ? 1 : 0;
> theA.b = myB;
> }
> }
>
>
> Do you have any suggestions how a more elegant solution could look
> like?
>
> Thanks and bye,
> Sebastian Knopp


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