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.

111 lines
2.9 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright (C) Microsoft Corporation, 1998.
  3. //
  4. // temparr.hpp
  5. //
  6. // Template class used by Direct3D ReferenceRasterizer 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. template <class T> class TemplArray
  16. {
  17. public:
  18. TemplArray( void );
  19. ~TemplArray( void );
  20. // It is the user of this operator who makes sure 0<=iIndex<m_dwArraySize.
  21. T& operator []( int iIndex );
  22. HRESULT CheckAndGrow( DWORD iIndex, DWORD dwGrowDelta = 16 );
  23. HRESULT CheckRange ( DWORD iIndex );
  24. // The user needs to make sure 0<=m_dwCurrent<m_dwArraySize.
  25. inline T CurrentItem(void) { return m_pArray[m_dwCurrent];};
  26. inline void SetCurrentItem(T item) { m_pArray[m_dwCurrent] = item;};
  27. inline DWORD CurrentIndex(void) { return m_dwCurrent;};
  28. inline void SetCurrentIndex(DWORD dwIdx) {m_dwCurrent = dwIdx;};
  29. inline DWORD ArraySize(void) { return m_dwArraySize;};
  30. private:
  31. T *m_pArray;
  32. DWORD m_dwArraySize;
  33. // Index to the current item or the size of data stored in the array
  34. DWORD m_dwCurrent;
  35. };
  36. template <class T>
  37. TemplArray< T >::TemplArray( void )
  38. {
  39. m_pArray = NULL;
  40. m_dwArraySize = 0;
  41. m_dwCurrent = 0;
  42. }
  43. template <class T>
  44. TemplArray< T >::~TemplArray( void )
  45. {
  46. if (m_pArray != NULL)
  47. delete m_pArray;
  48. m_dwArraySize = 0;
  49. }
  50. template <class T> T&
  51. TemplArray< T >::operator[]( int iIndex )
  52. {
  53. return m_pArray[iIndex];
  54. }
  55. template <class T> HRESULT
  56. TemplArray< T >::CheckAndGrow( DWORD iIndex, DWORD dwGrowDelta )
  57. {
  58. if (iIndex >= m_dwArraySize)
  59. {
  60. DWORD dwNewArraySize = m_dwArraySize + dwGrowDelta;
  61. while (iIndex >= dwNewArraySize)
  62. dwNewArraySize += dwGrowDelta;
  63. T *pTmpArray = new T[dwNewArraySize];
  64. if (pTmpArray == NULL)
  65. return DDERR_OUTOFMEMORY;
  66. memset(pTmpArray, 0, sizeof(T) * dwNewArraySize);
  67. if (m_pArray != NULL)
  68. {
  69. _ASSERT(m_dwArraySize != 0,
  70. "CheckAndGrow: Array size cannot be NULL" );
  71. // Copy existing stuff into new array
  72. memcpy(pTmpArray, m_pArray, m_dwArraySize * sizeof(T));
  73. // Free up existing array
  74. delete m_pArray;
  75. }
  76. // Assign new array
  77. m_pArray = pTmpArray;
  78. m_dwArraySize = dwNewArraySize;
  79. }
  80. return D3D_OK;
  81. }
  82. template <class T> HRESULT
  83. TemplArray< T >::CheckRange( DWORD iIndex )
  84. {
  85. if (iIndex >= m_dwArraySize)
  86. {
  87. return DDERR_INVALIDPARAMS;
  88. }
  89. return D3D_OK;
  90. }
  91. #endif _TEMPLARR_HPP