0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

什么時候需要Boost序列化

科技綠洲 ? 來源:Linux開發(fā)架構(gòu)之路 ? 作者:Linux開發(fā)架構(gòu)之路 ? 2023-11-10 10:14 ? 次閱讀

程序開發(fā)中,序列化是經(jīng)常需要用到的。像一些相對高級語言,比如JAVA, C#都已經(jīng)很好的支持了序列化,那么C++呢?當(dāng)然一個比較好的選擇就是用Boost,這個號稱C++準(zhǔn)標(biāo)準(zhǔn)庫的東西。

什么時候需要序列化呢?舉個例子,我們定義了一個class,比如:

class CCar
{
public:
	void SetName(std::string& strName){m_strName = strName;}
	std::string GetName() const{return m_strName;}
private:
	std::string m_strName;
};

然后我們想把這個類的一個對象保存到文件中或者通過網(wǎng)絡(luò)發(fā)出去,怎么辦呢?答案就是:把這個對象序列化,然后我們可以得到一個二進(jìn)制字節(jié)流,或者XML格式表示等等。

這樣我們就可以保存這個對象到文件中或者通過網(wǎng)絡(luò)發(fā)出去了。把序列化的數(shù)據(jù)進(jìn)行反序列化,就可以得到一個CCar對象了。

Boost已經(jīng)很好的支持了序列化這個東西,很好很強(qiáng)大。

Boost網(wǎng)站上有介紹: Serialization

對于序列化,Boost是這么定義的:

Here, we use the term "serialization" to mean the reversible deconstruction of an arbitrary set of C++ data structures to a sequence of bytes. Such a system can be used to reconstitute an equivalent structure in another program context. Depending on the context, this might used implement object persistence, remote parameter passing or other facility. In this system we use the term"archive" to refer to a specific rendering of this stream of bytes. This could be a file of binary data, text data, XML, or some other created by the user of this library.

這段英文很簡單,我相信大多數(shù)程序員都能看的懂。

基本上Boost序列化可以分為兩種模式:侵入式(intrusive)和非侵入式(non-intrusive)

侵入式(intrusive)

先來看看侵入式。我們先來定義一個類,這個類支持序列化:

class CMyData
{
private:
	friend class boost::serialization::access;

	template< class Archive >
	void serialize(Archive& ar, const unsigned int version)
	{
		ar & _tag;
		ar & _text;
	}

	
public:
	CMyData():_tag(0), _text(""){}

	CMyData(int tag, std::string text):_tag(tag), _text(text){}

	int GetTag() const {return _tag;}
	std::string GetText() const {return _text;}

private:
	int _tag;
	std::string _text;
};

其中,我們可以看到這些代碼:

friend class boost::serialization::access;

	template< class Archive >
	void serialize(Archive& ar, const unsigned int version)
	{
		ar & _tag;
		ar & _text;
	}

這些代碼就是用來實現(xiàn)序列化的,這些代碼存在于類CMyData中,也就是為什么稱這種模式是“侵入式”的原因了。

看看怎么把這個對象序列化。這里,我把這個對象以二進(jìn)制的方式保存到了一個ostringstream中了,當(dāng)然也可以保存為其他形式,比如XML。也可以保存到文件中。代碼都是類似的。

void TestArchive1()
{
	CMyData d1(2012, "China, good luck");
	std::ostringstream os;
	boost::archive::binary_oarchive oa(os);
	oa < < d1;//序列化到一個ostringstream里面

	std::string content = os.str();//content保存了序列化后的數(shù)據(jù)。

	CMyData d2;
	std::istringstream is(content);
	boost::archive::binary_iarchive ia(is);
	ia > > d2;//從一個保存序列化數(shù)據(jù)的string里面反序列化,從而得到原來的對象。

	std::cout < < "CMyData tag: " < < d2.GetTag() < < ", text: " < < d2.GetText() < < "n";
}

先生成一個CMyData的對象,然后序列化保存到一個ostringstream中,接著再把這個序列化的數(shù)據(jù)反序列化,得到原來的對象,打印出來,我們會發(fā)現(xiàn)反序列化的對象的數(shù)據(jù)成員跟序列化前的對象一模一樣。哈哈,成功了,簡單吧。至于Boost怎么實現(xiàn)這個過程的,看Boost源代碼吧,Boost的網(wǎng)站上也有一些介紹。Boost確實設(shè)計的很巧妙,不得不佩服那幫家伙。

那么可以序列化CMyData的子類嗎,答案是肯定的。其實很簡單就是在子類的序列化函數(shù)里面先序列化基類的??纯创a就明白了:

class CMyData_Child: public CMyData
{
private:
	friend class boost::serialization::access;

	template< class Archive >
	void serialize(Archive& ar, const unsigned int version)
	{
		// serialize base class information
		ar & boost::serialization::base_object< CMyData >(*this);
		ar & _number;
	}


public:
	CMyData_Child():_number(0.0){}

	CMyData_Child(int tag, std::string text, float number):CMyData(tag, text), _number(number){}

	float GetNumber() const{return _number;}

private:
	float _number;
};

void TestArchive3()
{
	CMyData_Child d1(2012, "China, good luck", 1.2);
	std::ostringstream os;
	boost::archive::binary_oarchive oa(os);
	oa < < d1;//序列化到一個ostringstream里面

	std::string content = os.str();//content保存了序列化后的數(shù)據(jù)。

	CMyData_Child d2;
	std::istringstream is(content);
	boost::archive::binary_iarchive ia(is);
	ia > > d2;//從一個保存序列化數(shù)據(jù)的string里面反序列化,從而得到原來的對象。

	std::cout < < "CMyData_Child tag: " < < d2.GetTag() < < ", text: " < < d2.GetText() < < ", number: "<

非侵入式(non-intrusive)

侵入式的缺點就是需要在class里面加一些代碼,那么有時候可能這個class已經(jīng)存在了,或者我們并不想往里面加入這么些代碼,那么怎么辦呢?ok,輪到非侵入式出場了。

比方說我們有這么個類:

class CMyData2
{
public:
	CMyData2():_tag(0), _text(""){}

	CMyData2(int tag, std::string text):_tag(tag), _text(text){}

	int _tag;
	std::string _text;
};

那么我們可以這么序列化:

namespace boost {
	namespace serialization {

		template< class Archive >
		void serialize(Archive & ar, CMyData2 & d, const unsigned int version)
		{
			ar & d._tag;
			ar & d._text;
		}

	} // namespace serialization
} // namespace boost

然后調(diào)用還是跟侵入式一模一樣,看:

void TestArchive2()
{
	CMyData2 d1(2012, "China, good luck");
	std::ostringstream os;
	boost::archive::binary_oarchive oa(os);
	oa < < d1;//序列化到一個ostringstream里面

	std::string content = os.str();//content保存了序列化后的數(shù)據(jù)。

	CMyData2 d2;
	std::istringstream is(content);
	boost::archive::binary_iarchive ia(is);
	ia > > d2;//從一個保存序列化數(shù)據(jù)的string里面反序列化,從而得到原來的對象。

	std::cout < < "CMyData2 tag: " < < d2._tag < < ", text: " < < d2._text < < "n";
}

成功。跟侵入式相比,非侵入式省去了在具體類里面加入序列化代碼。但是我們看看非侵入式模式里面的類的定義,我們會發(fā)現(xiàn)我們把數(shù)據(jù)成員搞成public的了。這是為什么呢?看看這個就明白了:

template< class Archive >
		void serialize(Archive & ar, CMyData2 & d, const unsigned int version)
		{
			ar & d._tag;
			ar & d._text;
		}

原來序列化函數(shù)需要訪問數(shù)據(jù)成員。這就是非侵入式的一個缺點了:需要把數(shù)據(jù)成員暴露出來。通過直接訪問數(shù)據(jù)成員也好,通過函數(shù)訪問也好,總之需要這個類把數(shù)據(jù)成員暴露出來,這樣序列化函數(shù)才能訪問。世界上沒有十全十美的東西,有時我們得到一個東西,往往會失去另外一個東西,不是嗎?

侵入式和非侵入式各有各的用處,看具體情況來決定用哪個了。

非侵入式可以支持子類序列化嗎?可以。跟侵入式一樣,其實也就是先序列化一下基類,然后再序列化子類的數(shù)據(jù)成員??创a:

class CMyData2_Child: public CMyData2
{
public:
	CMyData2_Child():_number(0.0){}

	CMyData2_Child(int tag, std::string text, float number):CMyData2(tag, text), _number(number){}

	float _number;
};

namespace boost {
	namespace serialization {

		template< class Archive >
		void serialize(Archive & ar, CMyData2_Child & d, const unsigned int version)
		{
			// serialize base class information
			ar & boost::serialization::base_object< CMyData2 >(d);
			ar & d._number;
		}

	} // namespace serialization
} // namespace boost

void TestArchive4()
{
	CMyData2_Child d1(2012, "test non-intrusive child class", 5.6);
	std::ostringstream os;
	boost::archive::binary_oarchive oa(os);
	oa < < d1;//序列化到一個ostringstream里面

	std::string content = os.str();//content保存了序列化后的數(shù)據(jù)。

	CMyData2_Child d2;
	std::istringstream is(content);
	boost::archive::binary_iarchive ia(is);
	ia > > d2;//從一個保存序列化數(shù)據(jù)的string里面反序列化,從而得到原來的對象。

	std::cout < < "CMyData2_Child tag: " < < d2._tag < < ", text: " < < d2._text < < ", number: "<

好了,以上就是序列化的簡單用法。接下里我們來重點關(guān)注一下數(shù)據(jù)成員的序列化,假如我們的類里面有指針,那么還能序列化嗎?比如下面的代碼,會發(fā)生什么事?

序列化指針數(shù)據(jù)成員

class CMyData
{
private:
	friend class boost::serialization::access;

	template< class Archive >
	void serialize(Archive& ar, const unsigned int version)
	{
		ar & _tag;
		ar & _text;
	}


public:
	CMyData():_tag(0), _text(""){}

	CMyData(int tag, std::string text):_tag(tag), _text(text){}

	int GetTag() const {return _tag;}
	std::string GetText() const {return _text;}

private:
	int _tag;
	std::string _text;
};
class CMyData_Child: public CMyData
{
private:
	friend class boost::serialization::access;

	template




	void serialize(Archive& ar, const unsigned int version)
	{
		// serialize base class information
		ar & boost::serialization::base_object



    (*this);
		ar & _number;
	}


public:
	CMyData_Child():_number(0.0){}

	CMyData_Child(int tag, std::string text, float number):CMyData(tag, text), _number(number){}

	float GetNumber() const{return _number;}

private:
	float _number;
};
class CMyData_Container
{
private:
	friend class boost::serialization::access;

	template< class Archive >
	void serialize(Archive& ar, const unsigned int version)
	{
		for(int i = 0; i < 3; i++)
		{
			ar & pointers[i];
		}
	}
public:
	CMyData* pointers[3];
};

void TestPointerArchive()
{
	std::string content;
	{
		CMyData d1(1, "a");
		CMyData_Child d2(2, "b", 1.5);

		CMyData_Container containter;
		containter.pointers[0] = &d1;
		containter.pointers[1] = &d2;
		containter.pointers[2] = &d1;

		std::ostringstream os;
		boost::archive::binary_oarchive oa(os);
		oa < < containter;

		content = os.str();
	}

	//反序列化
	{
		CMyData_Container container;
		std::istringstream is(content);
		boost::archive::binary_iarchive ia(is);
		ia > > container;

		for (int i = 0; i < 3; i++)
		{
			CMyData* d = container.pointers[i];
			std::cout < < "pointer" < < i + 1 < ": " < < d- >GetText() < < "n";

			if (i == 1)
			{
				CMyData_Child* child = reinterpret_cast< CMyData_Child* >(d);
				std::cout < < "pointer" < < i + 1 < ", number: " < < child- >GetNumber() < < "n";
			}
		}
	}
}

注意,我們在CMyData_Container對象里面放進(jìn)去了3個指針,其中第二個指針是CMyData的子類。

然后進(jìn)行序列化,再反序列化,我們會發(fā)現(xiàn),第一個,第三個指針輸出了正確的信息,然而第二個指針有點問題,本身我們存進(jìn)去的時候是個CMyData_Child 對象,通過測試我們可以發(fā)現(xiàn),CMyData_Child的基類部分,我們可以正確的輸出,但是CMyData_Child的成員_number,卻得不到正確信息。這是個問題。

也就是說,序列化指針是可以的,但是需要注意多態(tài)的問題。假如我們不需要考慮多態(tài),那么以上的代碼就可以正常工作了。但是如果要考慮多態(tài)的問題,那么就得特殊處理了。下面再來介紹序列化多態(tài)指針。

序列化多態(tài)指針數(shù)據(jù)成員

上一個章節(jié)里面演示了如果序列化指針成員,但是有個問題,就是當(dāng)基類指針指向一個派生類對象的時候,然后序列化這個指針,那么派生類的信息就被丟掉了。這個很不好。那么怎么來解決這個問題呢?很幸運,Boost的開發(fā)人員已經(jīng)考慮到了這個問題。再一次感受到Boost的強(qiáng)大。

有兩種方法可以解決這個問題:

  1. registration
  2. export

具體可以參考: http://www.boost.org/doc/libs/1_51_0/libs/serialization/doc/serialization.html#derivedpointers

這里我們介紹第二種方式,這種方式比較簡單,也用的比較好。就是通過一個宏把派生類給命名一下。

這個關(guān)鍵的宏是:BOOST_CLASS_EXPORT_GUID

相關(guān)解釋:

The macro BOOST_CLASS_EXPORT_GUID associates a string literal with a class. In the above example we've used a string rendering of the class name. If a object of such an "exported" class is serialized through a pointer and is otherwise unregistered, the "export" string is included in the archive. When the archive is later read, the string literal is used to find the class which should be created by the serialization library. This permits each class to be in a separate header file along with its string identifier. There is no need to maintain a separate "pre-registration" of derived classes that might be serialized. This method of registration is referred to as "key export".

如何使用這個神奇的宏BOOST_CLASS_EXPORT_GUID來實現(xiàn)序列化指向派生類的指針呢?先給出代碼:

class CMyData
{
private:
	friend class boost::serialization::access;

	template< class Archive >
	void serialize(Archive& ar, const unsigned int version)
	{
		ar & _tag;
		ar & _text;
	}

	
public:
	CMyData():_tag(0), _text(""){}

	CMyData(int tag, std::string text):_tag(tag), _text(text){}
	virtual ~CMyData(){}

	int GetTag() const {return _tag;}
	std::string GetText() const {return _text;}

private:
	int _tag;
	std::string _text;
};

class CMyData_Child: public CMyData
{
private:
	friend class boost::serialization::access;

	template< class Archive >
	void serialize(Archive& ar, const unsigned int version)
	{
		// serialize base class information
		ar & boost::serialization::base_object< CMyData >(*this);
		ar & _number;
	}


public:
	CMyData_Child():_number(0.0){}

	CMyData_Child(int tag, std::string text, float number):CMyData(tag, text), _number(number){}

	float GetNumber() const{return _number;}

private:
	float _number;
};

BOOST_CLASS_EXPORT_GUID(CMyData_Child, "CMyData_Child")

class CMyData_Container
{
private:
	friend class boost::serialization::access;

	template< class Archive >
	void serialize(Archive& ar, const unsigned int version)
	{
		for(int i = 0; i < 3; i++)
		{
			ar & pointers[i];
		}
	}
public:
	CMyData* pointers[3];
};


void TestPointerArchive()
{
	std::string content;
	{
		CMyData d1(1, "a");
		CMyData_Child d2(2, "b", 1.5);

		CMyData_Container containter;
		containter.pointers[0] = &d1;
		containter.pointers[1] = &d2;
		containter.pointers[2] = &d1;

		std::ostringstream os;
		boost::archive::binary_oarchive oa(os);
		oa < < containter;

		content = os.str();
	}

	//·′DòáD?ˉ
	{
		CMyData_Container container;
		std::istringstream is(content);
		boost::archive::binary_iarchive ia(is);
		ia > > container;

		for (int i = 0; i < 3; i++)
		{
			CMyData* d = container.pointers[i];
			std::cout < < "pointer" < < i + 1 < ": " < < d- >GetText() < < "n";

			if (i == 1)
			{
				CMyData_Child* child = reinterpret_cast< CMyData_Child* >(d);
				std::cout < < "pointer" < < i + 1 < ", number: " < < child- >GetNumber() < < "n";
			}
		}
	}
}

這次我們可以正確的讀取到第二個指針指向的對象了,可以看到_number的爭取值了。

把代碼和上個版本想比較,我們會發(fā)現(xiàn)2個不同:

  1. CMyData類里面多了個虛的析構(gòu)函數(shù);
  2. 調(diào)用BOOST_CLASS_EXPORT_GUID給派生類CMyData_Child綁定一個字符串。

第二點很容易理解,就是給某個派生類命名一下,這樣就可以當(dāng)作一個key來找到相應(yīng)的類。那么第一點為什么要增加一個虛析構(gòu)函數(shù)呢?是我無意中添加的嗎?當(dāng)然不是,其實這個是序列化指向派生類的指針的其中一個關(guān)鍵。先看Boost網(wǎng)站上面的一段描述:

It turns out that the kind of object serialized depends upon whether the base class (base in this case) is polymophic or not. Ifbase is not polymorphic, that is if it has no virtual functions, then an object of the typebasewill be serialized. Information in any derived classes will be lost. If this is what is desired (it usually isn't) then no other effort is required.

If the base class is polymorphic, an object of the most derived type (derived_oneorderived_twoin this case) will be serialized. The question of which type of object is to be serialized is (almost) automatically handled by the library.

ok,通過這段描述,我們發(fā)現(xiàn)Boost序列化庫會判斷基類是不是多態(tài)的。判斷的依據(jù)就是這個基類里面有沒有虛函數(shù)。我們知道,當(dāng)一個類里面有虛函數(shù)的時候,C++編譯器會自動給這個類增加一個成員:_vfptr,就是虛函數(shù)表指針。我沒有花太多時間去看Boost有關(guān)這部分的源代碼,但是我猜測Boost是根據(jù)這個_vfptr來判斷是需要序列化基類,還是派生類的。我們增加一個虛析構(gòu)函數(shù)的目的也就是讓CMyData產(chǎn)生一個_vfptr。我們可以試一下把上面的代碼里面的析構(gòu)函數(shù)改成非虛的,那么派生類序列化就會失敗,跟上一個章節(jié)得到相同的結(jié)果。至于Boost怎么知道該序列化哪個派生類,相信這個是BOOST_CLASS_EXPORT_GUID的功勞,至于怎么實現(xiàn),還是需要看源代碼,但是我自己沒有仔細(xì)研究過,有興趣的朋友可以學(xué)習(xí)Boost的源代碼。Boost的設(shè)計很巧妙,我們可以學(xué)到不少東西。當(dāng)然這個得有時間細(xì)細(xì)學(xué)習(xí)。好了,序列化指向派生類指針就2個要點:

  1. 讓Boost知道基類是多態(tài)的,其實就是確?;惱锩嬗袀€虛函數(shù);
  2. 通過BOOST_CLASS_EXPORT_GUID給派生類綁定一個字符串,當(dāng)作一個key。

至于第一種序列化指向派生類的基類指針:registration,可以參考http://www.boost.org/doc/libs/1_51_0/libs/serialization/doc/serialization.html#derivedpointers,上面講的非常清楚。我本人很少使用這種方式,這里也就略過不講了。

序列化數(shù)組

一個小細(xì)節(jié),上面講到的序列化指針章節(jié)里面,我們看到代碼:

class CMyData_Container
{
private:
	friend class boost::serialization::access;

	template< class Archive >
	void serialize(Archive& ar, const unsigned int version)
	{
		for(int i = 0; i < 3; i++)
		{
			ar & pointers[i];
		}
	}
public:
	CMyData* pointers[3];
};

其中的序列化函數(shù)里面有個for循環(huán),難道每次序列化一個數(shù)組都需要弄一個for語句嗎,這個是不是可以改進(jìn)呢?答案是肯定的。Boost自己會檢測數(shù)組。也就是說我們可以把代碼改成下面的形式:

class CMyData_Container
{
private:
	friend class boost::serialization::access;

	template< class Archive >
	void serialize(Archive& ar, const unsigned int version)
	{
		ar & pointers;
	}
public:
	CMyData* pointers[3];
};

代碼短了很多,方便吧。

支持STL容器

上面我們使用了一個普通數(shù)組來保存指針,我相信在平常寫程序過程中,大家都會使用STL容器,比如list,map,array等等。至少我自己是經(jīng)常使用的。那么Boost序列化庫可以序列化STL容器嗎?很幸運,Boost序列化庫已經(jīng)支持了STL容器。原話是:

The above example uses an array of members. More likely such an application would use an STL collection for such a purpose. The serialization library contains code for serialization of all STL classes. Hence, the reformulation below will also work as one would expect.

我們一開始就是用std::string作為CMyData的一個成員,我們不需要做任何工作就可以直接序列化std::string,這是因為Boost序列化庫已經(jīng)支持std::string了。從上面的英文描述里面可以看到Boost serialization庫可以支持所有STL類,神奇吧。至少我本人經(jīng)常使用std::list, vector, map, string等,都可以正常工作。下面的代碼使用std::vector代替了普通數(shù)組,可以正常工作。

class CMyData
{
private:
	friend class boost::serialization::access;

	template< class Archive >
	void serialize(Archive& ar, const unsigned int version)
	{
		ar & _tag;
		ar & _text;
	}

	
public:
	CMyData():_tag(0), _text(""){}

	CMyData(int tag, std::string text):_tag(tag), _text(text){}
	virtual ~CMyData(){}

	int GetTag() const {return _tag;}
	std::string GetText() const {return _text;}

private:
	int _tag;
	std::string _text;
};

class CMyData_Child: public CMyData
{
private:
	friend class boost::serialization::access;

	template< class Archive >
	void serialize(Archive& ar, const unsigned int version)
	{
		// serialize base class information
		ar & boost::serialization::base_object< CMyData >(*this);
		ar & _number;
	}


public:
	CMyData_Child():_number(0.0){}

	CMyData_Child(int tag, std::string text, float number):CMyData(tag, text), _number(number){}

	float GetNumber() const{return _number;}

private:
	float _number;
};

BOOST_CLASS_EXPORT_GUID(CMyData_Child, "CMyData_Child")

//ê1ó?STLèY?÷
class CMyData_ContainerSTL
{
private:
	friend class boost::serialization::access;

	template< class Archive >
	void serialize(Archive& ar, const unsigned int version)
	{
		ar & vPointers;
	}
public:
	std::vector< CMyData* > vPointers;
};



void TestPointerArchiveWithSTLCollections()
{
	std::string content;
	{
		CMyData d1(1, "parent obj");
		CMyData_Child d2(2, "child obj", 2.5);

		CMyData_ContainerSTL containter;
		containter.vPointers.push_back(&d1);
		containter.vPointers.push_back(&d2);
		containter.vPointers.push_back(&d1);
		

		std::ostringstream os;
		boost::archive::binary_oarchive oa(os);
		oa < < containter;

		content = os.str();
	}

	//·′DòáD?ˉ
	{
		CMyData_ContainerSTL container;
		std::istringstream is(content);
		boost::archive::binary_iarchive ia(is);
		ia > > container;

		std::cout< "Test STL collections:n";
		BOOST_FOREACH(CMyData* p, container.vPointers)
		{
			std::cout < < "object text: " < < p- >GetText() < < "n";

			CMyData_Child* child = dynamic_cast< CMyData_Child* >(p);
			if (child)
			{
				std::cout < < "child object number: " < < child- >GetNumber() < < "n";
			}
		}
	}
}

一不小心就用到了BOOST_FOREACH,看來這個確實很好用啊,呵呵。省去了寫很長的iterator來遍歷整個vector。

class版本

再來考慮一個問題,比方說現(xiàn)在我們程序升級了,然后把某個類給升級了一下,加了一個成員,那么之前保存的序列化的數(shù)據(jù)還能匹配到新的類嗎?看一下序列化函數(shù),我們會發(fā)現(xiàn)這個序列化函數(shù)有個參數(shù),叫做version

template

void serialize(Archive& ar, const unsigned int version)

通過這個參數(shù),我們就可以解決class版本的問題??催@段描述

In general, the serialization library stores a version number in the archive for each class serialized. By default this version number is 0. When the archive is loaded, the version number under which it was saved is read.

也就是說如果我們不刻意指定version,那么Boost序列化庫就會默認(rèn)設(shè)置為0并且保存到序列化結(jié)果中。

如果我們要標(biāo)記不同的class版本,可以使用宏BOOST_CLASS_VERSION,比如

BOOST_CLASS_VERSION(CMyData, 1)

具體這里就不舉例了。參考Boost說明。

save和load分開

一直到現(xiàn)在我們都是用了一個序列化函數(shù)

template

void serialize(Archive& ar, const unsigned int version)

其實,序列化包括序列化和發(fā)序列化兩部分,或者稱之為save和load,甚至mashalling,unmarshalling。反正就這個意思。

還有一個奇怪的地方,就是通常我們輸入輸出是用<<和>>的,那么在函數(shù)serialize里面我們用了&。其實這個是Boost對&做了一個封裝。假如現(xiàn)在是做序列化,那么&就等同于<<,假如是反序列化,那么&就等同于>>。然后序列化和反序列化統(tǒng)統(tǒng)用一個函數(shù)serialize來實現(xiàn)。這也體現(xiàn)了Boost的巧妙設(shè)計。

那么如果有特殊需求,我們需要把序列化和反序列化分開,應(yīng)該怎么實現(xiàn)呢?

就好比上面的class版本問題,save和load可能就是不一樣的,因為load需要考慮兼容舊的版本。這里就偷懶使用Boost文檔上的例子了。我們可以看到save和load是分開的。

#include 
#include 
#include 
#include 

class bus_route
{
    friend class boost::serialization::access;
    std::list

注意需要使用宏BOOST_SERIALIZATION_SPLIT_MEMBER()來告訴Boost序列化庫使用save和load代替serialize函數(shù)。

到這里,我們幾乎把Boost序列化庫所有的內(nèi)容都介紹完畢了。這個庫是相當(dāng)?shù)膎ice,基本可以cover所有的case。而且就開源庫來講,Boost的說明文檔真的算是很好的了?;旧隙加性敿?xì)的說明,就序列化庫而言,直接看這個頁面就基本ok了,Serialization - Tutorial 相當(dāng)?shù)脑敿?xì)。盡管讀英文比較累,但是可以獲得原汁原味的第一手權(quán)威資料,花這些功夫還是值得的。

我的例子里面使用了二進(jìn)制流來保存序列化后的數(shù)據(jù),其實還有其他的archive格式,比如text,XML等等。甚至我們可以自己來實現(xiàn)序列化格式。Boost已經(jīng)定義了一個統(tǒng)一的接口,我們要實現(xiàn)自己的格式,只需要繼承相應(yīng)的Boost::archive里面的類就可以了。

好了,寫完了,希望對大家有點幫助。如有錯誤,歡迎指出。

附,完整測試代碼,使用這段代碼前需要確保VISUAL STUDIO已經(jīng)設(shè)置了Boost的路徑。

// Serialization.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include "boost/serialization/serialization.hpp"
#include "boost/archive/binary_oarchive.hpp"
#include "boost/archive/binary_iarchive.hpp"
#include 
#include "boost/foreach.hpp"
#include "boost/any.hpp"
#include 



#include < string >
#include < Windows.h >
#include < iostream >
#include < sstream >
#include < vector >


//測試序列化
class CMyData
{
private:
	friend class boost::serialization::access;

	template< class Archive >
	void serialize(Archive& ar, const unsigned int version)
	{
		ar & _tag;
		ar & _text;
	}

	
public:
	CMyData():_tag(0), _text(""){}

	CMyData(int tag, std::string text):_tag(tag), _text(text){}
	virtual ~CMyData(){}

	int GetTag() const {return _tag;}
	std::string GetText() const {return _text;}

private:
	int _tag;
	std::string _text;
};


void TestArchive1()
{
	std::string content;

	{
		CMyData d1(2012, "China, good luck");
		std::ostringstream os;
		boost::archive::binary_oarchive oa(os);
		oa < < d1;//序列化到一個ostringstream里面

		content = os.str();//content保存了序列化后的數(shù)據(jù)。
	}
	

	{
		CMyData d2;
		std::istringstream is(content);
		boost::archive::binary_iarchive ia(is);
		ia > > d2;//從一個保存序列化數(shù)據(jù)的string里面反序列化,從而得到原來的對象。

		std::cout < < "CMyData tag: " < < d2.GetTag() < < ", text: " < < d2.GetText() < < "n";
	}
	
}


class CMyData2
{
public:
	CMyData2():_tag(0), _text(""){}

	CMyData2(int tag, std::string text):_tag(tag), _text(text){}

	int _tag;
	std::string _text;
};

namespace boost {
	namespace serialization {

		template< class Archive >
		void serialize(Archive & ar, CMyData2 & d, const unsigned int version)
		{
			ar & d._tag;
			ar & d._text;
		}

	} // namespace serialization
} // namespace boost

void TestArchive2()
{
	CMyData2 d1(2012, "China, good luck");
	std::ostringstream os;
	boost::archive::binary_oarchive oa(os);
	oa < < d1;//序列化到一個ostringstream里面

	std::string content = os.str();//content保存了序列化后的數(shù)據(jù)。

	CMyData2 d2;
	std::istringstream is(content);
	boost::archive::binary_iarchive ia(is);
	ia > > d2;//從一個保存序列化數(shù)據(jù)的string里面反序列化,從而得到原來的對象。

	std::cout < < "CMyData2 tag: " < < d2._tag < < ", text: " < < d2._text < < "n";
}


//序列化子類,侵入式
class CMyData_Child: public CMyData
{
private:
	friend class boost::serialization::access;

	template< class Archive >
	void serialize(Archive& ar, const unsigned int version)
	{
		// serialize base class information
		ar & boost::serialization::base_object< CMyData >(*this);
		ar & _number;
	}


public:
	CMyData_Child():_number(0.0){}

	CMyData_Child(int tag, std::string text, float number):CMyData(tag, text), _number(number){}

	float GetNumber() const{return _number;}

private:
	float _number;
};

BOOST_CLASS_EXPORT_GUID(CMyData_Child, "CMyData_Child")


void TestArchive3()
{
	CMyData_Child d1(2012, "China, good luck", 1.2);
	std::ostringstream os;
	boost::archive::binary_oarchive oa(os);
	oa < < d1;//序列化到一個ostringstream里面

	std::string content = os.str();//content保存了序列化后的數(shù)據(jù)。

	CMyData_Child d2;
	std::istringstream is(content);
	boost::archive::binary_iarchive ia(is);
	ia > > d2;//從一個保存序列化數(shù)據(jù)的string里面反序列化,從而得到原來的對象。

	std::cout < < "CMyData_Child tag: " < < d2.GetTag() < < ", text: " < < d2.GetText() < < ", number: "<
聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • JAVA
    +關(guān)注

    關(guān)注

    19

    文章

    2946

    瀏覽量

    104372
  • Boost
    +關(guān)注

    關(guān)注

    5

    文章

    364

    瀏覽量

    47890
  • 程序
    +關(guān)注

    關(guān)注

    115

    文章

    3747

    瀏覽量

    80669
  • C++
    C++
    +關(guān)注

    關(guān)注

    21

    文章

    2090

    瀏覽量

    73406
收藏 人收藏

    評論

    相關(guān)推薦

    如何使用Serde進(jìn)行序列化和反序列化

    。它是 Rust 生態(tài)中最受歡迎的序列化庫之一。 基礎(chǔ)用法 安裝 在 Rust 項目中使用 Serde,需要在 Cargo.toml 文件中添加如下依賴: [dependencies] serde
    的頭像 發(fā)表于 09-30 17:09 ?1216次閱讀

    Java序列化的機(jī)制和原理

    本文講解了Java序列化的機(jī)制和原理。從文中你可以了解如何序列化一個對象,什么時候需要序列化以及Java
    發(fā)表于 07-10 07:27

    c語言序列化和反序列化有何區(qū)別

    這里寫自定義目錄標(biāo)題c語言序列化和反序列化tplut.htplut.c測試代碼參考c語言序列化和反序列化網(wǎng)絡(luò)調(diào)用,數(shù)據(jù)傳輸都需要把數(shù)據(jù)
    發(fā)表于 07-14 07:32

    關(guān)于c語言序列化和反序列化的知識點看完你就懂了

    關(guān)于c語言序列和反序列化的知識點你就懂了
    發(fā)表于 10-15 08:47

    SpringMVC JSON框架的自定義序列化與反序列化

    限于createTime和updateTime,更貼近于需求缺點就是需要轉(zhuǎn)換的字段都需要使用注解,工作量有點大當(dāng)然有其他的統(tǒng)一處理方案,這里不贅述。自定義反序列化在jackson框架上實現(xiàn)自定義
    發(fā)表于 10-10 16:02

    理解PHP反序列化漏洞

    理解PHP反序列化漏洞
    發(fā)表于 09-07 11:03 ?7次下載
    理解PHP反<b class='flag-5'>序列化</b>漏洞

    java序列化和反序列化范例和JDK類庫中的序列化API

    存放在一個文件中; 2) 在網(wǎng)絡(luò)上傳送對象的字節(jié)序列。 在很多應(yīng)用中,需要對某些對象進(jìn)行序列化,讓它們離開內(nèi)存空間,入住物理硬盤,以便長期保存。比如最常見的是Web服務(wù)器中的Session對象,當(dāng)有 10萬用戶并發(fā)訪問,就有可能
    發(fā)表于 09-27 10:13 ?6次下載

    java序列化的幾種方式

    一.Java序列化的作用 有的時候我們想要把一個Java對象變成字節(jié)流的形式傳出去,有的時候我們想要從一個字節(jié)流中恢復(fù)一個Java對象。例如,有的時候我們想要 把一個Java對象寫入到
    發(fā)表于 09-27 11:15 ?0次下載

    static屬性為什么不會被序列化

    實現(xiàn)序列化和反序列化為什么要實現(xiàn)Serializable接口?
    的頭像 發(fā)表于 07-15 11:03 ?1701次閱讀

    C#實現(xiàn)對象序列化的三種方式是什么

    很多小伙伴一提到序列化,都會想到二進(jìn)制序列化,但其實序列化并不僅僅只是二進(jìn)制序列化,我們常說的對象序列化有三種方式,分別是二進(jìn)制
    的頭像 發(fā)表于 02-22 16:11 ?1064次閱讀
    C#實現(xiàn)對象<b class='flag-5'>序列化</b>的三種方式是什么

    python序列化對象

    序列化對象:將對象轉(zhuǎn)換為可以存儲或傳輸?shù)男问健? (1) 用于存儲:將對象的字節(jié)序列存儲到文件中,程序退出后不會消失,便于后續(xù)使用。
    的頭像 發(fā)表于 03-10 09:57 ?2295次閱讀

    什么是序列化 為什么要序列化

    什么是序列化? “序列化”(Serialization )的意思是將一個對象轉(zhuǎn)化為字節(jié)流。 這里說的對象可以理解為“面向?qū)ο蟆崩锏哪莻€對象,具體的就是存儲在內(nèi)存中的對象數(shù)據(jù)。 與之相反的過程是“反序列化
    的頭像 發(fā)表于 09-14 17:22 ?2383次閱讀
    什么是<b class='flag-5'>序列化</b> 為什么要<b class='flag-5'>序列化</b>

    ROS中的序列化實現(xiàn)

    不是很多。 為什么ROS不使用現(xiàn)成的序列化工具或者庫呢?可能ROS誕生的時候(2007年),有些序列化庫可能還不存在(protobuf誕生于2008年),更有可能是ROS的創(chuàng)造者認(rèn)為當(dāng)時沒有合適的工具
    的頭像 發(fā)表于 09-14 17:26 ?848次閱讀

    如何用C語言進(jìn)行json的序列化和反序列化

    json是目前最為流行的文本數(shù)據(jù)傳輸格式,特別是在網(wǎng)絡(luò)通信上廣泛應(yīng)用,隨著物聯(lián)網(wǎng)的興起,在嵌入式設(shè)備上,也需要開始使用json進(jìn)行數(shù)據(jù)傳輸,那么,如何快速簡潔地用C語言進(jìn)行json的序列化和反序列化
    的頭像 發(fā)表于 10-07 11:05 ?1322次閱讀

    Java序列化怎么使用

    轉(zhuǎn)換方式就叫做序列化。將文件或者網(wǎng)絡(luò)傳輸中得到的 byte[] 數(shù)組轉(zhuǎn)換為 java 對象就叫做反序列化。 怎么使用 如果一個 Java 對象要能被序列化,必須實現(xiàn)一個特殊
    的頭像 發(fā)表于 10-10 14:19 ?393次閱讀