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.

119 lines
2.3 KiB

  1. #ifndef __ARRAY_HXX_INCLUDED__
  2. #define __ARRAY_HXX_INCLUDED__
  3. #pragma warning(disable:4710) // inline fn not expanded (retail warning)
  4. template <class T> class CAutoArray
  5. {
  6. public:
  7. CAutoArray();
  8. virtual ~CAutoArray();
  9. int Size() { return _iHighestItemNum; }
  10. bool EnsureSize(int minSize);
  11. bool GetAt( int index, T* pRet );
  12. bool Set( int index, T& val );
  13. bool Append( T& val );
  14. void Empty();
  15. private:
  16. T *_pArray;
  17. int _iArraySize;
  18. int _iHighestItemNum;
  19. enum { ARRAY_GROWTH_GRANULARITY = 4 };
  20. };
  21. template <class T>
  22. CAutoArray<T>::CAutoArray() :
  23. _pArray( NULL ),
  24. _iArraySize( 0 ),
  25. _iHighestItemNum( 0 )
  26. {
  27. }
  28. template <class T>
  29. CAutoArray<T>::~CAutoArray()
  30. {
  31. if ( _pArray ) {
  32. delete [] _pArray;
  33. }
  34. }
  35. template <class T>
  36. bool CAutoArray<T>::EnsureSize(int minSize)
  37. {
  38. assert( minSize > 0 );
  39. int i;
  40. if ( minSize > _iArraySize ) {
  41. T* pNewArray = new T[minSize+ARRAY_GROWTH_GRANULARITY];
  42. if ( pNewArray ) {
  43. if ( _pArray ) {
  44. for (i=0 ; i < _iHighestItemNum ; ++i) {
  45. pNewArray[i] = _pArray[i];
  46. }
  47. delete [] _pArray;
  48. }
  49. _pArray = pNewArray;
  50. _iArraySize = minSize+ARRAY_GROWTH_GRANULARITY;
  51. return true; // new array alloc'ed/copied
  52. }
  53. return false; // failed to create new array
  54. }
  55. return true;
  56. }
  57. template <class T>
  58. bool CAutoArray<T>::GetAt( int index, T* pRet )
  59. {
  60. if ( index < _iArraySize ) {
  61. *pRet = _pArray[index];
  62. return true;
  63. }
  64. return false;
  65. }
  66. template <class T>
  67. bool CAutoArray<T>::Set( int index, T& val )
  68. {
  69. if ( index < _iArraySize ) {
  70. _pArray[index] = val;
  71. if ( index+1 > _iHighestItemNum ) {
  72. _iHighestItemNum = index+1;
  73. }
  74. return true;
  75. }
  76. return false;
  77. }
  78. template <class T>
  79. bool CAutoArray<T>::Append( T& val )
  80. {
  81. unsigned int iIndex = Size();
  82. if (EnsureSize(iIndex + 1))
  83. {
  84. return Set(iIndex, val);
  85. }
  86. return false;
  87. }
  88. template <class T>
  89. void CAutoArray<T>::Empty()
  90. {
  91. if ( _pArray ) {
  92. delete [] _pArray;
  93. }
  94. _pArray = NULL;
  95. _iHighestItemNum = 0;
  96. }
  97. #endif // __ARRAY_HXX_INCLUDED__