DataDirector API
dynamic_ptr_array.h
Go to the documentation of this file.
1 #ifndef _DYNAMIC_PTR_ARRAY_H
2 #define _DYNAMIC_PTR_ARRAY_H
3 
4 /*! \file dynamic_ptr_array.h
5  \brief The disti::DynamicPtrArray class. A templated array of objects pointers capable of dynamically growing.
6 
7  \par Copyright Information
8 
9  Copyright (c) 2012 The DiSTI Corporation.<br>
10  11301 Corporate Blvd; Suite 100<br>
11  Orlando, Florida 32817<br>
12  USA<br>
13  <br>
14  All rights reserved.<br>
15 
16  This Software contains proprietary trade secrets of DiSTI and may not be
17 reproduced, in whole or part, in any form, or by any means of electronic,
18 mechanical, or otherwise, without the written permission of DiSTI. Said
19 permission may be derived through the purchase of applicable DiSTI product
20 licenses which detail the distribution rights of this content and any
21 Derivative Works based on this or other copyrighted DiSTI Software.
22 
23  NO WARRANTY. THE SOFTWARE IS PROVIDED "AS-IS," WITHOUT WARRANTY OF ANY KIND,
24 AND ANY USE OF THIS SOFTWARE PRODUCT IS AT YOUR OWN RISK. TO THE MAXIMUM EXTENT
25 PERMITTED BY APPLICABLE LAW, DISTI AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES
26 AND CONDITIONS, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
27 IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY AND/OR FITNESS FOR A
28 PARTICULAR PURPOSE, TITLE, AND NON-INFRINGEMENT, WITH REGARD TO THE SOFTWARE.
29 
30  LIMITATION OF LIABILITY. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW,
31 IN NO EVENT SHALL DISTI OR ITS SUPPLIERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
32 INDIRECT, OR CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION,
33 DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS
34 INFORMATION, OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR
35 INABILITY TO USE THE SOFTWARE, EVEN IF DISTI HAS BEEN ADVISED OF THE POSSIBLITY
36 OF SUCH DAMAGES. DISTI'S ENTIRE LIABILITY AND YOUR EXCLUSIVE REMEDY SHALL NOT
37 EXCEED FIVE DOLLARS (US$5.00).
38 
39  The aforementioned terms and restrictions are governed by the laws of the
40 State of Florida and the United States of America.
41 
42 */
43 
44 
45 
46 #include "string.h"
47 #include "assert.h"
48 #include "DDD_ClassInvariant.h"
49 
50 namespace disti
51 {
52 
53 /** \brief A templated array of object pointers. The array dynamically resizes as needed. */
54 template <class T>
56 {
57  protected:
58 
59  unsigned int _size; /** Number of elements in the array */
60  unsigned int _count; /** Number of elements used */
61  T *_objects; /** Dynamic array is an array of objects */
62 
63  /** Grow the array. If nothing has been allocated yet, set the size to 2 elements. Otherwise double the array size. */
64  void GrowArray(void)
65  {
66  if (0 == _size)
67  Size(2);
68  else
69  Size(_size * 2);
70 
71  assert(( _size > 0) && (NULL != _objects));
72  }
73 
74  /** Grows the array size to the new size given. Reallocates the array as needed.
75  * Will NOT ever shrink the array size.
76  * \param newSize The new number of elements in the array
77  */
78  void Size(unsigned int newSize)
79  {
80  if (newSize > _size)
81  {
82  T *newobjs;
83  newobjs = new T[newSize];
84  assert(newobjs);
85 
86  // clear new array to NULLs
87  memset(newobjs,0,newSize*sizeof(T));
88 
89  if (_objects)
90  {
91  // copy the old array
92  memcpy(newobjs,_objects,_size*sizeof(T));
93 
94  delete [] _objects;
95  }
96 
97  _objects = newobjs;
98  _size = newSize;
99  }
100  }
101 
102  public:
103 
104  typedef T ElementType;
105 
106  /** DynamicPtrArray constructor
107  * \param The initial number of elements to be allocated for the array
108  */
109  DynamicPtrArray(int initialSize=0)
110  {
111  _size = 0;
112  _count = 0;
113  _objects = NULL;
114 
115  if (initialSize)
116  Size(initialSize);
117  }
118 
119  DynamicPtrArray(const DynamicPtrArray& other)
120  {
121  _size = 0;
122  _count = 0;
123  _objects = NULL;
124 
125  *this = other;
126  }
127 
128  /** DynamicPtrArray destructor. Clears the array but does not delete the objects being pointed to by the array
129  */
131  {
132  if (_objects)
133  {
134  delete []_objects;
135  }
136  };
137 
138  /** Returns the number of elements used of this dynamic array
139  * \return The number of elements used of the array (number of elements)
140  */
141  inline unsigned int Count() const { return _count; }
142 
143  /** Returns the current size of this dynamic array
144  * \return The current size of the array (number of elements)
145  */
146  inline unsigned int Size() const { return _size; }
147 
148  /** Attempts to insert the object into the list at the specified location. If the
149  * specified location is beyond the array bounds, the object is inserted at the next
150  * position in the array.
151  * \param obj The object to insert
152  * \param loc The index location to insert into
153  */
154  void InsertObject(const T& obj,unsigned int loc)
155  {
156  unsigned int newCount = _count+1;
157 
158  // Check to see if the new count will exceed the size, if so Grow
159  if (newCount > _size)
160  {
161  GrowArray();
162  }
163 
164  // Now increase the count
165  _count++;
166 
167  // If the Location Index exceeds the array bounds,
168  // put the new object at the end of the array
169  if (loc > _count)
170  {
171  assert(0 && "Array out of bounds");
172  }
173 
174  // Move the array elements up to make room to insert the new element
175  for (unsigned int i=_count-1; i > loc; i--)
176  _objects[i] = _objects[i-1];
177 
178  _objects[loc] = obj; // Insert the new object.
179  }
180 
181  /** Adds the specified object into the list, at the end of the list
182  * \param A pointer to the object to add
183  * \return Where the object was inserted
184  */
185  unsigned int InsertObject(const T& obj)
186  {
187  // Check to see if the new count will exceed the size, if so Grow
188  if ((_count+1) > _size)
189  GrowArray();
190 
191  _objects[_count] = obj; // Insert the new object.
192 
193  // Now increase the count
194  _count++;
195 
196  return _count;
197  }
198 
199  /** Adds the specified object into the list, at the head of the list
200  * Treats the list like a stack, hence the name Push
201  * \param A pointer to the object to add
202  */
203  void PushObject(const T& obj)
204  {
205  InsertObject(obj,0);
206  }
207 
208  /** Insert the object into the list at the specified location
209  * \param obj The object to insert
210  * \param loc The index location to insert into
211  */
212  void InsertObjectAfter(const T& obj,unsigned int loc)
213  {
214 
215  InsertObject(obj,loc+1);
216  }
217 
218  /** Deletes the specified object from the list. Does not free it
219  * \param A pointer to the object to delete
220  * \return true if object was found and deleted else false
221  */
222  bool DeleteObject(const T& obj)
223  {
224  bool rVal = false;
225 
226  int loc = Position(obj);
227 
228  if (loc >= 0)
229  rVal = DeleteObjectAtIndex(loc);
230 
231  return rVal;
232  }
233 
234  /** Deletes the object at the specified index. Does not free it
235  * \param Index of the object to delete
236  * \return true if object was found and deleted else false
237  */
238  bool DeleteObjectAtIndex(unsigned int index)
239  {
240  if (index >= _count)
241  {
242  return false;
243  }
244  else
245  {
246  for (unsigned int j=index; j < (_count - 1u); j++)
247  _objects[j] = _objects[j+1u];
248 
249  _count--;
250  }
251 
252  return true;
253  }
254 
255  /** Deletes the object at the specified index and frees it
256  * \param Index of the object to delete
257  * \return true if object was found and deleted else false
258  */
259  bool RemoveAndDeallocateObjectAtIndex(unsigned int index)
260  {
261  if (index >= _count)
262  {
263  return false;
264  }
265  else
266  {
267  if (_objects[index])
268  delete _objects[index];
269 
270  for (unsigned int j=index; j < (_count - 1u); j++)
271  _objects[j] = _objects[j+1u];
272 
273  _count--;
274  }
275 
276  return true;
277  }
278 
279  /** Get the index position of the specified object in the list
280  * \param obj The object to find
281  * \return The index of the object in the list, -1 if it isn't in the list
282  */
283  int Position(const T& obj)
284  {
285  for (unsigned int i=0;i < _count; i++)
286  if (obj==_objects[i])
287  return i;
288  return -1;
289  }
290 
291  /** Remove the specified object from the array and then call delete on it
292  * \param obj The pointer to the object to remove
293  */
294  bool RemoveAndDeallocate(T &obj)
295  {
296  int i = Position(obj);
297 
298  if (i >= 0)
299  {
300  delete _objects[i]; // delete the object
301  DeleteObjectAtIndex(i); // remove it from the array
302 
303  }
304  return (i >= 0);
305  }
306 
307  /** Swap the objects at index a and b
308  * \param a Index of first object
309  * \param b Index of second object
310  */
311  void Swap(unsigned int a,unsigned int b)
312  {
313  T tmp;
314  tmp = _objects[a];
315  _objects[a] = _objects[b];
316  _objects[b] = tmp;
317  }
318 
319  /** Empties the list, calling delete on all of its elements
320  * This method is intended for pointer data types only.
321  */
322  void EmptyList(void)
323  {
324  for (unsigned int i=0; i < Count(); i++)
325  if (_objects[i])
326  delete _objects[i];
327 
328  ClearList();
329  }
330 
331  /** Clears the list so Count() is zero. Does not attempt to delete the objects data */
332  void ClearList(void)
333  {
334  // Clear object array
335  if (_objects)
336  memset(_objects,0,sizeof(T) * _size);
337 
338  _count = 0;
339  }
340 
341  /** Overload of array operator to get element at desired index */
342  T &operator[] (int index)
343  {
344  assert( (index >= 0) && (index < (int)_count) && (index < (int)_size)
345  && "Array out of bounds"); //ensure index is within bounds
346 
347  // This gets an element.
348  return _objects[index];
349  }
350 
351  /** Overload of array operator to get element at desired index - const safe version*/
352  const T &operator[] (int index) const
353  {
354  assert( (index >= 0) && (index < (int)_count) && (index < (int)_size)
355  && "Array out of bounds"); //ensure index is within bounds
356 
357  // This gets an element.
358  return _objects[index];
359  }
360 
361  /** Return the internal array pointer
362  * If you call any other methods on this object it invalidates the pointer.*/
363  inline T* InternalArray(void)
364  {
365  return _objects;
366  }
367 
368  /** Overload the assignment operator to create a duplicate of the righthand operand */
370  {
371  Size(right.Size());
372  for(unsigned int i = 0; i <Size(); i++)
373  {
374  _objects[i] = right._objects[i];
375  }
376  _count = right.Count();
377 
378  return *this;
379  }
380 
381  /** \return true if the list is empty */
382  bool IsEmpty(void) const
383  {
384 
385  return (_count==0);
386  }
387 };
388 
389 } // namespace disti
390 
391 #endif
392 
void Swap(unsigned int a, unsigned int b)
Definition: dynamic_ptr_array.h:311
A templated array of object pointers. The array dynamically resizes as needed.
Definition: dynamic_ptr_array.h:55
void InsertObject(const T &obj, unsigned int loc)
Definition: dynamic_ptr_array.h:154
unsigned int Size() const
Definition: dynamic_ptr_array.h:146
unsigned int _count
Definition: dynamic_ptr_array.h:60
This header defines a DDD_DEBUG only macro for facilitating evaluating class invariants in the Data D...
DynamicPtrArray(int initialSize=0)
Definition: dynamic_ptr_array.h:109
~DynamicPtrArray(void)
Definition: dynamic_ptr_array.h:130
bool RemoveAndDeallocateObjectAtIndex(unsigned int index)
Definition: dynamic_ptr_array.h:259
void Size(unsigned int newSize)
Definition: dynamic_ptr_array.h:78
void PushObject(const T &obj)
Definition: dynamic_ptr_array.h:203
void InsertObjectAfter(const T &obj, unsigned int loc)
Definition: dynamic_ptr_array.h:212
void GrowArray(void)
Definition: dynamic_ptr_array.h:64
T * InternalArray(void)
Definition: dynamic_ptr_array.h:363
int Position(const T &obj)
Definition: dynamic_ptr_array.h:283
bool RemoveAndDeallocate(T &obj)
Definition: dynamic_ptr_array.h:294
T * _objects
Definition: dynamic_ptr_array.h:61
unsigned int Count() const
Definition: dynamic_ptr_array.h:141
bool DeleteObjectAtIndex(unsigned int index)
Definition: dynamic_ptr_array.h:238
bool IsEmpty(void) const
Definition: dynamic_ptr_array.h:382
void ClearList(void)
Definition: dynamic_ptr_array.h:332
DynamicPtrArray< T > & operator=(const DynamicPtrArray< T > &right)
Definition: dynamic_ptr_array.h:369
unsigned int InsertObject(const T &obj)
Definition: dynamic_ptr_array.h:185
void EmptyList(void)
Definition: dynamic_ptr_array.h:322
Definition: AttributeChangedEmitter.h:46
T & operator[](int index)
Definition: dynamic_ptr_array.h:342
bool DeleteObject(const T &obj)
Definition: dynamic_ptr_array.h:222