在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