namespace CertSrv { template class TPtrListEnum; template class TPtrList { public: TPtrList() : m_pHead(NULL), m_dwCount(0) {} ~TPtrList(){ Cleanup(); } bool AddHead(C* pData); bool AddTail(C* pData); void Cleanup(); DWORD GetCount() const { return m_dwCount;} bool IsEmpty() const {return NULL==m_pHead;} bool InsertAt(C* pData, DWORD dwIndex); bool RemoveAt(DWORD dwIndex); C* GetAt(DWORD dwIndex); C* Find(C& Data); DWORD FindIndex(C& Data); friend class TPtrListEnum; protected: struct TPtrListNode { TPtrListNode(C* pData) : m_pData(pData), m_pNext(NULL) {} ~TPtrListNode() { delete m_pData;} C* m_pData; TPtrListNode* m_pNext; }; typedef TPtrListNode* TNODEPTR; typedef TPtrListNode TNODE; TNODEPTR m_pHead; DWORD m_dwCount; }; template class TPtrListEnum { public: TPtrListEnum() : m_pList(NULL), m_pCrt(NULL) {} TPtrListEnum(const TPtrList& List) { Set(List);} void Set(const TPtrList& List) { m_pList = &List; Reset(); } void Reset() { m_pCrt = m_pList->m_pHead; } C* Next(); protected: const TPtrList *m_pList; typename TPtrList::TNODEPTR m_pCrt; }; template C* TPtrListEnum::Next() { if(!m_pCrt) return NULL; C* pResult = m_pCrt->m_pData; m_pCrt = m_pCrt->m_pNext; return pResult; } template void TPtrList::Cleanup() { TNODEPTR pCrt, pNext; for(pCrt = m_pHead; pCrt; pCrt=pNext) { pNext = pCrt->m_pNext; delete pCrt; } m_pHead = NULL; m_dwCount = 0; } template bool TPtrList::AddHead(C* pData) { TNODEPTR pNew = new TNODE(pData); if(!pNew) return false; pNew->m_pNext = m_pHead; m_pHead = pNew; m_dwCount++; return true; } template bool TPtrList::AddTail(C* pData) { TNODEPTR pNew = new TNODE(pData); if(!pNew) return false; for(TNODEPTR *ppCrt = &m_pHead; *ppCrt; ppCrt = &(*ppCrt)->m_pNext) NULL; *ppCrt = pNew; m_dwCount++; return true; } template C* TPtrList::Find(C& Data) { TPtrListEnum ListEnum(*this); for(C* pResult = ListEnum.Next(); pResult; pResult= ListEnum.Next()) { if(*pResult == Data) return pResult; } return NULL; } #define DWORD_MAX 0xffffffff template DWORD TPtrList::FindIndex(C& Data) { DWORD dwIndex = 0; TPtrListEnum ListEnum(*this); for(C* pResult = ListEnum.Next(); pResult; pResult= ListEnum.Next(), dwIndex++) { if(*pResult == Data) return dwIndex; } return DWORD_MAX; } template bool TPtrList::InsertAt(C* pData, DWORD dwIndex) { DWORD dwCrt; TNODEPTR *ppCrt; TNODEPTR pNew = new TNODE(pData); if(!pNew) return false; for(ppCrt = &m_pHead, dwCrt=0; NULL!=*ppCrt && (dwCrtm_pNext, dwCrt++) NULL; pNew->m_pNext = *ppCrt; *ppCrt = pNew; m_dwCount++; return true; } template bool TPtrList::RemoveAt(DWORD dwIndex) { DWORD dwCrt; TNODEPTR *ppCrt, pDel; for(ppCrt = &m_pHead, dwCrt=0; NULL!=*ppCrt && (dwCrtm_pNext, dwCrt++) NULL; if(!*ppCrt) return false; pDel = *ppCrt; *ppCrt = (*ppCrt)->m_pNext; delete pDel; m_dwCount--; return true; } template C* TPtrList::GetAt(DWORD dwIndex) { DWORD dwCrt; TNODEPTR pCrt; for(pCrt = m_pHead, dwCrt=0; NULL!=pCrt && (dwCrtm_pNext, dwCrt++) NULL; return pCrt?pCrt->m_pData:NULL; } } // namespace CertSrv