Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
5.8 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright (C) Microsoft Corporation, 1998.
  3. //
  4. // temparr.hpp
  5. //
  6. // Template class used by Direct3D RefDev for stateset and so on.
  7. //
  8. // The following error codes should be defined before included this file:
  9. // DDERR_OUTOFMEMORY
  10. // D3D_OK
  11. // DDERR_INVALIDPARAMS
  12. ///////////////////////////////////////////////////////////////////////////////
  13. #ifndef _TEMPLARR_HPP
  14. #define _TEMPLARR_HPP
  15. //--------------------------------------------------------------------------
  16. //
  17. // Template for growable arrays
  18. //
  19. //--------------------------------------------------------------------------
  20. template <class ARRAY_ELEMENT>
  21. class GArrayT
  22. {
  23. public:
  24. GArrayT()
  25. {
  26. m_pArray = NULL;
  27. m_dwArraySize = 0;
  28. m_dwGrowSize = 8;
  29. }
  30. ~GArrayT()
  31. {
  32. char tmp[256];
  33. wsprintf( tmp, "m_dwArraySize = %d, m_pArray = %08x\n", m_dwArraySize,
  34. m_pArray );
  35. _ASSERT( !((m_dwArraySize == 0)^(m_pArray == NULL)), tmp );
  36. if( m_pArray ) delete[] m_pArray;
  37. }
  38. virtual void SetGrowSize( DWORD dwGrowSize)
  39. {
  40. m_dwGrowSize = dwGrowSize;
  41. }
  42. virtual HRESULT Grow( DWORD dwIndex )
  43. {
  44. if( dwIndex < m_dwArraySize ) return S_OK;
  45. DWORD dwNewArraySize = (dwIndex/m_dwGrowSize + 1) * m_dwGrowSize;
  46. ARRAY_ELEMENT *pNewArray = AllocArray( dwNewArraySize );
  47. if( pNewArray == NULL ) return DDERR_OUTOFMEMORY;
  48. for( DWORD i = 0; i<m_dwArraySize; i++ )
  49. pNewArray[i] = m_pArray[i];
  50. delete[] m_pArray;
  51. m_pArray = pNewArray;
  52. m_dwArraySize = dwNewArraySize;
  53. return S_OK;
  54. }
  55. virtual HRESULT Grow( DWORD dwIndex, BOOL* pRealloc )
  56. {
  57. if( dwIndex < m_dwArraySize )
  58. {
  59. if( pRealloc ) *pRealloc = FALSE;
  60. return S_OK;
  61. }
  62. if( pRealloc ) *pRealloc = TRUE;
  63. DWORD dwNewArraySize = m_dwArraySize;
  64. while( dwNewArraySize <= dwIndex ) dwNewArraySize += m_dwGrowSize;
  65. ARRAY_ELEMENT *pNewArray = AllocArray( dwNewArraySize );
  66. if( pNewArray == NULL ) return DDERR_OUTOFMEMORY;
  67. for( DWORD i = 0; i<m_dwArraySize; i++ )
  68. pNewArray[i] = m_pArray[i];
  69. delete[] m_pArray;
  70. m_pArray = pNewArray;
  71. m_dwArraySize = dwNewArraySize;
  72. return S_OK;
  73. }
  74. virtual ARRAY_ELEMENT *AllocArray( DWORD dwSize ) const
  75. {
  76. return new ARRAY_ELEMENT[dwSize];
  77. }
  78. virtual ARRAY_ELEMENT& operator []( DWORD dwIndex ) const
  79. {
  80. char tmp[256];
  81. wsprintf( tmp, "dwIndex = %d, m_dwArraySize = %d\n", dwIndex,
  82. m_dwArraySize );
  83. _ASSERT(dwIndex < m_dwArraySize, tmp);
  84. return m_pArray[dwIndex];
  85. }
  86. virtual BOOL IsValidIndex( DWORD dwIndex ) const
  87. {
  88. return (dwIndex < m_dwArraySize);
  89. }
  90. virtual DWORD GetSize() const
  91. {
  92. return m_dwArraySize;
  93. }
  94. virtual DWORD GetGrowSize() const
  95. {
  96. return m_dwGrowSize;
  97. }
  98. protected:
  99. ARRAY_ELEMENT *m_pArray;
  100. DWORD m_dwArraySize;
  101. DWORD m_dwGrowSize;
  102. };
  103. //--------------------------------------------------------------------------
  104. //
  105. // A more powerful template for a growable array
  106. //
  107. //--------------------------------------------------------------------------
  108. template <class T> class TemplArray
  109. {
  110. public:
  111. TemplArray( void );
  112. ~TemplArray( void );
  113. // It is the user of this operator who makes sure 0<=iIndex<m_dwArraySize.
  114. T& operator []( int iIndex );
  115. HRESULT CheckAndGrow( DWORD iIndex, DWORD dwGrowDelta = 16 );
  116. HRESULT CheckRange ( DWORD iIndex );
  117. // The user needs to make sure 0<=m_dwCurrent<m_dwArraySize.
  118. inline T CurrentItem(void) { return m_pArray[m_dwCurrent];};
  119. inline void SetCurrentItem(T item) { m_pArray[m_dwCurrent] = item;};
  120. inline DWORD CurrentIndex(void) { return m_dwCurrent;};
  121. inline void SetCurrentIndex(DWORD dwIdx) {m_dwCurrent = dwIdx;};
  122. inline DWORD ArraySize(void) { return m_dwArraySize;};
  123. private:
  124. T *m_pArray;
  125. DWORD m_dwArraySize;
  126. // Index to the current item or the size of data stored in the array
  127. DWORD m_dwCurrent;
  128. };
  129. template <class T>
  130. TemplArray< T >::TemplArray( void )
  131. {
  132. m_pArray = NULL;
  133. m_dwArraySize = 0;
  134. m_dwCurrent = 0;
  135. }
  136. template <class T>
  137. TemplArray< T >::~TemplArray( void )
  138. {
  139. if (m_pArray != NULL)
  140. delete m_pArray;
  141. m_dwArraySize = 0;
  142. }
  143. template <class T> T&
  144. TemplArray< T >::operator[]( int iIndex )
  145. {
  146. return m_pArray[iIndex];
  147. }
  148. template <class T> HRESULT
  149. TemplArray< T >::CheckAndGrow( DWORD iIndex, DWORD dwGrowDelta )
  150. {
  151. if (iIndex >= m_dwArraySize)
  152. {
  153. DWORD dwNewArraySize = m_dwArraySize + dwGrowDelta;
  154. while (iIndex >= dwNewArraySize)
  155. dwNewArraySize += dwGrowDelta;
  156. T *pTmpArray = new T[dwNewArraySize];
  157. if (pTmpArray == NULL)
  158. return DDERR_OUTOFMEMORY;
  159. memset(pTmpArray, 0, sizeof(T) * dwNewArraySize);
  160. if (m_pArray != NULL)
  161. {
  162. _ASSERT(m_dwArraySize != 0,
  163. "CheckAndGrow: Array size cannot be NULL" );
  164. // Copy existing stuff into new array
  165. memcpy(pTmpArray, m_pArray, m_dwArraySize * sizeof(T));
  166. // Free up existing array
  167. delete [] m_pArray;
  168. }
  169. // Assign new array
  170. m_pArray = pTmpArray;
  171. m_dwArraySize = dwNewArraySize;
  172. }
  173. return D3D_OK;
  174. }
  175. template <class T> HRESULT
  176. TemplArray< T >::CheckRange( DWORD iIndex )
  177. {
  178. if (iIndex >= m_dwArraySize)
  179. {
  180. return DDERR_INVALIDPARAMS;
  181. }
  182. return D3D_OK;
  183. }
  184. #endif _TEMPLARR_HPP