zoukankan      html  css  js  c++  java
  • boost Serialization

    简单的开始
    Serialization的中文解释是“串行化” 、“序列化”或者“持久化” ,就是将内存中的对象保存到磁盘中,等到程序再次运行的时候再读取磁盘中的文件恢复原来的对象。下面来看一个简单的例子:
    #include <fstream>
    #include <iostream>
    #include <boost/archive/text_oarchive.hpp>
    #include <boost/archive/text_iarchive.hpp>
    class A
    {
    private:
        //为了能让串行化类库能够访问私有成员,所以要声明一个友元类
        friend class boost::serialization::access;
        //对象的数据
        int a;
        double b;
        //串行化的函数,这一个函数完成对象的保存与恢复
        template<class Archive>
        void serialize(Archive & ar, const unsigned int version)
        {
            ar & a;  //就是这么简单,也可以使用 ar<<a 这样的语法
            ar & b;
        }
    public:
        A(int aa,double bb):a(aa),b(bb){}
        A(){}
        void print(){std::cout<<a<<' '<<b<<std::endl;}
    };   
    int main()
    {
       std::ofstream fout("file.txt");//把对象写到file.txt文件中
       boost::archive::text_oarchive oa(fout);//文本的输出归档类,使用一个ostream来构造
       A obj(1,2.5);
       oa<<obj;//保存obj对象
       fout.close();//关闭文件
      
       std::ifstream fin("file.txt");
       boost::archive::text_iarchive ia(fin);//文本的输入归档类
       A newobj;
       ia>>newobj;//恢复到newobj对象
       newobj.print();
       fin.close();
       system("pause");
       return 0;
    }
     
    从上面可以看出,boost是使用text_oarchive和text_iarchive 类,来完成一个对象的序列化的。使用这两个类的步骤是:
    1.      在源程序中包含boost/archive/text_oarchive.hpp 和 boost/archive/text_iarchive.hpp 这两个文件。
    2.      为需要序列化的类添加一个template<class Archive> void serialize(Archive & ar, const unsigned int version)的模版成员函数。
    3.      如果需要对象中包含私有成员的话,需要把boost::serialization::access类声明为友元。
    4.      在主函数中,创建一个输出文件流对象, 使用这个对象构造一个text_oarchive对象,然后就可以使用<<操作符来输出对象了。
    5.      最后,同样的,使用text_iarchive来恢复对象。
     
    继承
    如果要序列化一个子类的话,方法是不同的。例:
    #include <boost/serialization/base_object.hpp> //一定要包含此头文件
    class B:A
    {
        friend class boost::serialization::access;
        char c;
        template<class Archive>
        void serialize(Archive & ar, const unsigned int version)
        {
            ar & boost::serialization::base_object<A>(*this);//注意这里
            ar & c;
        }
    public:
        ...
    };   
     
    对子类进行序列化的步骤是:
    1.      包含boost/serialization/base_object.hpp头文件
    2.      在serialize模版函数中,使用ar & boost::serialization::base_object<父类>(*this)这样的语法来保存父类的数据,不能直接调用父类的serialize函数
     
    STL容器
    如果要序列化一个STL容器,要使用boost自带的头文件,不能直接#include<vector>
    例如:
    #include <boost/serialization/list.hpp>// Serialization中特定的头文件,在list.hpp中已经包含了stl的list头文件
    Class A
    {
     ...
     list<int> list;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
     {
          ar & list;
     }
    ...
    }
    在Serialization中,类似的头文件还有vector.hpp string.hpp set.hpp map.hpp slist.hpp等等。
     
    数组和指针
    对于数组和指针可以直接序列化,例:
    Class A
    {
     ...
     int a[10];
     int *b
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
     {
          ar & a;
          ar & b;
     }
    ...
    }
     
    其他的archive类
    除了text_iarchive和text_oarchive之外,还有其他的archive类,可以把对象保存成不同格式的文件。
    // a portable text archive
    boost::archive::text_oarchive(ostream &s) // saving
    boost::archive::text_iarchive(istream &s) // loading
     
    // a portable text archive using a wide character stream
    boost::archive::text_woarchive(wostream &s) // saving
    boost::archive::text_wiarchive(wistream &s) // loading
     
    // a non-portable native binary archive
    boost::archive::binary_oarchive(ostream &s) // saving
    boost::archive::binary_iarchive(istream &s) // loading
     
    // a portable XML archive
    boost::archive::xml_oarchive(ostream &s) // saving
    boost::archive::xml_iarchive(istream &s) // loading
     
    // a portable XML archive which uses wide characters - use for utf-8 output
    boost::archive::xml_woarchive(wostream &s) // saving
    boost::archive::xml_wiarchive(wistream &s) // loading
     
  • 相关阅读:
    Web Storage中的sessionStorage和localStorage
    Javascrip获取页面URL信息
    SqlServer 查看备份文件中逻辑文件信息的Sql语句
    实用ExtJS教程100例-011:ExtJS Form 使用JSON数据赋值和取值
    实用ExtJS教程100例-010:ExtJS Form异步加载和提交数据
    实用ExtJS教程100例-009:ExtJS Form无刷新文件上传
    实用ExtJS教程100例-008:使用iframe填充ExtJS Window组件
    实用ExtJS教程100例-007:ExtJS中Window组件最小化
    实用ExtJS教程100例-006:ExtJS中Window的用法示例
    实用ExtJS教程100例-005:自定义对话框Ext.MessageBox.show
  • 原文地址:https://www.cnblogs.com/wubiyu/p/1372582.html
Copyright © 2011-2022 走看看