ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

boost 序列化--C++--serialization--支持常用STL--内存数据文件持久化--继承序列化

2020-11-25 11:33:15  阅读:345  来源: 互联网

标签:include NVP -- boost STL ar SERIALIZATION 序列化 BOOST


在C++编程中,一个常用的操作是保存数据到文件,用于下次加载时使用,为达到这一目的,boost库提供了序列化的库供使用,源代码如下所示:

#include "boost/serialization/serialization.hpp"
#include "boost/archive/binary_oarchive.hpp"
#include "boost/archive/binary_iarchive.hpp"
#include "boost/foreach.hpp"
#include "boost/any.hpp"
#include "boost/serialization/vector.hpp"
#include "boost/serialization/list.hpp"
#include "boost/serialization/deque.hpp"
#include "boost/serialization/string.hpp"
#include "boost/serialization/split_free.hpp"
#include "boost/serialization/base_object.hpp"
#include "boost/serialization/access.hpp"
#include "boost/serialization/version.hpp"
#include "boost/serialization/export.hpp"
#include "boost/archive/text_iarchive.hpp"
#include "boost/archive/text_oarchive.hpp"

class class1
{
	//序列数据
	friend class boost::serialization::access;
	template<class Archive>//序列化函数
	void serialize(Archive& ar,const unsigned int version)
	{
		ar&m_filename;
		if(Archive::is_loading::value)//加载数据
		{
			ar&m_ANormalLabelSizeMap;
			std::map<std::string,ANormalLabelSize*>::iterator it=m_ANormalLabelSizeMap.begin();
			for (;it!=m_ANormalLabelSizeMap.end();it++)
			{
				//创建对象加载数据
				ANormalLabelSize* pANormalLabelSize=new ANormalLabelSize;
				ar&(pANormalLabelSize);
				it->second = pANormalLabelSize;
			}
		}
		if(Archive::is_saving::value)
		{
			ar&m_ANormalLabelSizeMap;
			std::map<std::string,ANormalLabelSize*>::iterator it=m_ANormalLabelSizeMap.begin();
			for (;it!=m_ANormalLabelSizeMap.end();it++)
			{
				ar&(*(it->second));//保存对象数据
			}
		}
	}
}

父类和子类序列化,源代码如下所示:
DrawObject.h文件

// ODShape.h: interface for the CShape class.
//
//

#if !defined(AFX_ODShape_H__125A1B30_9A77_4C4D_9B48_894687D03BEC__INCLUDED_)
#define AFX_ODShape_H__125A1B30_9A77_4C4D_9B48_894687D03BEC__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "gdiplus.h"
#include <list>
#include "GeometryCompute.h"
#include <string>
#include "GDIPlusSerialization.h"

//对象类型
enum DrawObjectType
{
	ObjectType_UnKnow,
	ObjectType_Pointer,
	ObjectType_Rectangle,
	ObjectType_Line,
	ObjectType_PolyLine,
	ObjectType_Polygon,
	ObjectType_Rotation,
	ObjectType_Text,
	ObjectType_Image,
	ObjectType_PolygonCrop,
	NumberOfDrawObjectTypes
} ;	

class DrawObject
{
public:
	DrawObject ();
	virtual ~DrawObject ();

public:
	//对象名称
	std::string m_DrawObjectName;//对象ID

	//画笔
	Gdiplus::Color m_PenColor;
	float m_PenWidth;
	int m_PenType;

	//画刷
	Gdiplus::Color m_FillColor;
	bool m_Filled;
	int m_BrushType;

	//状态属性
	Gdiplus::PointF m_Center;
	std::string m_TipText;
	unsigned MID;
	bool m_Dirty;
	int m_Id;
	int m_ZOrder;
	float m_Rotation;	
	bool m_bIsDrawCenterPoint;
	int m_ConnectionCount;
	bool m_Selected;
	bool m_bIsImage;
	int m_HandleCount;

	//微调
	float m_Step;
	bool m_IsCreated;

	//对象类型名
	DrawObjectType m_ObjectType;

	//绘制模式 0代表正常绘图 1代表只绘制关键点
	int m_DrawMode;

public:
	//序列数据
	friend class boost::serialization::access;
	template<class Archive>//序列化函数
	void serialize(Archive& ar,const unsigned int version)
	{
		//对象名称
		ar&BOOST_SERIALIZATION_NVP(m_DrawObjectName);

		//画笔
		ar&BOOST_SERIALIZATION_NVP(m_PenColor);
		ar&BOOST_SERIALIZATION_NVP(m_PenWidth);
		ar&BOOST_SERIALIZATION_NVP(m_PenType);

		//画刷
		ar&BOOST_SERIALIZATION_NVP(m_FillColor);
		ar&BOOST_SERIALIZATION_NVP(m_Filled);
		ar&BOOST_SERIALIZATION_NVP(m_BrushType);

		//状态属性
		ar&BOOST_SERIALIZATION_NVP(m_Center);
		//ar&m_Center;
		ar&BOOST_SERIALIZATION_NVP(m_TipText);
		ar&BOOST_SERIALIZATION_NVP(MID);
		ar&BOOST_SERIALIZATION_NVP(m_Dirty);
		ar&BOOST_SERIALIZATION_NVP(m_Id);
		ar&BOOST_SERIALIZATION_NVP(m_ZOrder);
		ar&BOOST_SERIALIZATION_NVP(m_Rotation);
		ar&BOOST_SERIALIZATION_NVP(m_bIsDrawCenterPoint);
		ar&BOOST_SERIALIZATION_NVP(m_ConnectionCount);
		ar&BOOST_SERIALIZATION_NVP(m_Selected);
		ar&BOOST_SERIALIZATION_NVP(m_bIsImage);
		ar&BOOST_SERIALIZATION_NVP(m_HandleCount);

		//微调
		ar&BOOST_SERIALIZATION_NVP(m_Step);
		ar&BOOST_SERIALIZATION_NVP(m_IsCreated);
	}
};

BOOST_SERIALIZATION_ASSUME_ABSTRACT(DrawObject);

typedef std::list<DrawObject*> ShapeList;
typedef ShapeList::iterator ShpIt;
typedef ShapeList::const_iterator ConShpIt;
#endif // !defined(AFX_ODShape_H__125A1B30_9A77_4C4D_9B48_894687D03BEC__INCLUDED_)

DrawObject.cpp文件

// ODShape.cpp: implementation of the CShape class.
//
//
#include "stdafx.h"
#include "DrawObject.h"
#include <math.h>
#include "CADControl.h"
#include "GeometryCompute.h"
#include <conio.h>
#include <objbase.h>
#include <stdio.h>

#include <string>
#include <iostream>
#include <sstream>
#include <vector>
#include "MyLogger.h"

using namespace Gdiplus;

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
					
#define ABGR(a,r,g,b)  ((COLORREF)(((BYTE)(r)|((WORD)((BYTE)(g))<<8))|(((DWORD)(BYTE)(b))<<16))|(((DWORD)((BYTE)(a))<<24)))
//
// Construction/Destruction
//
BOOST_CLASS_VERSION(DrawObject,0)

int DrawObjectCount=0;
DrawObject ::DrawObject()
{	
}

子类.h文件

// ODLine.h: interface for the CDrawLine class.
//
//

#if !defined(AFX_ODLINE_H__DE7440CD_6BE1_4086_AFCB_899CC264EB7B__INCLUDED_)
#define AFX_ODLINE_H__DE7440CD_6BE1_4086_AFCB_899CC264EB7B__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "DrawObject.h"
class DrawLine : public DrawObject
{
public:
	Gdiplus::PointF m_StartPoint;
	Gdiplus::PointF m_EndPoint;

	std::string entryStart;
	std::string entryEnd;

	/// <summary>
	///  Graphic objects for hit test
	/// </summary>
	Gdiplus::GraphicsPath* m_AreaPath;
	Gdiplus::Pen* m_AreaPen;
	Gdiplus::Region* m_AreaRegion;

private:
	//序列数据
	friend class boost::serialization::access;
	template<class Archive>//序列化函数 常量函数无法改变对象值
	void serialize(Archive& ar,const unsigned int version)
	{
		//ar&BOOST_SERIALIZATION_BASE_OBJECT_NVP(*this);
		ar&BOOST_SERIALIZATION_NVP(boost::serialization::base_object<DrawObject>(*this));
		ar&BOOST_SERIALIZATION_NVP(m_StartPoint);
		ar&BOOST_SERIALIZATION_NVP(m_EndPoint);
	}
}

子类.cpp文件

// ODLine.cpp: implementation of the CDrawLine class.
//
//

#include "stdafx.h"
#include "DrawLine.h"
#include "gdiplus.h"
#include "GeometryComputeGdiPlus.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//
// Construction/Destruction
//
using namespace Gdiplus;
using namespace std;

BOOST_CLASS_VERSION(DrawLine,0)
BOOST_CLASS_EXPORT_GUID(DrawLine,"DrawLine")

DrawLine::DrawLine()
{
	entryStart = "Start";
	entryEnd = "End";

	m_StartPoint.X = 0;
	m_StartPoint.Y = 0;
	m_EndPoint.X = 1;
	m_EndPoint.Y = 1;

	Initialize();
}

  欢迎光临知了软件开发网络平台,本公司定制开发各类软件,主要方向为桌面专业软件开发和插件定制开发,桌面软件主要包括文字图形识别类软件,信息管理类软件,3D打印类软件,视频类软件以及其它涉及专业的各类图形图像处理软件。插件包含AE插件,AI插件,PS插件,PDF插件,3DMAX插件以及Word,Excel等Office插件开发。详情请咨询,微信QQ:312117271,手机:18928899728,邮箱: anjingzhi_sea@163.com.
公司网址:http://www.zhiliaos.com

标签:include,NVP,--,boost,STL,ar,SERIALIZATION,序列化,BOOST
来源: https://blog.csdn.net/weixin_42247427/article/details/110120598

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有