Chinaunix首页 | 论坛 | 博客
  • 博客访问: 266175
  • 博文数量: 28
  • 博客积分: 3065
  • 博客等级: 中校
  • 技术积分: 610
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-30 15:30
文章分类

全部博文(28)

文章存档

2011年(3)

2009年(9)

2008年(16)

我的朋友

分类: C/C++

2008-10-28 21:50:16

   所谓序列化,个人理解就是对数据结构的“打包”和“解包”。经过“打包”后的数据结构为字节序列,可以保存为二进制、文本、XML等格式,boost中称这种保存为“archive”;同样,“解包”就是从“archive”中读出这些字节序列,然后再还原为数据结构。
    当然序列化的作用也是显而易见的,比如说,有两个进程p1和p2,p1经过一系列处理产生结果并保存在数据结构d1里,而d1恰好又是p2的输入。你该怎么办?除了共享内存,可能你会想到将d1中的每个数据单元通过管道输出到p2,然后p2再重组数据结构;如果d1结构单一还是可以的,要是d1集指针、成员对象、hash、继承等于一体,这样做非累死你不可。其实,序列化的工作正是来完成你的上述操作,只不过人家走正规化了,只要你通过它的操作符(<<)告诉它数据结构中哪些是要通信的数据单元就OK了。
    1.序列化动态库:-L/usr/local/lib -lboost_serialization-gcc34-mt
    2.库中已经提供的可序列化数据类型,可以在/usr/local/include/boost-1_36/boost/serialization查到
    3.“打包”和“解包”可以定义在同一个函数serialize()做,也可以分开,“打包”用save(),“解包”用load()。
 
以下例子的测试环境是:Linux 2.6.9,gcc 3.4.3,boost 1.36
 

/* demo.cpp */
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
using namespace __gnu_cxx;

#include
#include

class subdemo
{
 private:
  friend class boost::serialization::access;
  friend ostream& operator << (ostream &os, const subdemo &sd)
  {
   os << sd.x << " " << sd.s;
   return os;
  }
  template
  void serialize(Archive & ar, const unsigned int version)
  {
   ar & x & s;
  }
  int x;
  string s;
 public:
  subdemo(){}
  subdemo(int i, string c) : x(i), s(c){}
};

class basedemo
{
 protected:
  friend class boost::serialization::access;
  template
  void serialize(Archive & ar, const unsigned int version)
  {
   ar & x & s;
  }
  int x;
  string s;
 public:
  basedemo(){}
  basedemo(int i, string c) : x(i), s(c){}
};

class demo : public basedemo
{
 private:
  friend class boost::serialization::access;
  template
  void serialize(Archive & ar, const unsigned int version)
  {
   ar & boost::serialization::base_object(*this);
   ar & i;
   ar & str;
   ar & vec;
   ar & hm;
   ar & sd;
  }
  int i;
  string str;
  vector vec;
  hash_map *hm;
  subdemo sd;
 public:
  demo()
  {
   hm = new hash_map;
  }
  demo(int a): basedemo(4321, "Perfect"), i(a), str("hello"), vec(2,4), sd(1234, "world")
  {
   hm = new hash_map;
   hm->insert(pair(11,"aaa"));
   hm->insert(pair(22,"bbb"));
  }
  void print()
  {
   cout << "test int:" << endl;
   cout << "-------------------" << endl;
   cout << i << endl << endl;

   cout << "test string:" << endl;
   cout << "-------------------" << endl;
   cout << str << endl << endl;

   cout << "test vector:" << endl;
   cout << "-------------------" << endl;
   copy(vec.begin(), vec.end(), ostream_iterator(cout," "));
   cout << endl << endl;

   cout << "test hash_map pointer:" << endl;
   cout << "-------------------" << endl;
   for (hash_map::iterator iter=hm->begin(); iter!=hm->end(); ++iter)
    cout << iter->first << " " << iter->second << endl;
   cout << endl;

   cout << "test member object:" << endl;
   cout << "-------------------" << endl;
   cout << sd << endl << endl;

   cout << "test derived class:" << endl;
   cout << "-------------------" << endl;
   cout << x << " " << s << endl << endl;
  }
  ~demo()
  {
   delete hm;
  }
};

int main() {
    ofstream ofs("filename");
 /* stringstream ss; */

    demo d(1);
 boost::archive::text_oarchive oa(ofs);
 /* boost::archive::text_oarchive oa(cout); */
 /* boost::archive::text_oarchive oa(ss); */
 oa << d;
 ofs.close();

    demo newd;
 ifstream ifs("filename");
 boost::archive::text_iarchive ia(ifs);
 /* boost::archive::text_iarchive ia(ss); */
 ia >> newd;
 newd.print();
 ifs.close();

    return 0;
}

编译:
 

g++ -c -I/usr/local/include/boost-1_36 demo.cpp
g++ -o demo demo.o -L/usr/local/lib -lboost_serialization-gcc34-mt

 

参考boost手册: 

阅读(3037) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~