#include "precomp.h" #include "NacList.h" template NacList::NacList(int nInitialSize, int nGrowthRate) : m_nSize(0), m_nHeadIndex(0), m_nTotalSize(nInitialSize), m_nGrowthRate(nGrowthRate) { ASSERT(nInitialSize > 0); ASSERT(nGrowthRate > 0); DBG_SAVE_FILE_LINE m_aElements = new T[nInitialSize]; if (m_aElements == NULL) { ERROR_OUT(("NacList::NacList - Out of memory")); } } template NacList::~NacList() { Flush(); delete [] m_aElements; } template void NacList::Flush() { m_nHeadIndex = 0; m_nSize = 0; } template bool NacList::PeekFront(T *pT) { if (m_nSize <= 0) { return false; } *pT = m_aElements[m_nHeadIndex]; return true; } template bool NacList::PeekRear(T *pT) { int nRearIndex; if (m_nSize <= 0) { return false; } nRearIndex = (m_nHeadIndex + m_nSize - 1) % m_nTotalSize; *pT = m_aElements[nRearIndex]; return true; } template bool NacList::PushFront(const T &t) { int nInsertIndex; // do we need to grow if (m_nSize >= m_nTotalSize) { Grow(); } if (m_nHeadIndex == 0) { m_nHeadIndex = m_nTotalSize - 1; } else { --m_nHeadIndex; } m_aElements[m_nHeadIndex] = t; m_nSize++; return true; } template bool NacList::PushRear(const T &t) { int nInsertIndex; // do we need to grow if (m_nSize >= m_nTotalSize) { Grow(); } nInsertIndex = (m_nHeadIndex + m_nSize) % m_nTotalSize; m_aElements[nInsertIndex] = t; m_nSize++; return true; } template bool NacList::PopFront(T *pT) { ASSERT(m_nSize >= 0); if (m_nSize <= 0) { return false; } *pT = m_aElements[m_nHeadIndex]; m_nHeadIndex = (m_nHeadIndex + 1) % m_nTotalSize; m_nSize--; return true; } template bool NacList::PopRear(T *pT) { int nRearIndex; ASSERT(m_nSize >= 0); if (m_nSize <= 0) { return false; } nRearIndex = (m_nHeadIndex + m_nSize - 1) % m_nTotalSize; *pT = m_aElements[nRearIndex]; m_nSize--; return true; } template int NacList::Grow() { T *aNew; int nTotalSize; int nIndex, nCopyIndex; nTotalSize = m_nTotalSize + m_nGrowthRate; DBG_SAVE_FILE_LINE aNew = new T[nTotalSize]; if (aNew == NULL) { ERROR_OUT(("Out of Memory")); return 0; } for (nIndex = 0; nIndex < m_nSize; nIndex++) { nCopyIndex = (nIndex + m_nHeadIndex) % m_nTotalSize; aNew[nIndex] = m_aElements[nCopyIndex]; } delete [] m_aElements; m_aElements = aNew; m_nTotalSize = nTotalSize; m_nHeadIndex = 0; return (nTotalSize); } // Thread Safe List template ThreadSafeList::ThreadSafeList(int nInitialSize, int nGrowthRate) : NacList(nInitialSize, nGrowthRate) { InitializeCriticalSection(&m_cs); } template ThreadSafeList::~ThreadSafeList() { DeleteCriticalSection(&m_cs); } template void ThreadSafeList::Flush() { EnterCriticalSection(&m_cs); NacList::Flush(); LeaveCriticalSection(&m_cs); } template bool ThreadSafeList::PeekFront(T *pT) { bool bRet; EnterCriticalSection(&m_cs); bRet = NacList::PeekFront(pT); LeaveCriticalSection(&m_cs); return bRet; } template bool ThreadSafeList::PeekRear(T *pT) { bool bRet; EnterCriticalSection(&m_cs); bRet = NacList::PeekRear(pT); LeaveCriticalSection(&m_cs); return bRet; } template bool ThreadSafeList::PushFront(const T &t) { bool bRet; EnterCriticalSection(&m_cs); bRet = NacList::PushFront(t); LeaveCriticalSection(&m_cs); return bRet; } template bool ThreadSafeList::PushRear(const T &t) { bool bRet; EnterCriticalSection(&m_cs); bRet = NacList::PushRear(t); LeaveCriticalSection(&m_cs); return bRet; } template bool ThreadSafeList::PopFront(T *pT) { bool bRet; EnterCriticalSection(&m_cs); bRet = NacList::PopFront(pT); LeaveCriticalSection(&m_cs); return bRet; } template bool ThreadSafeList::PopRear(T *pT) { bool bRet; EnterCriticalSection(&m_cs); bRet = NacList::PopRear(pT); LeaveCriticalSection(&m_cs); return bRet; } template int ThreadSafeList::Size() { int nRet; EnterCriticalSection(&m_cs); nRet = NacList::Size(); LeaveCriticalSection(&m_cs); return nRet; } // each instance type of the template needs to be declared here // For example: // template class NacList; // list of integers // template class NacList; // list of pointers to integers // you have to disable warnings for the following error, else // the compiler thinks that a second instantiation is occuring // when it's really only a second declaration // This generates a warning which becomes an error #pragma warning(disable:4660) #include "PacketSender.h" template class ThreadSafeList;