Boost serialization of template derived class

coincoin

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 ?

sehe

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;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to wrap derived template class with Boost::python?

From Dev

boost::serialization - Serializing a class derived from a generic attribute / feature container

From Dev

Set tracking traits of template class in boost serialization to reduce memory consumption

From Dev

enum class and boost serialization

From Dev

enum class and boost serialization

From Dev

Trying to send an derived class through a boos::asio socket using boost::serialization

From Dev

boost serialization of struct derived from struct list

From Dev

boost serialization and deserialization for different derived classes

From Dev

boost serialization of struct derived from struct list

From Dev

Derived class template

From Dev

Boost serialization unregistered class errors

From Dev

boost::serialization: is it possible to avoid template functions?

From Dev

Template Specialization Not Working with Derived Class

From Dev

Template function argument in derived class

From Dev

Use derived class for template constructor

From Dev

boost python with template class

From Dev

boost python with template class

From Dev

Boost serialization of reference member abstract class

From Dev

Boost serialization of base class of final subclass error

From Dev

Boost : serialization of class instances that contain pointers

From Dev

Visibility of a base class constructor in a derived template class

From Dev

boost shared_ptr and derived class

From Dev

How to expose derived class function in boost python

From Dev

boost::serialisation for inherited template class

From Dev

boost::serialisation for inherited template class

From Dev

How to get MFC serialization going without a CWinApp-derived class?

From Dev

How to call constructor of a template base class in a template derived class?

From Dev

Calling constructor of a template base class from a template derived class

From Dev

Virtual function implemented as template in derived class

Related Related

  1. 1

    How to wrap derived template class with Boost::python?

  2. 2

    boost::serialization - Serializing a class derived from a generic attribute / feature container

  3. 3

    Set tracking traits of template class in boost serialization to reduce memory consumption

  4. 4

    enum class and boost serialization

  5. 5

    enum class and boost serialization

  6. 6

    Trying to send an derived class through a boos::asio socket using boost::serialization

  7. 7

    boost serialization of struct derived from struct list

  8. 8

    boost serialization and deserialization for different derived classes

  9. 9

    boost serialization of struct derived from struct list

  10. 10

    Derived class template

  11. 11

    Boost serialization unregistered class errors

  12. 12

    boost::serialization: is it possible to avoid template functions?

  13. 13

    Template Specialization Not Working with Derived Class

  14. 14

    Template function argument in derived class

  15. 15

    Use derived class for template constructor

  16. 16

    boost python with template class

  17. 17

    boost python with template class

  18. 18

    Boost serialization of reference member abstract class

  19. 19

    Boost serialization of base class of final subclass error

  20. 20

    Boost : serialization of class instances that contain pointers

  21. 21

    Visibility of a base class constructor in a derived template class

  22. 22

    boost shared_ptr and derived class

  23. 23

    How to expose derived class function in boost python

  24. 24

    boost::serialisation for inherited template class

  25. 25

    boost::serialisation for inherited template class

  26. 26

    How to get MFC serialization going without a CWinApp-derived class?

  27. 27

    How to call constructor of a template base class in a template derived class?

  28. 28

    Calling constructor of a template base class from a template derived class

  29. 29

    Virtual function implemented as template in derived class

HotTag

Archive