Welcome to mirror list, hosted at ThFree Co, Russian Federation.

growarray.h « DSUtilLite « common - github.com/mpc-hc/LAVFilters.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 93bfbf679754539490730eed50374b737255af1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 *      Copyright (C) 2010-2015 Hendrik Leppkes
 *      http://www.1f0.de
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

// Class template: Re-sizable array. 

// To grow or shrink the array, call SetSize(). 
// To pre-allocate the array, call Allocate(). 

// Notes:
// Copy constructor and assignment operator are private, to avoid throwing exceptions. (One could easily modify this.)
// It is the caller's responsibility to release the objects in the array. The array's destuctor does not release them.
// The array does not actually shrink when SetSize is called with a smaller size. Only the reported size changes.

#pragma once

#include <assert.h>
#include "DShowUtil.h"

template <class T>
class GrowableArray
{
public:
  GrowableArray()
  {
  }

  virtual ~GrowableArray()
  {
    free(m_pArray);
  }

  // Allocate: Reserves memory for the array, but does not increase the count.
  HRESULT Allocate(DWORD alloc)
  {
    HRESULT hr = S_OK;
    if (alloc > m_allocated || !m_pArray)
    {
      T *pNew = (T *)realloc(m_pArray, sizeof(T) * alloc);
      if (!pNew) {
        free(m_pArray);
        m_pArray = nullptr;
        m_allocated = 0;
        return E_OUTOFMEMORY;
      }
      m_pArray = pNew;
      ZeroMemory(m_pArray+m_allocated, (alloc - m_allocated) * sizeof(T));
      m_allocated = alloc;
    }
    return hr;
  }

  HRESULT Clear()
  {
    free(m_pArray);
    m_pArray = nullptr;
    m_count = m_allocated = 0;
    return S_OK;
  }

  // SetSize: Changes the count, and grows the array if needed.
  HRESULT SetSize(DWORD count)
  {
    HRESULT hr = S_OK;
    if (count > m_allocated)
    {
      hr = Allocate(count);
    }
    if (SUCCEEDED(hr))
    {
      m_count = count;
    }
    return hr;
  }

  HRESULT Append(GrowableArray<T> *other)
  {
    return Append(other->Ptr(), other->GetCount());
  }

  HRESULT Append(const T *other, DWORD dwSize)
  {
    HRESULT hr = S_OK;
    DWORD old = GetCount();
    hr = SetSize(old + dwSize);
    if (SUCCEEDED(hr))
      memcpy(m_pArray + old, other, dwSize);

    return S_OK;
  }

  DWORD GetCount() const { return m_count; }
  DWORD GetAllocated() const { return m_allocated; }

  // Accessor.
  T& operator[](DWORD index)
  {
    assert(index < m_count);
    return m_pArray[index];
  }

  // Const accessor.
  const T& operator[](DWORD index) const
  {
    assert(index < m_count);
    return m_pArray[index];
  }

  // Return the underlying array.
  T* Ptr() { return m_pArray; }

protected:
  GrowableArray& operator=(const GrowableArray& r);
  GrowableArray(const GrowableArray &r);

  T       *m_pArray    = nullptr;
  DWORD   m_count      = 0;        // Nominal count.
  DWORD   m_allocated  = 0;        // Actual allocation size.
};