Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost serialization of template derived class

I have a templated base class Base and a templated derived class Derived which I want to serialize.
The below simplified code compiles and run but do not serialize data members from base class.

#include <vector>
#include <string>
#include <iostream>
#include <sstream>

#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/serialization/access.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/export.hpp>

template<class U, class V>
struct Base {
    Base(U uu, V vv) : u(uu), v(vv) {}
    U u;
    V v;
};

template<class V, class T>
struct Derived : public Base<V, int>, public Base<V, std::string> {
    Derived(T tt) : Base<V, int>(2.0, 4), Base<V, std::string>(3.0, std::string("hello")), t(tt) {}
    T t;
};

// does not work
//BOOST_CLASS_EXPORT(Derived);

namespace boost { namespace serialization {

template<class Archive, class U, class V>
void serialize(Archive & ar, Base<U,V> &obj, const unsigned int version) {
    ar& BOOST_SERIALIZATION_NVP(obj.u);
    ar& BOOST_SERIALIZATION_NVP(obj.v);
}

template<class Archive, class V, class T>
void serialize(Archive & ar, Derived<V,T> &obj, const unsigned int version) {
    boost::serialization::make_nvp("Base1", 
        boost::serialization::base_object<Base<V, int>>(obj) );
    boost::serialization::make_nvp("Base2", 
        boost::serialization::base_object<Base<V, std::string>>(obj) );
    // does not work
    // ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base<V, int>); 
    // ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base<V, std::string>);
    ar& BOOST_SERIALIZATION_NVP(obj.t);
}

}} // end namespace

int main() {
    Derived<double, int> a(10);

    std::ostringstream archive_ostream;
    boost::archive::xml_oarchive oa(archive_ostream);
    oa << BOOST_SERIALIZATION_NVP(a); 
    std::cout << archive_ostream.str() << std::endl;
}

Demo : Live On Coliru

Output is only :

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="12">
<a class_id="0" tracking_level="0" version="0">
    <obj.t>10</obj.t>
</a>

So how can I get the serialization of Base::u and Base::v ? I have tried using BOOST_CLASS_EXPORT(Derived); without success.

Bonus question : How can I also use the macro BOOST_SERIALIZATION_BASE_OBJECT_NVP in this case ?

like image 617
coincoin Avatar asked Jul 06 '15 15:07

coincoin


2 Answers

You forgot to actually write the NVP to the archive:

ar & boost::serialization::make_nvp("Base1", boost::serialization::base_object<Base<V, int>>(obj) );
ar & boost::serialization::make_nvp("Base2", boost::serialization::base_object<Base<V, std::string>>(obj) );

With that changed, it now prints

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="12">
<a class_id="0" tracking_level="0" version="0">
    <Base1 class_id="1" tracking_level="0" version="0">
        <obj.u>2.00000000000000000e+00</obj.u>
        <obj.v>4</obj.v>
    </Base1>
    <Base2 class_id="2" tracking_level="0" version="0">
        <obj.u>3.00000000000000000e+00</obj.u>
        <obj.v>hello</obj.v>
    </Base2>
    <obj.t>10</obj.t>
</a>

See Live On Coliru

#include <vector>
#include <string>
#include <iostream>
#include <sstream>

#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/serialization/access.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/export.hpp>

template<class U, class V>
struct Base {
    Base(U uu, V vv) : u(uu), v(vv) {}
    U u;
    V v;
};

template<class V, class T>
struct Derived : public Base<V, int>, public Base<V, std::string> {
    Derived(T tt) : Base<V, int>(2.0, 4), Base<V, std::string>(3.0, std::string("hello")), t(tt) {}
    T t;
};

// does not work
//BOOST_CLASS_EXPORT(Derived);

namespace boost { namespace serialization {

template<class Archive, class U, class V>
void serialize(Archive & ar, Base<U,V> &obj, const unsigned int /*version*/) {
    ar& BOOST_SERIALIZATION_NVP(obj.u);
    ar& BOOST_SERIALIZATION_NVP(obj.v);
}

template<class Archive, class V, class T>
void serialize(Archive & ar, Derived<V,T> &obj, const unsigned int /*version*/) {
    ar & boost::serialization::make_nvp("Base1", boost::serialization::base_object<Base<V, int>>(obj) );
    ar & boost::serialization::make_nvp("Base2", boost::serialization::base_object<Base<V, std::string>>(obj) );
    // does not work
    // ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base<V, int>); 
    // ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base<V, std::string>);
    ar& BOOST_SERIALIZATION_NVP(obj.t);
}

}} // end namespace

int main() {
    Derived<double, int> a(10);

    std::ostringstream archive_ostream;
    boost::archive::xml_oarchive oa(archive_ostream);
    oa << BOOST_SERIALIZATION_NVP(a); 
    std::cout << archive_ostream.str() << std::endl;
}
like image 148
sehe Avatar answered Nov 14 '22 05:11

sehe


I only answer the "bonus" question since @sehe already answered the main one:

In order to use BOOST_SERIALIZATION_BASE_OBJECT_NVP, serialize must be a member function (see the definition in nvp.hpp).

The following works: I had to typedef the Base classes to make the macro work:

template<class Archive>
void serialize(Archive & ar,  const unsigned int version)
{

    typedef Base<V, int> Base1; 
    typedef Base<V, std::string> Base2; 

    ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base1);
    ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base2);

    ar& BOOST_SERIALIZATION_NVP(t);
}

See Live On Coliru

like image 40
m.s. Avatar answered Nov 14 '22 07:11

m.s.