boost 序列化多态类

#include "stdafx.h"

 

#include <boost/archive/text_oarchive.hpp>

#include <boost/archive/text_iarchive.hpp>

#include <boost/serialization/vector.hpp>

#include <boost/serialization/list.hpp>

#include<boost/serialization/string.hpp>

#include<boost/serialization/version.hpp>

#include <boost\serialization\base_object.hpp>

#include <boost\serialization\export.hpp>

 

#include <string>

#include <iostream>

#include <sstream>

#include <fstream>

#include <vector>

#include <algorithm>

#include <windows.h>

#include <direct.h>

 

using namespace std;

 

class BaseOperator;

class findLineOperator;

 

BOOST_CLASS_EXPORT(BaseOperator)

BOOST_CLASS_EXPORT(findLineOperator)

 

class BaseNode

{

public:

BaseNode()

{

 

}

//virtual void print();

private:

friend class boost::serialization::access;;

template<class Archive>

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

{

ar & a;

ar & b;

}

 

std::string a = "this is BaseNode a";

std::string b = "this is BaseNode b";

};

 

class BaseOperator :public BaseNode

{

public:

BaseOperator()

{

 

}

//virtual void print();

private:

friend class boost::serialization::access;

template<class Archive>

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

{

ar & boost::serialization::base_object<BaseNode>(*this);

ar & c;

ar & d;

}

 

std::string c = "this is BaseOperator c";

std::string d = "this is BaseOperator d";

};

class findLineOperator :public BaseOperator {

 

public:

//virtual void print();

int id = 99;

std::string strName = "eric";

std::string strValue = "lili";

 

findLineOperator() {

//id = 66;

//strName = "me";

//strValue= "you";

}

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 & boost::serialization::base_object<BaseOperator>(*this);

ar & id;

ar & strName;

ar & strValue;

}

};

 

class ToolBlock

{

private:

friend class boost::serialization::access;

template<class Archive>

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

{

ar & listMR;

 

}

public:

void add(BaseNode*node) { this->listMR.push_back(node); }

protected:

std::vector<BaseNode*> listMR;

 

};

 

//void BaseNode::print()

//{

//

//}

//

//void BaseOperator::print()

//{

//

//}

//

//void findLineOperator::print()

//{

//

//}

 

int main(int argc, _TCHAR* argv[])

{

ToolBlock *tool = new ToolBlock();

BaseNode *mr = new BaseOperator();

BaseNode *mr2 = new BaseOperator();

 

tool->add(mr);

tool->add(mr2);

 

string m_name;

//string fileName = "dfgkdfjh\\save.block";

string configdir = "asv";

std::string dir = configdir;

_mkdir(dir.c_str());

dir = configdir + "\\" + m_name;

_mkdir(dir.c_str());

string fileName = configdir + ".block";

 

std::ofstream ofs;

 

ofs.open(fileName.c_str(), ofstream::out);

 

if (ofs)

{

boost::archive::text_oarchive oa(ofs);

 

oa << tool;

 

}

ofs.close();

return 0;

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

究极调参工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值