Source code of Windows XP (NT5)
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.

89 lines
2.0 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Copyright (C) 1994 - 1988, Microsoft Corporation
  4. //
  5. // File: xarray.hxx
  6. //
  7. // Contents: Safe array allocation templates
  8. //
  9. // Templates: XArray
  10. //
  11. // History: 14-Jul-98 SitaramR Created
  12. //
  13. //----------------------------------------------------------------------------
  14. #pragma once
  15. //+-------------------------------------------------------------------------
  16. //
  17. // Class: XArray
  18. //
  19. // Purpose: Smart array template based on the new/delete allocator
  20. //
  21. // History: 14-Jul-98 SitaramR Created
  22. //
  23. //--------------------------------------------------------------------------
  24. template <class T> class XArray
  25. {
  26. public:
  27. XArray() : _cElems( 0 ), _pElems( 0 )
  28. {
  29. }
  30. XArray( XArray<T> & src )
  31. {
  32. //
  33. // Don't do this in initializers -- _pElems is declared first
  34. // so the old array is acquired before the count is copied
  35. //
  36. _cElems = src._cElems;
  37. _pElems = src.Acquire();
  38. }
  39. ~XArray(void) { if (_pElems) { delete [] _pElems;} }
  40. BOOL Init( SIZE_T cElems )
  41. {
  42. Assert( _pElems == 0 );
  43. _cElems = cElems;
  44. _pElems = new T[(int)cElems];
  45. return ( _pElems != 0 );
  46. }
  47. void Set( SIZE_T cElems, T * pElems )
  48. {
  49. Assert( _pElems == 0 );
  50. _cElems = cElems;
  51. _pElems = pElems;
  52. }
  53. T * Get() const { return _pElems; }
  54. T * GetPointer() const { return _pElems; }
  55. T * Acquire() { T * p = _pElems; _pElems = 0; _cElems = 0; return p; }
  56. BOOL IsNull() const { return ( 0 == _pElems); }
  57. T & operator[](SIZE_T iElem) { return _pElems[iElem]; }
  58. T const & operator[](SIZE_T iElem) const { return _pElems[iElem]; }
  59. SIZE_T Count() const { return _cElems; }
  60. SIZE_T SizeOf() const { return _cElems * sizeof T; }
  61. void Free() { delete [] Acquire(); }
  62. private:
  63. T * _pElems;
  64. SIZE_T _cElems;
  65. };