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的代碼還是很好看懂的,把這部份都搞清了就成範型高手了......明天繼續。
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章