Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will Boost.Serialization work straightforwardly with virtual diamond inheritance?

I am looking at Boost.Serialization for the first time, and I cannot find a clear assessment (or instructions) regarding the serialization of a virtual diamond inheritance structure.

Consider the following class hierarchy:

class A { int a; }

class B1 : public virtual A { int b1; }

class B2 : public virtual A { int b2; }

class B3 : public virtual A { int b3; }

class C12 : public virtual B1, public virtual B2 { int c12; }

class C13 : public virtual B1, public virtual B3 { int c13; }

class C23 : public virtual B2, public virtual B3 { int c23; }

class D123 : public virtual C12, public virtual C13, public virtual C23 { int d123; }

What is the proper (hopefully, simple) way to implement serialization within all of these classes using Boost.Serialization?

Note: There are no pointer or reference class members that need to be serialized. Also note: I am happy to use dynamic_cast<> to assure that any pointers or references to any classes in this hierarchy are of the desired, derived-most type: I am simply concerned about how to properly, and cleanly, guarantee that all BASE-class data members are properly serialized, and deserialized, along with the current class being serialized.

like image 394
Dan Nissenbaum Avatar asked Nov 19 '12 04:11

Dan Nissenbaum


1 Answers

When serializing objects with virtual base classes, you will have to explicitly enable object tracking for the virutal base classes:

BOOST_CLASS_TRACKING(virtual_base, boost::serialization::track_always)

From object tracking:

In a diamond heritance structure with a virtual base class, object tracking will prevent redundant save/load invocations. So here is one case where it might be convenient to override the default tracking trait. (Note: in a future version the default will be reimplemented to automatically track classes used as virtual bases).

As for de/serializing base classes, use:

archive & BOOST_SERIALIZATION_BASE_OBJECT_NVP(base_class);

before serializing member variables (archive & BOOST_SERIALIZATION_NVP(variable)) with intrusive serialization. For non-intrusive serialization it's up to you to deal with all the member variables involved.

All this is based on the assumption that you de/serialize a non-polymorphic class (with virtual base classes) through a pointer or reference of the most derived type.

like image 132
Anonymous Coward Avatar answered Sep 30 '22 19:09

Anonymous Coward