首页 > C++ > 具体化SerializeElements

具体化SerializeElements

Dec 14th,2011 发表评论

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

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


CATALOGLIST.H

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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<CString,CString&>			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<CCatalogNode,CCatalogNode&>	m_lstCatalogList;
};
//void AFXAPI SerializeElements(CArchive& ar,CCatalogList* pElements,int nCount);

CATALOGLIST.CPP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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<<sRead[i];
		i++;
	}
	*/
 
	CArchive ar(&nfBinOut,CArchive::load);
 
	Serialize(ar);
	ar.Close();
 
	nfBinOut.Close();
	return TRUE;
}
 
void CCatalogList::Serialize(CArchive &ar)
{
	m_lstCatalogList.Serialize(ar);
}
 
//void AFXAPI SerializeElements(CArchive& ar,CCatalogNode* pElements,int nCount)
template <> void AFXAPI SerializeElements <CCatalogNode> (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;
	}
	else
	{
		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. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.