提升模板派生类

时间:2015-07-06 15:34:21

标签: c++ serialization boost polymorphism

我有一个模板化的基类Base和一个模板化的派生类Derived,我想序列化。
下面的简化代码编译并运行,但不从基类序列化数据成员。

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

演示: Live On Coliru

仅输出:

<?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>

那么如何才能获得Base::uBase::v的序列化?我尝试使用BOOST_CLASS_EXPORT(Derived);但没有成功。

奖金问题:在这种情况下,如何使用宏BOOST_SERIALIZATION_BASE_OBJECT_NVP

2 个答案:

答案 0 :(得分:3)

您忘记将NVP实际写入存档:

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

随着更改,它现在打印

<?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>

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

答案 1 :(得分:3)

我只回答“奖金”问题,因为@sehe已经回答了主要问题:

要使用BOOST_SERIALIZATION_BASE_OBJECT_NVPserialize必须是成员函数(请参阅nvp.hpp中的定义)。

以下工作:我必须输入基类来使宏工作:

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

请参阅 Live On Coliru