boost库xml序列化的详细介绍
今天利用编版本的时间研究了一下boost的序列化,特别是xml序列化的东东,还是有很多收获,记下来怕以后忘记了,人老了,很多东东都记不得了......
(一)研究boost库xml序列化要做准备的工作
a.下一个最新的boost库记住用1.32版本的,这个版本提供了XML序列化的支持
b.windows下用的VC7.0,这个是一定要了,我一直没舍得换我的VC6,结果编译boost的serialization的库用了很多毛招就是编不过,实在没办法了,只好老老实实装了VC7,原因是VC6对模版类的支持不好,比GCC差得很远,VC7要强很多了......其实VC7也是挺好用的。
c.打开boost/libs/serialization/vc7ide下的工程文件,然后编lib就行了
d.做一个新工程作测试用我的叫xmlser
e.记得一定要把工程的RTTI的支持选上,我就是因为这个没选,挠了半天的脑袋,具体设置property page->C/C++->Language->Enable Run-Time Type Info一定要选上(Yes),我就是没注意这个,结果在我序列化std::vector< int > ints; 的时候死要dynamic_cast<T*>上了,有兴趣的可以到MSDN上看一看关于RTTI的文档,这些都准备好就可写测试代码了。
(二)boost的xml序列化的测试代码
最好在做这部之前看看boost提供的助部份的文档,而且最好看看boost/libs/serialization/example下的例子程序,不过我建议最好不要直接用这部份代码做试验,一是比较复杂,二是你会不太了解其中的实现细节,下面就是一步步写得测试代码
// xmlser.cpp : Defines the entry point for the console application.
//#include "stdafx.h"
#include <fstream> #include <boost/serialization/vector.hpp> #include <boost/archive/xml_oarchive.hpp> #include <boost/archive/xml_iarchive.hpp> #pragma comment(lib,"libboost_serialization.lib") class test { public: test() : mvalue(-1) { } template< class archive > void serialize( archive &ar , unsigned int filever ) { ar & BOOST_SERIALIZATION_NVP(mvalue); } int mvalue; }; int _tmain(int argc, _TCHAR* argv[]) { /** * save archive */ { std::vector< int > ints; test* a = new test; ints.push_back( 100 ); ints.push_back( 200 ); std::ofstream ofs("test.xml"); boost::archive::xml_oarchive oa(ofs); int nvalue = 106; oa << BOOST_SERIALIZATION_NVP(nvalue); oa << BOOST_SERIALIZATION_NVP(ints); oa << BOOST_SERIALIZATION_NVP(a); delete a; } /** * load from archive */ { std::vector< int > ints; std::ifstream ofs("test.xml"); boost::archive::xml_iarchive oa(ofs); int nvalue = 0; test* a = NULL; oa >> BOOST_SERIALIZATION_NVP(nvalue); oa >> BOOST_SERIALIZATION_NVP(ints); oa >> BOOST_SERIALIZATION_NVP(a); for( std::vector< int >::iterator i = ints.begin() ; i != ints.end() ; i++ ) { printf("the value of vector %d/n",*i); } } return 0; } 这里要说明的就是如果你想使用std::vector等STL容器的序列化一定要包括#include <boost/serialization/vector.hpp>这句话,因为这个文件定义了template<class Archive, class Allocator> inline void serialize( Archive & ar, STD::vector<bool, Allocator> & t, const unsigned int file_version ){ boost::serialization::split_free(ar, t, file_version); }这样一个模版函数,这就是这个文件的主要作用。有了这个文件你就会发现boost和stl合作做序列化是一件多么让人愉快的事情啊,呵呵,boost库真的很好用啊,快点儿成为标准吧......。
另外关于序列化东东,这里用了一个宏BOOST_SERIALIZATION_NVP,感兴趣的可以看看宏展开是什么,xml_serialization序列化是一个pair,原因很简单,XML结点肯定需要一个结点的名字,和结点的值对吧,所以这个宏让你可以直接用你想序列化的对象生成一个叫NVP的对象,你可以不用这个宏直接写,写成下面的形式也是可以的oa >> boost::serialization::make_nvp("vector",ints);这样可以指定你想要结点名字我个人觉得这样可能会更有用一些。
关于类的序列化要最基本要有一个
template< class archive >
void serialize( archive &ar , unsigned int filever ) { ar & BOOST_SERIALIZATION_NVP(mvalue); }这样的模版函数,因为最后编译完所有的序列化实现都会经过这个函数。我试了一个对象的序列化发现boost真是太好了
test* a = NULL;
oa >> BOOST_SERIALIZATION_NVP(nvalue); oa >> BOOST_SERIALIZATION_NVP(ints); oa >> BOOST_SERIALIZATION_NVP(a);boost用判断对象指针为空调用load_pointer为你生成一个对象有了这个功能真是太方便了,我跟踪了一下实现代码#@%^&*,展开的名字太长了有两个地方是实现这个功能的主要部份一个是register_type,还有就是那个ar.load_pointer,没有时间了,要休息了明天还有封版本,只能做到这里了,有兴趣的朋友可以试一下boost的代码还是很好看懂的,把这部份都搞清了就成范型高手了......明天继续。