提高使用序列多态存档 [英] Boost Serialization using polymorphic archives

查看:138
本文介绍了提高使用序列多态存档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用的boost ::序列化库为它的序列化需求的客户端 - 服务器应用程序。

我需要序列化和反序列化,似乎并没有工作态对象。文档不说,它支持但没有相关的例子证明什么,我想在这里做。所以,我不是很肯定。我的问题是可以序列化/使用boost反序列多态对象?如果是的话,我究竟做错了什么?

谢谢!

code:

 使用命名空间std;阶级基础{
  上市:
    INT DATA1;    友元类的boost ::系列化::访问;    无效序列化(提高::档案:: polymorphic_iarchive&放大器; AR,
                   const的无符号整型FILE_VERSION){
        AR&安培; DATA1;
    }    无效序列化(提高::档案:: polymorphic_oarchive&放大器; AR,
                   const的无符号整型FILE_VERSION){
        AR&安培; DATA1;
    }  上市:
    基(){};
    基地(INT _D):DATA1(_D){}
    虚拟无效美孚()const的{性病::法院LT&;< 基地<<的std :: ENDL;}
};派生类:公共基础{
  上市:
    INT数据2;    友元类的boost ::系列化::访问;    无效序列化(提高::档案:: polymorphic_iarchive&放大器; AR,
                   const的无符号整型FILE_VERSION){
        AR&安培;提高::系列化:: base_object<碱基GT;(*此)及数据2;
    }    无效序列化(提高::档案:: polymorphic_oarchive&放大器; AR,
                   const的无符号整型FILE_VERSION){
        AR&安培;提高::系列化:: base_object<碱基GT;(*此)及数据2;
    }  上市:
    派生(){};
    衍生(INT _B,诠释_D):基地(_B),数据2(_D){}
    虚拟无效美孚()const的{性病::法院LT&;< 衍生<<的std :: ENDL;}
};INT主(INT ARGC,CHAR *的argv []){
    //客户端
    常量基* B1 =新的派生(1,2);    的std :: ostringstream OSS;
    提高::档案:: polymorphic_text_oarchive OA(OSS);
    OA<< * B1;    //服务器
    基地* B2 =新的派生(3,4);    的std :: istringstream ISS(oss.str());
    提高::档案:: polymorphic_text_iarchive IA(ISS);
    IA>> * B2;    //输出1,OK
    COUT<< B2->&DATA1 LT;< ENDL;    //输出4,为什么没有在派生类中的数据写入?
    COUT<< (将dynamic_cast&下;衍生* GT;(B2)) - > data2的&所述;&下; ENDL;    返回0;
}


解决方案

找到解决方案。我不得不派生类中导出的语句:

  BOOST_CLASS_EXPORT(衍生);

发布的东西,有一些修正工作。

 使用命名空间std;阶级基础{
  上市:
    INT DATA1;    友元类的boost ::系列化::访问;    模板< typename的归档和GT;
    无效连载(归档和放大器; AR,const的无符号整型FILE_VERSION){
        AR&安培; DATA1;
    }  上市:
    基(){};
    基地(INT _D):DATA1(_D){}
    虚拟无效美孚()const的{性病::法院LT&;< 基地<<的std :: ENDL;}
};派生类:公共基础{
  上市:
    INT数据2;    友元类的boost ::系列化::访问;    模板< typename的归档和GT;
    无效连载(归档和放大器; AR,const的无符号整型FILE_VERSION){
        AR&安培;提高::系列化:: base_object<碱基GT;(*此);
        AR&安培;数据2;
    }  上市:
    派生(){};
    衍生(INT _B,诠释_D):基地(_B),数据2(_D){}
    虚拟无效美孚()const的{性病::法院LT&;< 衍生<<的std :: ENDL;}
};BOOST_CLASS_EXPORT(衍生);INT主(INT ARGC,CHAR *的argv []){
    //客户端
    //分配到基类型
    的std ::的unique_ptr<常量基地> B1(新的派生(1,2));    的std :: ostringstream OSS;
    提高::档案:: text_oarchive的OA(OSS);
    OA&安培; b1.get();    //服务器
    //从基类派生
    的std ::的unique_ptr<碱基GT; B2;    的std :: istringstream ISS(oss.str());
    提高::档案:: text_iarchive IA(ISS);
    {
        基地*温度;
        1A和;温度;
        b2.reset(临时);
    }
    COUT<< B2->&DATA1 LT;< ENDL;
    COUT<< (将dynamic_cast&下;衍生* GT(b2.get())) - > data2的&所述;&下; ENDL;    返回0;
}

I am working on a client-server application that uses boost::serialization library for it's serialization needs.

I need to serialize and deserialize polymorphic objects that does not seem to work. The documentation does say that it is supported but none of the related examples demonstrate what I'm trying to do here. So, I am not very sure. My question is can serialize/deserialize polymorphic objects using boost? If yes, what am I doing wrong here?

Thanks!

code:

using namespace std;  

class base {  
  public:
    int data1;  

    friend class boost::serialization::access;  

    void serialize(boost::archive::polymorphic_iarchive & ar, 
                   const unsigned int file_version) {  
        ar & data1;  
    }  

    void serialize(boost::archive::polymorphic_oarchive & ar, 
                   const unsigned int file_version){  
        ar & data1;  
    }  

  public:  
    base() {};  
    base(int _d) : data1(_d) {}  
    virtual void foo() const {std::cout << "base" << std::endl;}  
};  

class derived : public base {  
  public:  
    int data2;  

    friend class boost::serialization::access;  

    void serialize(boost::archive::polymorphic_iarchive & ar, 
                   const unsigned int file_version) {  
        ar & boost::serialization::base_object<base>(*this) & data2;  
    }  

    void serialize(boost::archive::polymorphic_oarchive & ar, 
                   const unsigned int file_version){  
        ar & boost::serialization::base_object<base>(*this) & data2;  
    }  

  public:  
    derived() {};  
    derived(int _b, int _d) : base(_b), data2(_d) {}  
    virtual void foo() const {std::cout << "derived" << std::endl;}  
};  

int main(int argc, char *argv[]) {  
    // client  
    const base *b1 = new derived(1, 2);  

    std::ostringstream oss;  
    boost::archive::polymorphic_text_oarchive oa(oss);  
    oa << *b1;  

    // server  
    base *b2 = new derived(3, 4);  

    std::istringstream iss(oss.str());  
    boost::archive::polymorphic_text_iarchive ia(iss);  
    ia >> *b2;  

    // prints 1, ok  
    cout << b2->data1 << endl;  

    // prints 4, why wasn't the derived class data written?
    cout << (dynamic_cast<derived*>(b2))->data2 << endl;  

    return 0;  
}  

解决方案

Found a resolution. I had to export the derived class with the statement:

BOOST_CLASS_EXPORT(derived);

Posting something that works with some corrections.

using namespace std;  

class base {  
  public:  
    int data1;  

    friend class boost::serialization::access;  

    template<typename Archive>  
    void serialize(Archive & ar, const unsigned int file_version) {  
        ar & data1;  
    }  

  public:  
    base() {};  
    base(int _d) : data1(_d) {}  
    virtual void foo() const {std::cout << "base" << std::endl;}  
};  

class derived : public base {  
  public:  
    int data2;  

    friend class boost::serialization::access;  

    template<typename Archive>  
    void serialize(Archive & ar, const unsigned int file_version) {  
        ar & boost::serialization::base_object<base>(*this);  
        ar & data2;  
    }  

  public:  
    derived() {};  
    derived(int _b, int _d) : base(_b), data2(_d) {}  
    virtual void foo() const {std::cout << "derived" << std::endl;}  
};  

BOOST_CLASS_EXPORT(derived);  

int main(int argc, char *argv[]) {  
    // client  
    // Assign to base type  
    std::unique_ptr<const base> b1(new derived(1, 2));  

    std::ostringstream oss;  
    boost::archive::text_oarchive oa(oss);  
    oa & b1.get();   

    // server  
    // Retrieve derived type from base  
    std::unique_ptr<base> b2;

    std::istringstream iss(oss.str());
    boost::archive::text_iarchive ia(iss);
    {
        base *temp; 
        ia & temp;
        b2.reset(temp);
    }
    cout << b2->data1 << endl;  
    cout << (dynamic_cast<derived*>(b2.get()))->data2 << endl;  

    return 0;  
}  

这篇关于提高使用序列多态存档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆