简体   繁体   English

继承的模板类的boost :: serialisation

[英]boost::serialisation for inherited template class

I have a virtual template class and a derived class. 我有一个虚拟模板类和一个派生类。 I'm trying now to use boost::serialisation for the derived class. 我现在正在尝试将boost :: serialisation用于派生类。 This is my code so far: 到目前为止,这是我的代码:

template <class T> class classOne { 
public:     
    classOne(){};   
    classOne(T val) : st(val){};    
    virtual ~classOne() {};     
    virtual double GetError() =0; 
protected:  T st;
private:
    friend class boost::serialization::access;
    template<class archive>
    void serialize(archive& ar, const unsigned int version)
    {
         ar & BOOST_SERIALIZATION_NVP(st);
    }
}; 
BOOST_SERIALIZATION_ASSUME_ABSTRACT(classOne<double>);

template <class T> class classTwo : public classOne<T>
{
public:
    classTwo() : classOne<T>(1.0), error(0){};
    classTwo(T val) : classOne<T>(val), error(val){};
    virtual T GetError() {return error;};

private:
    T error;
    friend class boost::serialization::access;
    template<class archive>
    void serialize(archive& ar, const unsigned int version)
    {
            ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(classOne<T>);
            ar & BOOST_SERIALIZATION_NVP(error);
    }
};
int main(int argc, char** argv){
   classTwo<double> ch2(0.5);
   std::ofstream ofs2("test2.xml");
   boost::archive::xml_oarchive oa2(ofs2);
   oa2 << BOOST_SERIALIZATION_NVP(ch2);
   ofs2.close();
}

The code compiles but at runtime i get: 代码可以编译,但是在运行时我得到:

terminate called after throwing an instance of 'boost::archive::xml_archive_exception'
    what():  Invalid XML tag name

When classOne is not template class everything works. 如果classOne不是模板类,则一切正常。 Can somebody help? 有人可以帮忙吗?

Well, the problem is quite obvious: <> don't go well inside XML element tags. 嗯,问题很明显: <>在XML元素标记内效果不佳。

So, here's what you I naively thought of to fix it. 所以,这就是您我天真的想解决的问题。

 
 
 
  
  ar & boost::serialization::make_nvp( "base", static_cast<classOne<T>&>(*this));
 
  

IMPORTANT UPDATE However, just today, I stumbled across the documentation here: 重要更新但是,就在今天,我偶然发现了以下文档:

Note the serialization of the base classes from the derived class. 注意派生类中基类的序列化。 Do NOT directly call the base class serialize functions. 不要直接调用基类的序列化函数。 Doing so might seem to work but will bypass the code that tracks instances written to storage to eliminate redundancies. 这样做似乎可行,但将绕过跟踪写入存储实例以消除冗余的代码。 It will also bypass the writing of class version information into the archive. 它还将绕过将类版本信息写入存档的过程。 For this reason, it is advisable to always make member serialize functions private. 因此,建议始终将成员序列化函数设为私有。

Oops. 哎呀。 So here's the new and correct advice: 因此,这是新的正确建议:

    typedef classOne<T> baseClass;
    ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(baseClass);

Now, you control the name of the xml element by working with the macro and using a typedef to hide the brackets. 现在,您可以通过使用宏并使用typedef隐藏括号来控制xml元素的名称。

Output: 输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="10">
<ch2 class_id="0" tracking_level="0" version="0">
    <baseClass class_id="1" tracking_level="0" version="0">
        <st>0.5</st>
    </baseClass>
<error>0.5</error>
</ch2>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM