Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1037685
  • 博文数量: 264
  • 博客积分: 6005
  • 博客等级: 大校
  • 技术积分: 2798
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-08 20:15
文章分类

全部博文(264)

文章存档

2011年(42)

2010年(213)

2009年(4)

2008年(2)

2007年(3)

分类:

2010-08-06 08:49:25

简单的开始

Serialization的中文解释是“串行化” 、“序列化”或者“持久化” ,就是将内存中的对象保存到磁盘中,等到程序再次运行的时候再读取磁盘中的文件恢复原来的对象。下面来看一个简单的例子:

  1. #include 
  2. #include 
  3. #include 
  4. #include 
  5. class A
  6. {
  7. private:
  8.     //为了能让串行化类库能够访问私有成员,所以要声明一个友元类
  9.     friend class boost::serialization::access;
  10.     //对象的数据
  11.     int a;
  12.     double b;
  13.     //串行化的函数,这一个函数完成对象的保存与恢复
  14.     template<class Archive>
  15.     void serialize(Archive & ar, const unsigned int version)
  16.     {
  17.         ar & a;  //就是这么简单,也可以使用 ar<
  18.         ar & b;
  19.     }
  20. public:
  21.     A(int aa,double bb):a(aa),b(bb){}
  22.     A(){}
  23.     void print(){std::cout<' '<
  24. };  
  25. int main()
  26. {
  27.    std::ofstream fout("file.txt");//把对象写到file.txt文件中
  28.    boost::archive::text_oarchive oa(fout);//文本的输出归档类,使用一个ostream来构造
  29.    A obj(1,2.5);
  30.    oa<//保存obj对象
  31.    fout.close();//关闭文件
  32.  
  33.    std::ifstream fin("file.txt");
  34.    boost::archive::text_iarchive ia(fin);//文本的输入归档类
  35.    A newobj;
  36.    ia>>newobj;//恢复到newobj对象
  37.    newobj.print();
  38.    fin.close();
  39.    system("pause");
  40.    return 0;
  41. }

从上面可以看出,boost是使用text_oarchive和text_iarchive 类,来完成一个对象的序列化的。使用这两个类的步骤是:

  1. 在源程序中包含boost/archive/text_oarchive.hpp 和 boost/archive/text_iarchive.hpp 这两个文件。
  2. 为需要序列化的类添加一个template void serialize(Archive & ar, const unsigned int version)的模版成员函数。
  3. 如果需要对象中包含私有成员的话,需要把boost::serialization::access类声明为友元。
  4. 在主函数中,创建一个输出文件流对象, 使用这个对象构造一个text_oarchive对象,然后就可以使用<<操作符来输出对象了。
  5. 最后,同样的,使用text_iarchive来恢复对象。

继承

如果要序列化一个子类的话,方法是不同的。例:

  1. #include  //一定要包含此头文件
  2. class B:A
  3. {
  4.     friend class boost::serialization::access;
  5.     char c;
  6.     template<class Archive>
  7.     void serialize(Archive & ar, const unsigned int version)
  8.     {
  9.         ar & boost::serialization::base_object(*this);//注意这里
  10.         ar & c;
  11.     }
  12. public:
  13.     ...
  14. };   

对子类进行序列化的步骤是:

  1. 包含boost/serialization/base_object.hpp头文件
  2. 在serialize模版函数中,使用ar & boost::serialization::base_object<父类>(*this)这样的语法来保存父类的数据,不能直接调用父类的serialize函数

STL容器

如果要序列化一个STL容器,要使用boost自带的头文件,不能直接#include

例如:

  1. // Serialization中特定的头文件,在list.hpp中已经包含了stl的list头文件
  2. #include 
  3. Class A
  4. {
  5.  ...
  6.  list<int> list;
  7. template<class Archive>
  8. void serialize(Archive & ar, const unsigned int version)
  9.  {
  10.       ar & list;
  11.  }
  12. ...
  13. }

在Serialization中,类似的头文件还有vector.hpp string.hpp set.hpp map.hpp slist.hpp等等。

数组和指针

对于数组和指针可以直接序列化,例:

  1. Class A
  2. {
  3.  ...
  4.  int a[10];
  5.  int *b
  6. template<class Archive>
  7. void serialize(Archive & ar, const unsigned int version)
  8.  {
  9.       ar & a;
  10.       ar & b;
  11.  }
  12. ...
  13. }

其他的archive类

除了text_iarchive和text_oarchive之外,还有其他的archive类,可以把对象保存成不同格式的文件。

  1. // a portable text archive
  2. boost::archive::text_oarchive(ostream &s) // saving
  3. boost::archive::text_iarchive(istream &s) // loading
  4.  
  5. // a portable text archive using a wide character stream
  6. boost::archive::text_woarchive(wostream &s) // saving
  7. boost::archive::text_wiarchive(wistream &s) // loading
  8.  
  9. // a non-portable native binary archive
  10. boost::archive::binary_oarchive(ostream &s) // saving
  11. boost::archive::binary_iarchive(istream &s) // loading
  12.  
  13. // a portable XML archive
  14. boost::archive::xml_oarchive(ostream &s) // saving
  15. boost::archive::xml_iarchive(istream &s) // loading
  16.  
  17. // a portable XML archive which uses wide characters - use for utf-8 output
  18. boost::archive::xml_woarchive(wostream &s) // saving
  19. boost::archive::xml_wiarchive(wistream &s) // loading
原文链接:
http://blog.csdn.net/freerock/archive/2007/08/17/1747928.aspx
阅读(1918) | 评论(0) | 转发(1) |
0

上一篇:boost Serialization 学习

下一篇:boost序列化

给主人留下些什么吧!~~