/////////////////////////////////////////////////////////////////////////////// // Copyright (C) Microsoft Corporation, 1998. // // temparr.hpp // // Template class used by Direct3D ReferenceRasterizer for stateset and so on. // // The following error codes should be defined before included this file: // DDERR_OUTOFMEMORY // D3D_OK // DDERR_INVALIDPARAMS /////////////////////////////////////////////////////////////////////////////// #ifndef _TEMPLARR_HPP #define _TEMPLARR_HPP template class TemplArray { public: TemplArray( void ); ~TemplArray( void ); // It is the user of this operator who makes sure 0<=iIndex TemplArray< T >::TemplArray( void ) { m_pArray = NULL; m_dwArraySize = 0; m_dwCurrent = 0; } template TemplArray< T >::~TemplArray( void ) { if (m_pArray != NULL) delete m_pArray; m_dwArraySize = 0; } template T& TemplArray< T >::operator[]( int iIndex ) { return m_pArray[iIndex]; } template HRESULT TemplArray< T >::CheckAndGrow( DWORD iIndex, DWORD dwGrowDelta ) { if (iIndex >= m_dwArraySize) { DWORD dwNewArraySize = m_dwArraySize + dwGrowDelta; while (iIndex >= dwNewArraySize) dwNewArraySize += dwGrowDelta; T *pTmpArray = new T[dwNewArraySize]; if (pTmpArray == NULL) return DDERR_OUTOFMEMORY; memset(pTmpArray, 0, sizeof(T) * dwNewArraySize); if (m_pArray != NULL) { _ASSERT(m_dwArraySize != 0, "CheckAndGrow: Array size cannot be NULL" ); // Copy existing stuff into new array memcpy(pTmpArray, m_pArray, m_dwArraySize * sizeof(T)); // Free up existing array delete m_pArray; } // Assign new array m_pArray = pTmpArray; m_dwArraySize = dwNewArraySize; } return D3D_OK; } template HRESULT TemplArray< T >::CheckRange( DWORD iIndex ) { if (iIndex >= m_dwArraySize) { return DDERR_INVALIDPARAMS; } return D3D_OK; } #endif _TEMPLARR_HPP