首页 > 代码库 > boost::serialization(1)基础

boost::serialization(1)基础

将字符串内容保存/加载到文本归档文件中

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

void save()
{
	std::ofstream ofs("t1.txt");
	boost::archive::text_oarchive oa(ofs);
	std::string s = "hello world!\n";
	oa << s;
}

void load()
{
	std::ifstream ifs("t1.txt");
	boost::archive::text_iarchive ia(ifs);
	std::string s;
	ia >> s; 
	std::cout << s << std::endl;
}

void fun()
{
	save();
	load();
}
使用 & 运算符执行 “转储 - 恢复” 操作
#include <fstream>
#include <iostream>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>

void save()
{
	std::ofstream ofs("t2.txt");
	boost::archive::text_oarchive oa(ofs);
	std::string s = "hello world!\n";
	oa & s;
}

void load()
{
	std::ifstream ifs("t2.txt");
	boost::archive::text_iarchive ia(ifs);
	std::string s;
	ia & s; 
	std::cout << s << std::endl;
}

void fun()
{
	save();
	load();
}
3从 XML 归档文件执行 “转储 - 恢复” 操作

#include <fstream>
#include <iostream>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
void save()
{
	std::ofstream ofs("t3.xml");
	boost::archive::xml_oarchive oa(ofs);
	std::string s = "hello world!\n";
	oa & BOOST_SERIALIZATION_NVP(s);
}

void load()
{
	std::ifstream ifs("t3.xml");
	boost::archive::xml_iarchive ia(ifs);
	std::string s;
	ia & BOOST_SERIALIZATION_NVP(s); 
	std::cout << s << std::endl;
}

void fun()
{
	save();
	load();
}
如果您想使用 XML 归档文件,而不是文本归档文件,则必须包含来自 Boost 源的头文件 xml_iarchive.hpp 和 xml_oarchive.hpp。这些头文件声明或定义了 XML 归档文件语义。但是,该 “转储 - 恢复” 操作与应用于文本归档文件的 “转储 - 恢复” 操作仍有些微不同之处:需要将数据打包到一个名为 BOOST_SERIALIZATION_NVP 的宏中。呵呵!头文件不同,多了一个宏来包装数据!

4序列化stl容器

要序列化 STL 列表和向量,则必须了解每个 STL 类型,应用程序代码必须包含来自 Serialization 源的具有类似名称的头文件,比如要序列化

std:::list,需要包含#include <boost/serialization/list.hpp>。

#include <fstream>
#include <iostream>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/assign.hpp>
#include <boost/serialization/list.hpp>
#include <boost/serialization/vector.hpp>

void save()
{
	std::ofstream ofs("t5.xml");
	boost::archive::xml_oarchive oa(ofs);
	std::list<float> list = boost::assign::list_of(1)(2)(3)(40)(5);
	std::vector<float> vect(list.begin(), list.end());
	oa << BOOST_SERIALIZATION_NVP(list);
	oa << BOOST_SERIALIZATION_NVP(vect);
}

void load()
{
	std::ifstream ifs("t5.xml");
	boost::archive::xml_iarchive ia(ifs);
	std::list<float> list;
	std::vector<float> vect;
	ia >> BOOST_SERIALIZATION_NVP(list);
	ia >> BOOST_SERIALIZATION_NVP(vect);

	std::ostream_iterator<float> oi(std::cout, " ");
	std::copy(list.begin(), list.end(), oi); std::cout << std::endl;
	std::copy(vect.begin(), vect.end(), oi); std::cout << std::endl;
}

void fun()
{
	save();
	load();
}
5序列化自定义类型的数据

序列化自定义的类

#include <fstream>
#include <iostream>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/assign.hpp>
#include <boost/serialization/list.hpp>
#include <boost/serialization/vector.hpp>

class student_info
{
public:
	student_info() {}
	student_info(const std::string& sn, const std::string& snm, const std::string& sg)
		: name_(sn), number_(snm), grade_(sg)
	{
	}

	void print_info() const
	{
		std::cout << name_ << " " << number_ << " " << grade_ << std::endl;
	}

private:
	friend class boost::serialization::access;
	template<typename Archive>
	void serialize(Archive& ar, const unsigned int version)
	{
		//ar & name_;
		//ar & number_;
		//ar & grade_;

		ar & BOOST_SERIALIZATION_NVP(name_);
		ar & BOOST_SERIALIZATION_NVP(number_);
		ar & BOOST_SERIALIZATION_NVP(grade_);
	}

private:
	std::string name_;
	std::string number_;
	std::string grade_;
};

void save()
{
	std::ofstream ofs("t6.xml");
	boost::archive::xml_oarchive oa(ofs);
	student_info sdinfo("wyp", "0099", "1");
	oa << BOOST_SERIALIZATION_NVP(sdinfo);
}

void load()
{
	std::ifstream ifs("t6.xml");
	boost::archive::xml_iarchive ia(ifs);
	student_info stinfo;
	ia >> BOOST_SERIALIZATION_NVP(stinfo);
	stinfo.print_info();
}

void fun()
{
	save();
	load();
}
这个是intrusive式的,non-intrusive要把类型改为共有的

#include <fstream>
#include <iostream>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/assign.hpp>
#include <boost/serialization/list.hpp>
#include <boost/serialization/vector.hpp>

class student_info
{
public:
	student_info() {}
	student_info(const std::string& sn, const std::string& snm, const std::string& sg)
		: name_(sn), number_(snm), grade_(sg)
	{
	}

	void print_info() const
	{
		std::cout << name_ << " " << number_ << " " << grade_ << std::endl;
	}

	std::string name_;
	std::string number_;
	std::string grade_;
};

template<typename Archive>
void serialize(Archive& ar, student_info& sdinfo, const unsigned int version)
{
	//ar & name_;
	//ar & number_;
	//ar & grade_;

	ar & BOOST_SERIALIZATION_NVP(sdinfo.name_);
	ar & BOOST_SERIALIZATION_NVP(sdinfo.number_);
	ar & BOOST_SERIALIZATION_NVP(sdinfo.grade_);
}

void save()
{
	std::ofstream ofs("t6.xml");
	boost::archive::xml_oarchive oa(ofs);
	student_info sdinfo("wyp", "0099", "1");
	oa << BOOST_SERIALIZATION_NVP(sdinfo);
}

void load()
{
	std::ifstream ifs("t6.xml");
	boost::archive::xml_iarchive ia(ifs);
	student_info stinfo;
	ia >> BOOST_SERIALIZATION_NVP(stinfo);
	stinfo.print_info();
}

void fun()
{
	save();
	load();
}
待续。。。。。。