输出档案(archive)类似于输出数据流(stream)。数据能通过<< 或 & 操作符存储到档案(archive)中:
ar << data;
ar & data;
输入档案(archive)类似于输入数据流(stream)。数据能通过>> 或 & 操作符从档案(archive)中装载。
ar >> data;
ar & data;
对于原始数据类型,当这些操作调用的时候,数据是简单的“被存储/被装载” “到/从” 档案(archive)。对于类(class)数据类型,类的serialize 函数被调用。对上面的操作,每个serialize 函数用来“存储/装载”其数据成员。这个处理采用递归的方式,直到所有包含在类中的数据“被存储/被装载”。
一个非常简单的情形
通常用serialize 函数来存储和装载类的数据成员。
这个库包含一个叫 demo.cpp 的程序,用于介绍如何用这个库。下面,我们从这个demo摘录代码,来介绍这个库应用的最简单情形。
#include <fstream>
// include headers that implement a archive in simple text format
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
/
// gps coordinate
//
// illustrates serialization for a simple type
//
class gps_position
{
private:
friend class boost::serialization::access;
// When the class Archive corresponds to an output archive, the
// & operator is defined similar to <<. Likewise, when the class Archive
// is a type of input archive the & operator is defined similar to >>.
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & degrees;
ar & minutes;
ar & secon