//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 02/08/2001 // PROJECT: // SUBSYSTEM: // FILE: GenericList.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of GenericList.cpp //---------------------------------------------------------------------------- #include "stdafx.h" #include "GenericList.h" template CGenericList::CGenericList() { m_nData = 0; m_pData = NULL; } template CGenericList::~CGenericList() { RemoveAll(); } template BOOL CGenericList::Add( TYPE pData, INT nIndex ) { // allocate space for new element m_pData = (TYPE*) realloc( m_pData, sizeof(TYPE) * ( m_nData + 1 ) ); if( !m_pData ) { m_nData = 0; return FALSE; } // add to end if( nIndex < 0 || nIndex >= m_nData ) nIndex = m_nData; // shift elements ::MoveMemory( &m_pData[ nIndex + 1 ], &m_pData[ nIndex ], sizeof(TYPE) * ( m_nData - nIndex ) ); // initialize new element m_pData[ nIndex ] = pData; return TRUE; } template BOOL CGenericList::Remove( INT nIndex ) { // validate position if( nIndex < 0 || nIndex >= m_nData ) return FALSE; // decrease number of elements m_nData --; // shift elements ::MoveMemory( &m_pData[ nIndex ], &m_pData[ nIndex + 1 ], sizeof(TYPE) * ( m_nData - nIndex ) ); // deallocate space m_pData = (TYPE*) realloc( m_pData, sizeof(TYPE) * m_nData ); if( !m_pData ) { m_nData = 0; return FALSE; } return TRUE; } template void CGenericList::RemoveAll() { while( GetCount() ) Remove( 0 ); }