首页 > C++ > 具体化SerializeElements

具体化SerializeElements

Dec 14th,2011 发表评论

一程序从vc6.0上移植到vs2011,其中需要串行化CList,由于CList参数是个类,要重写SerializeElements方法。原来代码SerializeElements是用非模板函数重写,发现总是断点不到。我们知道现在第三代具体化(ISO/ANSI C++标准)中非模板函数算最大,它将覆盖具体化和常规模板,难道vs2001是非官方草案版模板的编译器?? 只能暂时改成具体化实现,它将覆盖MFC常规模板。

template <> void AFXAPI SerializeElements  (CArchive& ar, CCatalogNode* pElements, INT_PTR nCount) 


CATALOGLIST.H

class CCatalogNode
{
public:
	CCatalogNode();
	~CCatalogNode();
	CCatalogNode(const CCatalogNode& x);
	const CCatalogNode& operator = (const CCatalogNode& x);
	const CCatalogNode& operator += (const CCatalogNode& x);
public:
	void Serialize(CArchive& ar);
public:
	short m_idxCata;
	CString	m_strCatalogName;
	CString m_strDirName;
	long	m_lTotalWordNum;
private:
	//CList			m_lstDocList;
};
//void AFXAPI SerializeElements(CArchive& ar,CCatalogNode* pElements,int nCount);


class CCatalogList  
{
public:
	CCatalogList();
	virtual ~CCatalogList();
	const CCatalogList& operator = (const CCatalogList& x);
	const CCatalogList& operator += (const CCatalogList& x);
	void InitCatalogList(int nMode=0);
	void DumpToFile (CString strFileName, int nSaveMode=0);
	BOOL GetFromFile(CString strFileName);
public:
	CCatalogNode GetAt(POSITION pos) const;
	CCatalogNode& GetAt(POSITION pos);
	CCatalogNode& GetNext(POSITION& rPos);
	POSITION AddCata(CCatalogNode& catanode);
	void Serialize(CArchive& ar);
private:
	CList	m_lstCatalogList;
};
//void AFXAPI SerializeElements(CArchive& ar,CCatalogList* pElements,int nCount);

CATALOGLIST.CPP

CCatalogNode::CCatalogNode(const CCatalogNode& x)
{
	*this=x;
}

//类节点构造
const CCatalogNode& CCatalogNode::operator = (const CCatalogNode& x)
{
	if(this==&x) return *this;
	m_lTotalWordNum = x.m_lTotalWordNum ;
	m_strCatalogName=x.m_strCatalogName;
	m_strDirName=x.m_strDirName;
	m_idxCata=x.m_idxCata;
	/*
	m_lstDocList.RemoveAll();
	POSITION pos = x.m_lstDocList.GetHeadPosition();
	while(pos!=NULL)
	{
		CString docnode=x.m_lstDocList.GetNext(pos);
		m_lstDocList.AddTail(docnode);
	}
	*/
	return *this;
}

const CCatalogNode& CCatalogNode::operator += (const CCatalogNode& x)
{
	if(this==&x) return *this;
	m_lTotalWordNum += x.m_lTotalWordNum ;
	m_strCatalogName=x.m_strCatalogName;
	m_strDirName=x.m_strDirName;
	m_idxCata=x.m_idxCata;
	/*
	POSITION pos = x.m_lstDocList.GetHeadPosition();
	while(pos!=NULL)
	{
		CString docnode=x.m_lstDocList.GetNext(pos);
		m_lstDocList.AddTail(docnode);
	}
	*/
	return *this;
}

CCatalogNode::CCatalogNode()
{
	m_idxCata=-1;

	m_lTotalWordNum=0;

}

CCatalogNode::~CCatalogNode()
{

}

CCatalogList::CCatalogList()
{

}

CCatalogList::~CCatalogList()
{
}

//nSaveMode<=0 保存文档的向量
//nSaveMode>0  不保存文档的向量
void CCatalogList::DumpToFile(CString strFileName, int nSaveMode)  // view the word list content
{
	CFile		fBinOut;
	if(!fBinOut.Open(strFileName,CFile::modeWrite | CFile::modeCreate))
	{
		AfxMessageBox("无法创建文件"+strFileName+"!");
		return;
	}
	
	CArchive ar(&fBinOut,CArchive::store);

	Serialize(ar);
	
	ar.Close();
	fBinOut.Close();
}

BOOL CCatalogList::GetFromFile(CString strFileName)  // view the word list content
{
	CFile	nfBinOut;
	if(!nfBinOut.Open(strFileName,CFile::typeBinary|CFile::modeRead))
	{
		AfxMessageBox("无法打开文件"+strFileName+"!");
		return FALSE;
	}
	/*
	char sRead[100]; 
	nfBinOut.Read(sRead,100);
	int i=0;
	while(i<100)
	{
		std::cout< void AFXAPI SerializeElements  (CArchive& ar, CCatalogNode* pElements, INT_PTR nCount) 
{
	ASSERT(nCount==0||
		AfxIsValidAddress(pElements,nCount*sizeof(CCatalogNode)));
	pElements->Serialize(ar);
}


void AFXAPI SerializeElements(CArchive& ar,CCatalogList* pElements,int nCount)
{
	ASSERT(nCount==0||
		AfxIsValidAddress(pElements,nCount*sizeof(CCatalogList)));
	pElements->Serialize(ar);
}

void CCatalogNode::Serialize(CArchive &ar)
{
	if(ar.IsStoring())
	{
		ar<>m_idxCata;
		ar>>m_strDirName;
		ar>>m_lTotalWordNum;
		ar>>m_strCatalogName;
	}
	//m_lstDocList.Serialize(ar);
}

const CCatalogList& CCatalogList::operator = (const CCatalogList& x)
{
	if(this==&x) return *this;
	m_lstCatalogList.RemoveAll();
	POSITION pos = x.m_lstCatalogList.GetHeadPosition();
	while(pos!=NULL)
	{
		CCatalogNode catanode=x.m_lstCatalogList.GetNext(pos);
		m_lstCatalogList.AddTail(catanode);
	}


	return *this;
}

const CCatalogList& CCatalogList::operator += (const CCatalogList& x)
{
	if(this==&x) return *this;
	POSITION pos = x.m_lstCatalogList.GetHeadPosition();
	while(pos!=NULL)
	{
		CCatalogNode catanode=x.m_lstCatalogList.GetNext(pos);
		m_lstCatalogList.AddTail(catanode);
	}
	return *this;
}

//nMode<=0 删除所有信息
//nMode=1  删除所有文档,但保留类别(节点)
//nMode=2  只删除文档向量所占用的内存
void CCatalogList::InitCatalogList(int nMode)
{

}



POSITION CCatalogList::AddCata(CCatalogNode &catanode)
{
	return m_lstCatalogList.AddTail(catanode);
}

CCatalogNode& CCatalogList::GetNext(POSITION &rPos)
{
	return m_lstCatalogList.GetNext(rPos);
}


CCatalogNode& CCatalogList::GetAt(POSITION pos)
{
	return m_lstCatalogList.GetAt(pos);
}
声明: 本文采用 BY-NC-SA 协议进行授权. 转载请注明转自: 具体化SerializeElements
  1. Woo0ooW | 2019年7月15日11:36 | #1

    请问博主,您github上的 game server,如果配置跑起来。。。我兴趣于游戏开发,但是一直找不到一个可用的游戏框架,望大佬指教

  1. 本文目前尚无任何 trackbacks 和 pingbacks.