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.

139 lines
3.0 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1991-1998
  5. //
  6. // File: tgrow.hxx
  7. //
  8. // Contents: Growable object with a stack-allocated base size
  9. //
  10. // Templates: XGrowable
  11. //
  12. // Notes: XGrowable is suitable for use with basic types only.
  13. //
  14. // History: 19-Feb-97 dlee Created
  15. // 02-Mar-98 KLam Added Copy method
  16. //
  17. //----------------------------------------------------------------------------
  18. #pragma once
  19. template <class T, unsigned C = MAX_PATH> class XGrowable
  20. {
  21. public:
  22. XGrowable( unsigned cInit = C ) :
  23. _pT( _aT ),
  24. _cT( C )
  25. {
  26. Win4Assert( 0 != _cT );
  27. SetSize( cInit );
  28. }
  29. XGrowable( XGrowable<T,C> const & src ) :
  30. _pT( _aT ),
  31. _cT( C )
  32. {
  33. Win4Assert( 0 != _cT );
  34. *this = src;
  35. }
  36. ~XGrowable() { Free(); }
  37. XGrowable<T,C> & operator =( XGrowable<T,C> const & src )
  38. {
  39. Win4Assert( 0 != _cT );
  40. Copy ( src.Get(), src.Count() );
  41. return *this;
  42. }
  43. T * Copy ( T const * pItem, unsigned cItems, unsigned iStart = 0 )
  44. {
  45. // Copies cItems of pItem starting at position iStart
  46. Win4Assert ( 0 != pItem );
  47. SetSize ( cItems + iStart );
  48. RtlCopyMemory ( _pT + iStart, pItem, cItems * sizeof(T) );
  49. return _pT;
  50. }
  51. void Free()
  52. {
  53. if ( _pT != _aT )
  54. {
  55. delete [] _pT;
  56. _pT = _aT;
  57. _cT = C;
  58. Win4Assert( 0 != _cT );
  59. }
  60. }
  61. T & operator[](unsigned iElem)
  62. {
  63. Win4Assert( iElem < _cT );
  64. return _pT[iElem];
  65. }
  66. T const & operator[](unsigned iElem) const
  67. {
  68. Win4Assert( iElem < _cT );
  69. return _pT[iElem];
  70. }
  71. T * SetSize( unsigned c )
  72. {
  73. Win4Assert( 0 != c );
  74. if ( c > _cT )
  75. {
  76. unsigned cOld = _cT;
  77. Win4Assert( 0 != _cT );
  78. do
  79. {
  80. _cT *= 2;
  81. }
  82. while ( _cT < c );
  83. T *pTmp = new T [ _cT ];
  84. RtlCopyMemory( pTmp, _pT, cOld * sizeof T );
  85. if ( _pT != _aT )
  86. delete [] _pT;
  87. _pT = pTmp;
  88. }
  89. return _pT;
  90. }
  91. void SetSizeInBytes( unsigned cb )
  92. {
  93. // round up to the next element size
  94. SetSize( (cb + sizeof T - 1 ) / sizeof T );
  95. }
  96. T * Get() { return _pT; }
  97. T const * Get() const { return _pT; }
  98. unsigned Count() const { return _cT; }
  99. unsigned SizeOf() const { return sizeof T * _cT; }
  100. void SetBuf( const T* p, unsigned cc )
  101. {
  102. Win4Assert( p );
  103. SetSize( cc );
  104. RtlCopyMemory( _pT,
  105. p,
  106. cc * sizeof( T ) );
  107. }
  108. private:
  109. unsigned _cT;
  110. T * _pT;
  111. T _aT[ C ];
  112. };