实验: 用模板来管理类队列

/** * @file LsContainerDeque.h */ #if !defined(_LSCONTAINERDEQUE_H_) #define _LSCONTAINERDEQUE_H_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include <deque> template <class T> class CLsContainerDeque { public: CLsContainerDeque(); virtual ~CLsContainerDeque(); void AddObj(T * pObj, int nIndex = -1); int GetObjCount(); BOOL IsValidPositon(int nIndex); int GetObjIndex(const T * pObj); T * GetObj(int nIndex); void SwapObj(int nIndexDst, int nIndexSrc); void SwapObj(int nIndexDst, T * pObjSrc); void SwapObj(T * pObjDst, T * pObjSrc); void ThrowToOwnership(std::deque<T *>& listObj); void DelObj(int nIndex); void DelObj(T * pObj); void DelAllObj(); private: std::deque<T *> m_stlList ; }; #include "LsContainerDeque.inl" #endif // !defined(_LSCONTAINERDEQUE_H_)

/** * @file LsContainerDeque.inl */ template <class T> CLsContainerDeque<T>::CLsContainerDeque<T>() { } template <class T> CLsContainerDeque<T>::~CLsContainerDeque<T>() { DelAllObj(); } template <class T> void CLsContainerDeque<T>::AddObj(T * pObj, int nIndex) { if(-1 == GetObjIndex(pObj)) { if((nIndex >= 0) && (nIndex <= GetObjCount())) { m_stlList.insert(m_stlList.begin() + nIndex, pObj) ; } else { m_stlList.push_back(pObj) ; } } } template <class T> void CLsContainerDeque<T>::DelAllObj() { for(size_t i = 0 ; i < m_stlList.size() ; i++) { T * p = m_stlList[i] ; if(p) { delete p ; p = NULL; } } m_stlList.clear() ; } template <class T> void CLsContainerDeque<T>::DelObj(int nIndex) { if((nIndex >= 0) && (nIndex < GetObjCount())) { T * p = m_stlList[nIndex] ; m_stlList.erase(m_stlList.begin() + nIndex); if(p) { delete p; p = NULL; } } } template <class T> void CLsContainerDeque<T>::DelObj(T * pObj) { if(pObj) { DelObj(GetObjIndex(pObj)) ; } } template <class T> void CLsContainerDeque<T>::ThrowToOwnership(std::deque<T *>& listObj) { listObj = m_stlList ; m_stlList.clear() ; } template <class T> int CLsContainerDeque<T>::GetObjCount() { return (int)m_stlList.size(); } template <class T> T * CLsContainerDeque<T>::GetObj(int nIndex) { if((nIndex >= 0) && (nIndex < GetObjCount())) { return m_stlList[nIndex] ; } else { return 0; } } template <class T> int CLsContainerDeque<T>::GetObjIndex(const T * pObj) { for(size_t i = 0; i < m_stlList.size(); i++) { if(m_stlList[i] == pObj) { return (int)i; } } return -1; } template <class T> BOOL CLsContainerDeque<T>::IsValidPositon(int nIndex) { int nCnt = GetObjCount(); return ((nIndex >= 0) && (nIndex < nCnt)) ? TRUE : FALSE; } template <class T> void CLsContainerDeque<T>::SwapObj(int nIndexDst, int nIndexSrc) { T * pObjDst = GetObj(nIndexDst); T * pObjSrc = GetObj(nIndexSrc); if(!IsValidPositon(nIndexDst)) { return; } if(!IsValidPositon(nIndexSrc)) { return; } if(nIndexDst == nIndexSrc) { return; } if(pObjDst == pObjSrc) { return; } m_stlList[nIndexSrc] = pObjDst; m_stlList[nIndexDst] = pObjSrc; } template <class T> void CLsContainerDeque<T>::SwapObj(int nIndexDst, T * pObjSrc) { T * pObjDst = GetObj(nIndexDst); int nIndexSrc = GetObjIndex(pObjSrc); if(!IsValidPositon(nIndexDst)) { return; } if(!IsValidPositon(nIndexSrc)) { return; } if(nIndexDst == nIndexSrc) { return; } if(pObjDst == pObjSrc) { return; } m_stlList[nIndexSrc] = pObjDst; m_stlList[nIndexDst] = pObjSrc; } template <class T> void CLsContainerDeque<T>::SwapObj(T * pObjDst, T * pObjSrc) { int nIndexSrc = GetObjIndex(pObjSrc); int nIndexDst = GetObjIndex(pObjDst); if(!IsValidPositon(nIndexDst)) { return; } if(!IsValidPositon(nIndexSrc)) { return; } if(nIndexDst == nIndexSrc) { return; } if(pObjDst == pObjSrc) { return; } m_stlList[nIndexSrc] = pObjDst; m_stlList[nIndexDst] = pObjSrc; }

int fnTestContainerDeque() { /** * 用模板来管理类队列的好处是: * 不用根据每一种数据类制作一个新的类队列的类 */ CLsContainerDeque<CGeneralClass> ContainerDeque; CGeneralClass * pData = NULL; CGeneralClass * pData1 = NULL; CGeneralClass * pData2 = NULL; /** * 往容器里添加数据 */ pData = new CGeneralClass(); ContainerDeque.AddObj(pData); pData = new CGeneralClass(); pData1 = pData; ContainerDeque.AddObj(pData); pData = new CGeneralClass(); pData2 = pData; ContainerDeque.AddObj(pData); /** * 假设有排序需求, * 需要交换数据的位置 */ ContainerDeque.SwapObj(0, 2); ContainerDeque.SwapObj(0, pData2); ContainerDeque.SwapObj(pData1, pData2); /** * 删除废数据 */ pData = ContainerDeque.GetObj(20);//给定位置 ContainerDeque.DelObj(pData); ContainerDeque.DelObj(pData1);//给定数据指针 /** * 在ContainerDeque中压入的数据, * 在ContainerDeque析构的时候释放掉了 */ return S_OK; }

 

 

你可能感兴趣的:(实验: 用模板来管理类队列)