boost 序列化

#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>


std::stringstream strstream;
void save() 
{ 
	boost::archive::text_oarchive oa(strstream); 
	int i = 1; 
	oa << i; //将i的值序列化到strstream 类中
	TRACE("%s\n",strstream.str().c_str());//22 serialization::archive 13 1
} 

void load() 
{ 
	boost::archive::text_iarchive ia(strstream); 
	int i = 0; 
	ia >> i; //由strstream 类中提取i的值
	TRACE("%s\n",strstream.str().c_str());//22 serialization::archive 13 1
} 

void CMFC08Dlg::OnBnClickedButton2()
{
	try
	{
		save(); 
		load(); 
	}
	catch (std::exception& e)
	{
		TRACE("%s\n",e.what());
	}
}

std::stringstream strstream;
class person
{
public:
	person(){

	}
	person(int age):age_(age){

	}
	int age()const{
		return age_;
	}
protected:
private:
	friend class boost::serialization::access;

	template <typename Archive>
	void serialize(Archive & ar,const unsigned int version)
	{
		//Boost.Serialization 除了 << 和 >> 之外还提供了 & 操作符。如果使用这个操作符,就不再需要在 serialize() 函数中区分是序列化和恢复了
		ar & age_;
	}
	int age_;
};
void save() 
{ 
	boost::archive::text_oarchive oa(strstream); 
	person per(28);
	oa << per; //将per 对象 序列化到strstream 类中
	TRACE("%s\n",strstream.str().c_str());//22 serialization::archive 13 0 0 28
} 

void load() 
{ 
	boost::archive::text_iarchive ia(strstream); 
	person p;
	ia >> p; //由strstream 类中提取原有的per的对象
	TRACE("%s\n",strstream.str().c_str());//22 serialization::archive 13 0 0 28
	TRACE("%d\n",p.age());//输出反序列化后的对象值28
} 

void CMFC08Dlg::OnBnClickedButton2()
{
	try
	{
		save(); 
		load(); 
	}
	catch (std::exception& e)
	{
		TRACE("%s\n",e.what());
	}
}

std::stringstream strstream;
class person
{
public:
	person(){

	}
	person(int age,const char* szName):age_(age),name_(szName),version_(0)
	{

	}
	int age()const{
		return age_;
	}
	const char* name() const {
		return name_.c_str();
	}
protected:
private:
	friend class boost::serialization::access;

	template <typename Archive>
	void serialize(Archive & ar,const unsigned int version)
	{
		//version 的版本可以通过 BOOST_CLASS_VERSION 定义其值,这样可以在序列化中根据这个版本值序列化成不同的对象
		//Boost.Serialization 除了 << 和 >> 之外还提供了 & 操作符。如果使用这个操作符,就不再需要在 serialize() 函数中区分是序列化和恢复了
		ar & age_;
		ar & name_;//序列化这个成员需要包含 #include <boost/serialization/string.hpp> ,同时还支持,boost/serialization/vector.hpp
		if(version > 0)
			ar & version_;
	}
	int age_;
	string name_;
	int version_;
};
void save() 
{ 
	boost::archive::text_oarchive oa(strstream); 
	person per(28,"Hello~");
	oa << per; //将per 对象 序列化到strstream 类中
	TRACE("%s\n",strstream.str().c_str());//22 serialization::archive 13 0 0 28 6 Hello~
} 

void load() 
{ 
	boost::archive::text_iarchive ia(strstream); 
	person p;
	ia >> p; //由strstream 类中提取原有的per的对象
	TRACE("%s\n",strstream.str().c_str());//22 serialization::archive 13 0 0 28 6 Hello~
	TRACE("%d\n",p.age());//输出反序列化后的对象值28
	TRACE("%s\n",p.name());//输出反序列化的对象属性Hello~
} 

void CMFC08Dlg::OnBnClickedButton2()
{
	try
	{
		save(); 
		load(); 
	}
	catch (std::exception& e)
	{
		TRACE("%s\n",e.what());
	}
}

std::stringstream strstream;
std::stringstream strstream_pointer;
std::stringstream strstream_scope;
class person
{
public:
	person(){

	}
	person(int age,const char* szName):age_(age),name_(szName),version_(0)
	{

	}
	int age()const{
		return age_;
	}
	const char* name() const {
		return name_.c_str();
	}
protected:
private:
	friend class boost::serialization::access;

	template <typename Archive>
	void serialize(Archive & ar,const unsigned int version)
	{
		//version 的版本可以通过 BOOST_CLASS_VERSION 定义其值,这样可以在序列化中根据这个版本值序列化成不同的对象
		//Boost.Serialization 除了 << 和 >> 之外还提供了 & 操作符。如果使用这个操作符,就不再需要在 serialize() 函数中区分是序列化和恢复了
		ar & age_;
		ar & name_;//序列化这个成员需要包含 #include <boost/serialization/string.hpp> ,同时还支持,boost/serialization/vector.hpp
		if(version > 0)
			ar & version_;
	}
	int age_;
	string name_;
	int version_;
};
void save() 
{ 
	boost::archive::text_oarchive oa(strstream); 
	//变量序列化
	person per(28,"Hello~");
	oa << per; //将per 对象 序列化到strstream 类中
	TRACE("%s\n",strstream.str().c_str());//22 serialization::archive 13 0 0 28 6 Hello~
	//指针序列化
	person *pPer=new person(28,"指针序列化");
	boost::archive::text_oarchive oa_pointer(strstream_pointer);
	oa_pointer<< pPer;
	delete pPer;
	//指针作用域序列化,包含,boost/serialization/scoped_ptr.hpp,还提供boost/serialization/shared_ptr.hpp
	boost::scoped_ptr<person> pScoped(new person(28,"指针作用域序列化"));
	boost::archive::text_oarchive oa_scope(strstream_scope);
	oa_scope<< pScoped;
} 

void load() 
{ 
	boost::archive::text_iarchive ia(strstream); 
	person p;
	ia >> p; //由strstream 类中提取原有的per的对象
	TRACE("%s\n",strstream.str().c_str());//22 serialization::archive 13 0 0 28 6 Hello~
	TRACE("%d\n",p.age());//输出反序列化后的对象值28
	TRACE("%s\n",p.name());//输出反序列化的对象属性Hello~
	//指针反序列化
	boost::archive::text_iarchive ia_pointer(strstream_pointer);
	person * pPointer;
	ia_pointer >> pPointer;
	//作用域指针反序列化
	boost::archive::text_iarchive ia_scope(strstream_scope);
	boost:scoped_ptr<person> pScoped;
	ia_scope >> pScoped;

} 

//动态对象序列化
BOOST_CLASS_EXPORT(developer)//包含此头文件,#include <boost/serialization/export.hpp>
std::stringstream str_register_type;
void save() 
{ 
	boost::archive::text_oarchive oa(strstream); 
	//使用persion指针 指向一个develper对象,如果按照前面的做法就仅仅序列化 persion对象,不能正确识别,使用BOOST_CLASS_EXPORT即可
	person *p=new developer(18,"Developer","C++");
	oa<< p;
	//由于BOOST_CLASS_EXPORT 是静态注册的,可能有些注册的类最后是不需要序列化的,它却可能注册那些不需要序列化的类型,我们还可以使用register_type,优点是只有需要序列化的类才注册
	boost::archive::text_oarchive oa_register(str_register_type);
	oa_register.register_type<developer>();//先注册再调用
	oa_register<< p;
} 

void load() 
{ 
	boost::archive::text_iarchive ia(strstream); 
	person *p;//反序列化时这里需要用persion指针,否则会报错
	ia>>p;
	//register_type
	boost::archive::text_iarchive ia_register(str_register_type);
	ia_register.register_type<developer>();
	person* pRegister;
	ia_register>> pRegister;
} 

std::stringstream strstream;
std::stringstream strstream_optimization;//优化
void save() 
{ 
	boost::archive::text_oarchive oa(strstream); 
	boost::array<int,3> a={0,1,2};
	oa<< a;
	//输出序列化的数据
	TRACE("%s\n",strstream.str().c_str());//22 serialization::archive 13 0 0 3 0 1 2
	//使用优化的函数序列化
	boost::archive::text_oarchive oa_optimization(strstream_optimization);
	oa_optimization<< boost::serialization::make_array(a.data(),a.size());//使用 boost::serialization::make_array 函数可以达到优化的效果
	//除了 boost::serialization::make_array ,还有 boost::serialization::make_binary_object ()序列化二进制数据,也是传递起始地址以及长度即可
	TRACE("%s\n",strstream_optimization.str().c_str());//22 serialization::archive 13 0 1 2 数据量减少了
	
} 

void load() 
{ 
	boost::archive::text_iarchive ia(strstream); 
	boost::array<int,3> a;
	ia>>a;
	//使用优化的函数反序列化
	boost::archive::text_iarchive ia_optimization(strstream_optimization);
	boost::array<int,3> a2;
	ia_optimization>>boost::serialization::make_array(a2.data(),a2.size());
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值