概述
// SaveData.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
//#include "ToolBlock.h"
//#include "findLineOperator.h"
//
//int main()
//{
// vector<BaseNode*> vec;
//
//
// findLineOperator *base = new findLineOperator();
// base->save();
//
// vec.push_back(base);
//
// return 0;
//}
// testSerialization.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/vector.hpp>
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;
class BaseNode
{
public:
std::string a="this is BaseNode a";
std::string b="this is BaseNode b";
//为使当前的结构能支持序列化,得加入下面的代码段
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & a;
ar & b;
}
};
class BaseOperator :public BaseNode
{
public:
std::string c = "this is BaseOperator c";
std::string d = "this is BaseOperator d";
//为使当前的结构能支持序列化,得加入下面的代码段
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & c;
ar & d;
}
};
class findLineOperator :public BaseOperator {
public:
int id;
std::string strName;
std::string strValue;
findLineOperator() {}
findLineOperator(int id, std::string strName, std::string strValue)
{
this->id = id;
this->strName = strName;
this->strValue = strValue;
}
private:
friend boost::serialization::access; //声明友元,授予访问权限
template<typename Archive>
void serialize(Archive &ar, const unsigned int version) //序列化函数
{
ar & id;
ar & strName;
ar & strValue;
//ar & listMR;
}
};
class ToolBlock
{
public:
std::vector<BaseNode*> listMR;
};
int main(int argc, _TCHAR* argv[])
{
std::stringstream ss;
//序列化
//findLineOperator p1(11, "anderson", "neo"); //被序列化的对象
ToolBlock *tool = new ToolBlock();
findLineOperator *mr = new findLineOperator(99,"eric","lili");
findLineOperator *mr2=new findLineOperator();
mr2->a = "this is b" ;
mr2->b = "第二条记录";
//压入对象
tool->listMR.push_back(mr);
tool->listMR.push_back(mr2);
//创建对象 //为被序列化对象添加两条记录
//对象初始化
//mr->a = "apple";
//mr->b = "第一条记录";
//对象序列化
string fileName = "save.cvteam";
std::ofstream ofs;
ofs.open(fileName.c_str(), ofstream::out);
if (ofs)
{
boost::archive::text_oarchive oa(ofs);
oa << tool->listMR;
}
ofs.close();
//boost::archive::text_oarchive(ss) << p1; //序列化
//序列化为xml形式要求中文为utf-8编码,否则打开文件是乱码
//std::stringstream ss;
//std::ofstream ofs("d:\a.xml");
//boost::archive::xml_oarchive oa(ofs);
//oa << BOOST_SERIALIZATION_NVP(p1);
//反序列化
//MyData p2; //被反序列化的对象
//boost::archive::text_iarchive(ss) >> p2; //反序列化
//printTest(p2);
//std::cout << "序列化后的=>" << ss.str() << std::endl;;
return 0;
}
最后
以上就是顺心煎饼为你收集整理的测试序列化的全部内容,希望文章能够帮你解决测试序列化所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复