ScummVM API documentation
px_array.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * Additional copyright for this file:
8  * Copyright (C) 1999-2000 Revolution Software Ltd.
9  * This code is based on source code created by Revolution Software,
10  * used with permission.
11  *
12  * This program is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see <http://www.gnu.org/licenses/>.
24  *
25  */
26 
27 #ifndef ICB_LIBRARY_CMYACTARRAY
28 #define ICB_LIBRARY_CMYACTARRAY
29 
30 #include "engines/icb/common/px_rcutypes.h"
31 
32 namespace ICB {
33 
34 #define MY_TEMPLATE template <class Type>
35 #define T_MYACTARRAY rcActArray<Type>
36 #define T_MYPTRARRAY rcAutoPtrArray<Type>
37 
38 MY_TEMPLATE class rcActArray {
39 public:
40  rcActArray() { // Construct an empty array
41  m_userPosition = m_allocatedSize = 0;
42  }
43 
45  m_userPosition = m_allocatedSize = 0;
46  (*this) = a;
47  }
48 
49  ~rcActArray(); // Destruct the array
50 
51  void operator=(const rcActArray &);
52 
53  // Member access functions
54  uint32 GetNoItems() const { return (m_userPosition); }
55 
56  uint32 Add(const Type &f); // Add an item.
57 
58  Type &operator[](uint32); // Give access to an entry
59  const Type &operator[](uint32 i) const;
60 
61  void SetSize(uint32 n) { ResizeArray(n); }
62  void Reset();
63 
64 private:
65  uint32 m_userPosition; // Next place to add an item to
66  uint32 m_allocatedSize; // How many items have been allocated
67 
68  Type **m_contents; // A pointer to pointers to the objects
69 
70  void ResizeArray(uint32); // Change the size of the array
71 };
72 
73 MY_TEMPLATE
74 void T_MYACTARRAY::operator=(const rcActArray &a) {
75  if (m_allocatedSize)
76  delete[] m_contents;
77  m_userPosition = a.m_userPosition;
78  m_allocatedSize = a.m_allocatedSize;
79 
80  if (m_allocatedSize)
81 
82  {
83  m_contents = new Type *[m_allocatedSize];
84  for (uint32 count = 0; count < m_allocatedSize; count++)
85  m_contents[count] = new Type(*(a.m_contents[count]));
86  }
87 }
88 
89 MY_TEMPLATE
90 Type &T_MYACTARRAY::operator[](uint32 n) {
91  if (n >= m_userPosition) {
92  ResizeArray(n);
93  m_userPosition = n + 1;
94  }
95  return (*(m_contents[n]));
96 }
97 
98 MY_TEMPLATE
99 const Type &T_MYACTARRAY::operator[](uint32 n) const {
100  // It is permissible to look at an element that has not been defined, as the constructor assures
101  // that the contents are valid
102  if (n >= m_userPosition) {
103  // Remove any 'constness' for a resize
104  (const_cast<rcActArray<Type> *>(this))->ResizeArray(n);
105  (const_cast<rcActArray<Type> *>(this))->m_userPosition = n + 1;
106  }
107 
108  return (*(m_contents[n]));
109 }
110 
111 MY_TEMPLATE T_MYACTARRAY::~rcActArray() { Reset(); }
112 
113 MY_TEMPLATE void T_MYACTARRAY::Reset() {
114  for (uint32 count = 0; count < m_allocatedSize; count++)
115  delete m_contents[count];
116 
117  if (m_allocatedSize)
118  delete[] m_contents;
119  m_allocatedSize = 0;
120  m_userPosition = 0;
121 }
122 
123 MY_TEMPLATE void T_MYACTARRAY::ResizeArray(uint32 n2) {
124  // if n is still within the allocated area then just set the last position
125  if (n2 >= m_allocatedSize) {
126  // Make sure we are going to make the thing big enough
127  uint32 nextSize = m_allocatedSize ? m_allocatedSize + m_allocatedSize : 1; // Double, or 1 if now 0
128  while (nextSize <= n2)
129  nextSize += nextSize;
130 
131  // Get a New pointer array of the correct size
132  Type **newArray = new Type *[nextSize];
133  if (m_allocatedSize > 0) {
134  // Copy in the old stuff
135  memcpy((unsigned char *)newArray, (unsigned char *)m_contents, m_allocatedSize * sizeof(Type *));
136  }
137  // Put empty objects in the newly allocated space
138  for (uint32 newObjects = m_allocatedSize; newObjects < nextSize; newObjects++)
139  newArray[newObjects] = new Type;
140  // Remove any old stuff
141  if (m_allocatedSize)
142  delete[] m_contents;
143  m_contents = newArray;
144  m_allocatedSize = nextSize;
145  }
146 }
147 
148 MY_TEMPLATE uint32 T_MYACTARRAY::Add(const Type &f) {
149  operator[](m_userPosition) = f;
150  return (m_userPosition - 1);
151 }
152 
153 MY_TEMPLATE class rcAutoPtrArray {
154  uint32 m_noContents; // How many entries have been allocated
155  uint32 m_userPosition; // Next position for the Add command
156 
157  Type **m_contents; // A pointer to pointers to the objects
158 
159  void ResizeArray(uint32); // Change the size of the array
160 public:
161  explicit rcAutoPtrArray() { // Construct an empty array
162  m_noContents = m_userPosition = 0;
163  }
164  ~rcAutoPtrArray(); // Destruct the array
165 
166  // Member access functions
167  uint32 GetNoItems() const { return (m_userPosition); }
168 
169  uint32 Add(Type *f) {
170  operator[](m_userPosition) = f;
171  return (m_userPosition - 1);
172  }
173 
174  Type *&operator[](uint32); // Give access to an entry
175  const Type *&operator[](uint32) const; // Give access to an entry
176 
177  void Reset();
178  void RemoveAndShuffle(uint32); // Remove an object from the array
179  void SetSize(uint32 n) { ResizeArray(n); }
180 
181  // Super dangerous, but faster, access to the array
182  Type *GetRawArray() { return (*m_contents); }
183 
184 private: // Prevent use of the PtrArray copy constructor
185  // The default copy constructor should never be called
186  rcAutoPtrArray(const rcAutoPtrArray &) {}
187  void operator=(const rcAutoPtrArray &) {}
188 };
189 
190 MY_TEMPLATE
191 Type *&T_MYPTRARRAY::operator[](uint32 n) {
192  if (n >= m_userPosition) {
193  ResizeArray(n);
194  m_userPosition = n + 1;
195  }
196  return (m_contents[n]);
197 }
198 
199 MY_TEMPLATE
200 const Type *&T_MYPTRARRAY::operator[](uint32 n) const {
201  // It is permissible to look at an element that has not been defined, as it will be defined as NULL
202  if (n >= m_userPosition) {
203  (const_cast<rcAutoPtrArray<Type> *>(this))->ResizeArray(n);
204  (const_cast<rcAutoPtrArray<Type> *>(this))->m_userPosition = n + 1;
205  }
206 
207  return const_cast<const Type *&>(m_contents[n]);
208 }
209 
210 MY_TEMPLATE T_MYPTRARRAY::~rcAutoPtrArray() { Reset(); }
211 
212 MY_TEMPLATE void T_MYPTRARRAY::Reset() {
213  // The pointer array maintains responsibility for deleting any contents
214  for (uint32 count = 0; count < m_userPosition; count++)
215  if (m_contents[count])
216  delete m_contents[count];
217  if (m_noContents)
218  delete[] m_contents;
219  m_noContents = m_userPosition = 0;
220 }
221 
222 MY_TEMPLATE void T_MYPTRARRAY::ResizeArray(uint32 n2) {
223  if (n2 >= m_noContents) {
224  // Double the allocation value
225  uint32 nextSize = m_noContents > 0 ? m_noContents + m_noContents : 1;
226  while (n2 >= nextSize)
227  nextSize = nextSize + nextSize;
228  // Get a New pointer array of the correct size
229  Type **newArray = new Type *[nextSize];
230  // Copy in the old stuff, if there is any
231  if (m_noContents > 0)
232  memcpy((unsigned char *)newArray, (unsigned char *)m_contents, m_noContents * sizeof(Type *));
233  // Reset the New entries
234  memset((unsigned char *)(newArray + m_noContents), 0, (nextSize - m_noContents) * sizeof(Type *));
235  // Remove any old stuff
236  if (m_noContents)
237  delete[] m_contents;
238  m_contents = newArray;
239  m_noContents = nextSize;
240  }
241 }
242 
243 MY_TEMPLATE void T_MYPTRARRAY::RemoveAndShuffle(uint32 n) {
244  // Remove an object from the array
245 
246  // First delete it
247  if (m_contents[n])
248  delete m_contents[n];
249  // and shuffle the array
250  memcpy(m_contents + n, m_contents + n + 1, (m_noContents - n - 1) * sizeof(Type *));
251 }
252 
253 template <class Type> class rcIntArray {
254  uint32 m_noContents; // How many entries there are
255  uint32 m_userPosition; // Where the next add position goes
256  Type *m_contents;
257 
258  void ResizeArray(uint32); // Change the size of the array
259 
260 public:
261  explicit rcIntArray() { // Construct an empty array
262  m_noContents = m_userPosition = 0;
263  }
264  ~rcIntArray() { // Destruct the array
265  if (m_noContents)
266  delete[] m_contents;
267  }
268 
269  // Copy constructor
270  rcIntArray(const rcIntArray &a) {
271  m_noContents = m_userPosition = 0;
272  (*this) = a;
273  }
274 
275  // Constructor with an initial size
276  rcIntArray(uint32 initialSize) { ResizeArray(initialSize); }
277 
278  const rcIntArray &operator=(const rcIntArray &);
279 
280  // Member access functions
281  uint32 GetNoItems() const { return (m_userPosition); }
282 
283  uint32 Add(Type f); // Add an integer. Only makes sense if the resize step is one
284 
285  Type &operator[](uint32); // Give access to an entry
286  const Type operator[](uint32) const; // Give access to an entry
287 
288  void Reset();
289  void SetSize(uint32 n) { ResizeArray(n); }
290 
291  Type *GetRawArray() { return (m_contents); }
292 };
293 
294 template <class Type> Type &rcIntArray<Type>::operator[](uint32 index) {
295  if (index >= m_userPosition) {
296  ResizeArray(index);
297  m_userPosition = index + 1;
298  }
299  return m_contents[index];
300 }
301 
302 // This version of [] allows the array to be part of a const function
303 template <class Type> const Type rcIntArray<Type>::operator[](uint32 index) const {
304  // It is permissible to look at an element that has not been defined, as it will have been set to 0
305  if (index >= m_userPosition) {
306  // Remove any 'constness' for a resize
307  (const_cast<rcIntArray<Type> *>(this))->ResizeArray(index);
308  (const_cast<rcIntArray<Type> *>(this))->m_userPosition = index + 1;
309  }
310 
311  return m_contents[index];
312 }
313 
314 template <class IntType> void rcIntArray<IntType>::ResizeArray(uint32 accessedSize) {
315  // Check if we need to do any reallocating
316  if (accessedSize >= m_noContents) {
317  uint32 newSize = m_noContents > 0 ? m_noContents * 2 : 1;
318  while (newSize <= accessedSize)
319  newSize = newSize + newSize;
320 
321  IntType *newArray = new IntType[newSize];
322  if (m_noContents)
323  memcpy(newArray, m_contents, m_noContents * sizeof(IntType));
324  // Call me a fool, but I like my integers initialised to 0
325  memset(newArray + m_noContents, 0, (newSize - m_noContents) * sizeof(IntType));
326 
327  if (m_noContents)
328  delete[] m_contents;
329  m_contents = newArray;
330  m_noContents = newSize;
331  }
332 }
333 
334 template <class IntType> const rcIntArray<IntType> &rcIntArray<IntType>::operator=(const rcIntArray<IntType> &obOpB) {
335  uint32 nCount;
336 
337  if (m_noContents)
338  delete[] m_contents;
339  m_userPosition = obOpB.m_userPosition;
340  m_noContents = obOpB.m_noContents;
341 
342  if (m_noContents) {
343  m_contents = new IntType[m_noContents];
344  for (nCount = 0; nCount < m_noContents; nCount++)
345  m_contents[nCount] = obOpB.m_contents[nCount];
346  }
347 
348  return *this;
349 }
350 
351 template <class Type> void rcIntArray<Type>::Reset() {
352  // CLear out the array
353  if (m_noContents) {
354  delete[] m_contents;
355  m_noContents = m_userPosition = 0;
356  }
357 }
358 
359 template <class Type> uint32 rcIntArray<Type>::Add(Type f) {
360  // Add an integer. Only makes sense if the resize step is one
361  operator[](m_userPosition) = f;
362  return (m_userPosition - 1);
363 }
364 
365 } // End of namespace ICB
366 
367 #endif // ndef _LIBRARY_CMYACTARRAY
Definition: px_array.h:253
Definition: actor.h:32
Type
Definition: log.h:33
Definition: px_array.h:153
Definition: px_array.h:38