hpp-util  4.14.0
Debugging tools for the HPP project.
Serialization

Serialization of a class hierarchy can be done as follows. The class hierarchy is:

  • AB inherits from A
  • ABC inherits from AB (which inherits from A), thus there cannot be at the same time ABC and DBC.

In the header file,

  • Use one of the HPP_SERIALIZABLE macros.
  • Except for the base class of the hierarchy (that is A), call BOOST_CLASS_EXPORT_KEY(AB)

In the definition file,

  • implement a function like
    template<class Archive>
    void AB::serialize(Archive & ar, const unsigned int version)
    {
    using namespace boost::serialization;
    (void) version;
    ar & make_nvp("base", base_object<A>(*this)); // declare inheritance.
    // ...
    }
    HPP_SERIALIZATION_IMPLEMENT(AB); // Or the implement macro corresponding
    the called declaration macro.
    Definition: serialization-fwd.hh:38
    HPP_UTIL_DLLAPI const char * version
    Definition: version.cc:40
    #define HPP_SERIALIZATION_IMPLEMENT(type)
    Definition: serialization.hh:76
  • Except for the base class of the hierarchy (that is A), call BOOST_CLASS_EXPORT_IMPLEMENT(AB)

If you know a class will not have any serializable child (i.e. AB exists but there is no no ABC), then you can remove BOOST_CLASS_EXPORT_KEY(AB) and replace BOOST_CLASS_EXPORT_IMPLEMENT(AB) by BOOST_CLASS_EXPORT(AB)