libcommonc++  0.7
DynamicArray.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_DynamicArray_hxx
24 #define __ccxx_DynamicArray_hxx
25 
26 #include <commonc++/Common.h++>
28 
29 #include <cstring>
30 
31 namespace ccxx {
32 
41 template <typename T> class DynamicArray
42 {
43  public:
44 
45  typedef T value_type;
46  typedef T* iterator;
47  typedef const T* const_iterator;
48  typedef T& reference;
49  typedef const T& const_reference;
50 
57  : _size(0),
58  _data(NULL)
59  {
60  resize(size, false);
61  }
62 
65  {
66  delete[] _data;
67  }
68 
76  void resize(uint_t size, bool copy = false)
77  {
78  if(size != _size)
79  {
80  T *old = _data;
81  _size = size;
82  _data = (_size > 0) ? new T[_size] : NULL;
83 
84  if(old)
85  {
86  if(copy)
87  std::memcpy(reinterpret_cast<void *>(_data),
88  reinterpret_cast<void *>(old),
89  _size * sizeof(T));
90  delete[] old;
91  }
92  }
93  }
94 
100  T* release()
101  {
102  T* data = _data;
103  _data = NULL;
104  _size = 0;
105 
106  return(data);
107  }
108 
110  bool isNull() const
111  { return(_data == NULL); }
112 
118  T* data()
119  { return(_data); }
120 
126  const T* data() const
127  { return(_data); }
128 
134  uint_t size() const
135  { return(_size); }
136 
142  T& operator[](int index)
143  {
144  if((index < 0) || (index >= static_cast<int>(_size)))
145  throw OutOfBoundsException();
146 
147  return(_data[index]);
148  }
149 
154  T operator[](int index) const
155  {
156  if((index < 0) || (index >= static_cast<int>(_size)))
157  throw OutOfBoundsException();
158 
159  return(_data[index]);
160  }
161 
163  operator T*()
164  { return(_data); }
165 
167  operator const T*() const
168  { return(_data); }
169 
171  bool operator!() const
172  { return(isNull()); }
173 
174  iterator begin()
175  { return(_data); }
176 
177  const_iterator begin() const
178  { return(_data); }
179 
180  iterator end()
181  { return(_data + _size); }
182 
183  const_iterator end() const
184  { return(_data + _size); }
185 
186  private:
187 
188  uint_t _size;
189  T* _data;
190 };
191 
192 } // namespace ccxx
193 
194 #endif // __ccxx_DynamicArray_hxx
T * iterator
Definition: DynamicArray.h++:46
const T & const_reference
Definition: DynamicArray.h++:49
const_iterator begin() const
Definition: DynamicArray.h++:177
const T * const_iterator
Definition: DynamicArray.h++:47
A simple object wrapper for dynamically-allocated arrays.
Definition: DynamicArray.h++:41
T * data()
Get the underlying array.
Definition: DynamicArray.h++:118
~DynamicArray()
Destructor.
Definition: DynamicArray.h++:64
unsigned int uint_t
An alias for unsigned int.
Definition: Integers.h++:74
const T * data() const
Get the underlying array.
Definition: DynamicArray.h++:126
bool isNull() const
Determine if this array is NULL.
Definition: DynamicArray.h++:110
DynamicArray(uint_t size)
Construct a new DynamicArray with the given initial size.
Definition: DynamicArray.h++:56
T & operator[](int index)
Index operator.
Definition: DynamicArray.h++:142
const_iterator end() const
Definition: DynamicArray.h++:183
void resize(uint_t size, bool copy=false)
Resize the array to a new size.
Definition: DynamicArray.h++:76
T & reference
Definition: DynamicArray.h++:48
T value_type
Definition: DynamicArray.h++:45
uint_t size() const
Get the size of the array.
Definition: DynamicArray.h++:134
An exception indicating that an attempted operation would result in an out-of-range array index acces...
Definition: OutOfBoundsException.h++:38
bool operator!() const
Unary NOT operator.
Definition: DynamicArray.h++:171
T operator[](int index) const
Index operator.
Definition: DynamicArray.h++:154
T * release()
Release ownership of the array.
Definition: DynamicArray.h++:100
iterator end()
Definition: DynamicArray.h++:180
Definition: AllocationMap.c++:25
iterator begin()
Definition: DynamicArray.h++:174