2

I am using boost to serialize\de-serialize several classes to disk. I am refactoring many classes which require changes the class hierarchy. I hope the following pseudo code is sufficiently clear in communicating my situation:

The original design was similar to the following:

class Alpha : public BaseOne, public BaseTwo
{
    //member data omitted
    void serialize(Archive & ar, const unsigned int version);
}
inline void Alpha::serialize(Archive & ar, const unsigned int version)
{
    ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(BaseOne);
    ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(BaseTwo);
    // also serialize member data
}

class Beta: public Alpha
{
    //member data omitted
    void serialize(Archive & ar, const unsigned int version);
}

inline void Beta:::serialize(Archive & ar, const unsigned int version)
{
    ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Alpha);
    // also serialize member data
}

The class hierarchy has changed to the following:

class Alpha : public BaseOne
{
    void serialize(Archive & ar, const unsigned int version);
}

class Beta: public Alpha, public BaseTwo
{
    void serialize(Archive & ar, const unsigned int version);
}

Is it possible to de-serialize data saved with previous class hierarchy into the new design?

Regards, John

JohnG
  • 55
  • 3

1 Answers1

2

I'll assume you're familiar with the regular versioning support in Boost Serialization (Boost class serialization, change in member types)).

In this case, I'd say you can't expect this to work, because the type information is "in the way" in the data stream (and can potentially no longer be mapped). I'd suggest making the old hierarchy still available, e.g. using inline namespaces:

namespace MyNS
{
      namespace V1 {

             struct Alpha;
             struct Beta;

      }

      inline namespace V2 {

             struct Alpha;
             struct Beta;

             struct Document { /* */ }

             Document upgrade(V1::Document&&); 
      }

 }
Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633