boost.serialization的特点
1. 支持XML和二进制两种形式
2. 支持类的版本号
3. 支持深度指针存储
4. 支持多个指针共享数据
5. 支持STL容器存储
6. 支持原生数组存储
7. 支持非侵入性的设计,序列化的类不需要继承特殊的类,不需要写特殊的方法(原来的类中要存储的属性必须共有)
我遇到的问题
1. 如果要存储STL容器,要引入头文件 #include <boost/serialization/vector.hpp>。
2. 如果之前引入了<windows.h>,编译会出错,原因是windows.h中定义了min,max宏,我们可以在#include<windows.h>之前把min,max禁止,使用#define NOMINMAX。或者使用#undef min和#undef max
我存在的疑问
XML中属性的意义尚不明确,手工添加数据可能有危险
XML中的类型用数字来表示,不知道读取原理,如果代码添加了新的类会不会对原有数据有影响
参考
详细说明见:$/boost_1_37_0/libs/serialization/doc/index.html
后面附了一个示例和一个生成的XML
附录A
///////////////////////////////////////////////
//file : .h
#pragma once
#include <vector>
#include <boost/serialization/base_object.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/vector.hpp>
using namespace std;
class DBConfigItem
{
public:
DBConfigItem(const String& dsn,const String & usrName,const String & password,size_t connectCnt);
DBConfigItem(const DBConfigItem & item);
DBConfigItem();
~DBConfigItem();
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & BOOST_SERIALIZATION_NVP(dataSourceName);
ar & BOOST_SERIALIZATION_NVP(usrName);
ar & BOOST_SERIALIZATION_NVP(usrPassword);
ar & BOOST_SERIALIZATION_NVP(connectCount);
}
private:
String dataSourceName;
String usrName;
String usrPassword;
size_t connectCount;
};
//=====================================================================
class DBConfig
{
public:
DBConfig();
~DBConfig();
void create();
void destroy();
void readFromXml(const String & pathName);
void saveToXml(const String & pathName);
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & BOOST_SERIALIZATION_NVP(cfgItemList);
}
private:
typedef std::vector<DBConfigItem*>::iterator iterator;
vector< DBConfigItem * > cfgItemList;
};
/////////////////////////////////////////////////////////
file : cpp
#include <iomanip>
#include <iostream>
#include <fstream>
#include <boost/archive/tmpdir.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
void DBConfig::readFromXml(const String & pathName)
{
// open the archive
std::ifstream ifs(pathName.c_str());
assert(ifs.good());
boost::archive::xml_iarchive ia(ifs);
// restore the schedule from the archive
//ia >> BOOST_SERIALIZATION_NVP(*this);
ia >> boost::serialization::make_nvp(BOOST_PP_STRINGIZE(config1), *this);
}
void DBConfig::saveToXml(const String & pathName)
{
// make an archive
std::ofstream ofs(pathName.c_str());
assert(ofs.good());
boost::archive::xml_oarchive oa(ofs);
oa << boost::serialization::make_nvp(BOOST_PP_STRINGIZE(config1), *this);
}
附录B
一个序列化后的XML文件
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization (View Source for full doctype...)>
- <boost_serialization signature="serialization::archive" version="5">
- <config1 class_id="0" tracking_level="0" version="0">
- <cfgItemList class_id="1" tracking_level="0" version="0">
<count>3</count>
<item_version>0</item_version>
- <item class_id="2" tracking_level="1" version="0" object_id="_0">
<dataSourceName>scott</dataSourceName>
<usrName>root</usrName>
<usrPassword>admin</usrPassword>
<connectCount>3</connectCount>
</item>
- <item class_id_reference="2" object_id="_1">
<dataSourceName>scott</dataSourceName>
<usrName>root</usrName>
<usrPassword>admin</usrPassword>
<connectCount>3</connectCount>
</item>
<item class_id_reference="2" object_id_reference="_1" />
</cfgItemList>
</config1>
</boost_serialization>