📄 objectlist.h
字号:
/**************************************************************************************
Project Name : Map Edit of my game
Module Name : Object list
File Name : ObjectList.h: interface for the CObjectList class.
Create : 2007-7-6, by Vigame
Update :
Copyright :
Reference :
Abstrct : The template of object list, not used.
**************************************************************************************/
#if !defined(AFX_OBJECTLIST_H__89A032ED_C7AE_412B_A030_A529ED685D2F__INCLUDED_)
#define AFX_OBJECTLIST_H__89A032ED_C7AE_412B_A030_A529ED685D2F__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
template <class Type>
struct nodeType
{
Type *object;
int nIndex;
nodeType<Type> *pNext;
};
template <class Type>
class CObjectList
{
public:
CObjectList();
virtual ~CObjectList();
public:
void InitList();
void DestoryList();
bool IsEmptyList(); // Returns true if the list is empty
int Length(); // Returns the number of objects in the list
Type * InsertObjectLast(Type *&ioNewObject);
void DeleteObject(Type *&inObject);
public:
nodeType<Type> *m_pFirst;
nodeType<Type> *m_pLast;
};
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
template <class Type>
CObjectList<Type>::CObjectList()
{
m_pFirst = NULL;
m_pLast = NULL;
}
template <class Type>
CObjectList<Type>::~CObjectList()
{
}
// Init and delete ///////////////////////////////////////////////////
template <class Type>
void CObjectList<Type>::InitList()
{
DestoryList();
}
template <class Type>
void CObjectList<Type>::DestoryList()
{
nodeType<Type> *current = NULL;
while (NULL != m_pFirst)
{
current = m_pFirst;
m_pFirst = m_pFisrt->pNext;
delete current->object;
delete current;
}
m_pLast = NULL;
}
// Is empty //////////////////////////////////////////////////////////
template <class Type>
bool CObjectList<Type>::IsEmptyList()
{
if (NULL == m_pFirst)
{
return true;
}
}
// List length ///////////////////////////////////////////////////////
template <class Type>
int CObjectList<Type>::Length()
{
int nCount = 0;
nodeType<Type> *current = m_pFirst;
while (NULL != current)
{
current = current->pNext;
nCount++;
}
return nCount;
}
// Insert Object last ////////////////////////////////////////////////
template <class Type>
Type * CObjectList<Type>::InsertObjectLast(Type *&ioNewObject)
{
nodeType<Type> *current = NULL;
current = new nodeType<Type>;
if (NULL == m_pFirst)
{
m_pFirst = current;
m_pLast = current;
m_pLast->pNext = NULL;
}
else
{
m_pLast->pNext = current;
m_pLast->pNext = NULL;
}
current->object = new Type;
ioNewObject = current->object;
return current->object;
}
// Delete Object ////////////////////////////////////////////////////
template <class Type>
void CObjectList<Type>::DeleteObject(Type *&inObject)
{
ASSERT(inObject != NULL);
nodeType<Type> *current = NULL;
nodeType<Type> *preCurrent = NULL;
if (NULL != m_pFirst)
{
current = m_pFirst->pNext;
preCurrent = m_pFirst;
}
if (inObject == m_pFirst)
{
m_pFirst = m_pFirst->pNext;
if (NULL != preCurrent->pNext)
{
preCurrent->pNext = NULL;
}
else
{
// If this list have one object.
m_pLast = NULL;
}
delete preCurrent->object;
delete preCurrent;
inObject = NULL;
}
else
{
while (NULL != current)
{
if (current->object == inObject)
{
// Find Object and delete it.
preCurrent->pNext = current->pNext;
current->pNext = NULL;
delete current->object;
delete current;
inObject = NULL;
// The object deleted was the last
if (NULL == preCurrent->pNext)
{
m_pLast = preCurrent;
}
}
else
{
preCurrent = current;
current = current->pNext;
}
}
}
if (NULL != inObject)
{
// Didn't find a object
}
}
#endif // !defined(AFX_OBJECTLIST_H__89A032ED_C7AE_412B_A030_A529ED685D2F__INCLUDED_)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -