GrowArray.h
Upload User: geng8029
Upload Date: 2021-01-30
Package Size: 187k
Code Size: 3k
Category:

Audio program

Development Platform:

Visual C++

  1. //-----------------------------------------------------------------------------
  2. // File: GrowArray.h
  3. // Desc: Growable array class.
  4. //
  5. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  6. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  7. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  8. // PARTICULAR PURPOSE.
  9. //
  10. //  Copyright (C) Microsoft Corporation. All rights reserved.
  11. //-----------------------------------------------------------------------------
  12. #pragma once
  13. namespace MediaFoundationSamples
  14. {
  15.     // Class template: Re-sizable array. 
  16.     // To grow or shrink the array, call SetSize(). 
  17.     // To pre-allocate the array, call Allocate(). 
  18.     // Notes:
  19.     // Copy constructor and assignment operator are private, to avoid throwing exceptions. (One could easily modify this.)
  20.     // It is the caller's responsibility to release the objects in the array. The array's destuctor does not release them.
  21.     // The array does not actually shrink when SetSize is called with a smaller size. Only the reported size changes.
  22.     template <class T>
  23.     class GrowableArray
  24.     {
  25.     public:
  26.         GrowableArray() : m_count(0), m_allocated(0), m_pArray(NULL)
  27.         {
  28.             
  29.         }
  30.         virtual ~GrowableArray()
  31.         {
  32.             SAFE_ARRAY_DELETE(m_pArray);
  33.         }
  34.         // Allocate: Reserves memory for the array, but does not increase the count.
  35.         HRESULT Allocate(DWORD alloc)
  36.         {
  37.             HRESULT hr = S_OK;
  38.             if (alloc > m_allocated)
  39.             {
  40.                 T *pTmp = new T[alloc];
  41.                 if (pTmp)
  42.                 {
  43.                     ZeroMemory(pTmp, alloc * sizeof(T));
  44.                     assert(m_count <= m_allocated);
  45.                     // Copy the elements to the re-allocated array.
  46.                     for (DWORD i = 0; i < m_count; i++)
  47.                     {
  48.                         pTmp[i] = m_pArray[i];
  49.                     }
  50.                     delete [] m_pArray;
  51.                     m_pArray = pTmp;
  52.                     m_allocated = alloc;
  53.                 }
  54.                 else
  55.                 {
  56.                     hr = E_OUTOFMEMORY;
  57.                 }
  58.             }
  59.             return hr;
  60.         }
  61.         // SetSize: Changes the count, and grows the array if needed.
  62.         HRESULT SetSize(DWORD count)
  63.         {
  64.             assert (m_count <= m_allocated);
  65.             HRESULT hr = S_OK;
  66.             if (count > m_allocated)
  67.             {
  68.                 hr = Allocate(count);
  69.             }
  70.             if (SUCCEEDED(hr))
  71.             {
  72.                 m_count = count;
  73.             }
  74.             return hr;
  75.         }
  76.         
  77.         DWORD GetCount() const { return m_count; }
  78.         // Accessor.
  79.         T& operator[](DWORD index)
  80.         {
  81.             assert(index < m_count);
  82.             return m_pArray[index];
  83.         }
  84.         // Const accessor.
  85.         const T& operator[](DWORD index) const
  86.         {
  87.             assert(index < m_count);
  88.             return m_pArray[index];
  89.         }
  90.         // Return the underlying array.
  91.         T* Ptr() { return m_pArray; }
  92.     protected:
  93.         GrowableArray& operator=(const GrowableArray& r);
  94.         GrowableArray(const GrowableArray &r);
  95.         T       *m_pArray;
  96.         DWORD   m_count;        // Nominal count.
  97.         DWORD   m_allocated;    // Actual allocation size.
  98.     };
  99. };  // namespace MediaFoundationSamples