序列化使用boost序列化类,而不会改变类 [英] serializing classes using boost serialization without changing the class

查看:153
本文介绍了序列化使用boost序列化类,而不会改变类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这片code有我们每一个时间来写一个类iefrom 模板<等级档案> AR &安培; BOOST_SERIALIZATION_NVP(B)。我们怎样才能使它短?
以及我们如何可以序列化STL容器?

 类Employee
{
私人的:
    友元类的boost ::系列化::访问;
    模板<类归档和GT;无效连载(归档和放大器; AR,
            const的无符号整型版)
    {
        AR&安培; BOOST_SERIALIZATION_NVP(一);
        AR&安培; BOOST_SERIALIZATION_NVP(二);
    }    int类型的;
    INT B:上市:
    员工(INT A,INT B)
    {
         这 - >一种=一个;
         这 - > B =;
    }};


解决方案

我建议你先从文档:)

STL容器是可序列化时,包括相关的标题:

 的#include<升压/系列化/ map.hpp>
#包括LT&;升压/系列化/ string.hpp>

其他的东西主机支持开箱即用。

您的可能的制作一般包装为已取得reflectible通过其他方式类型(例如融合顺序,Qt的QObject对象等)

this piece of code has to be written every time we make a class i.e.from template<class archive> to ar & BOOST_SERIALIZATION_NVP(b) . How can we make it short? and how can we serialize the stl containers ?

 class Employee  
{ 
private:      
    friend class boost::serialization::access;  
    template<class Archive> void serialize(Archive & ar,  
            const unsigned int version)   
    {  
        ar & BOOST_SERIALIZATION_NVP(a);  
        ar & BOOST_SERIALIZATION_NVP(b);  
    }  

    int a; 
    int b; 

public:  
    Employee(int a, int b)  
    { 
         this->a = a; 
         this->b = b; 
    } 

}; 

解决方案

I suggest you start with the documentation :)

STL containers are serializable when you include the relevant header:

#include <boost/serialization/map.hpp>
#include <boost/serialization/string.hpp>

A host of other stuff is supported out of the box.

You could make generic wrappers for types that have already been made "reflectible" by other means (e.g. Fusion Sequences, Qt QObjects etc.)

这篇关于序列化使用boost序列化类,而不会改变类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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