DynamicArray.h++
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef __ccxx_DynamicArray_hxx
00024 #define __ccxx_DynamicArray_hxx
00025
00026 #include <commonc++/Common.h++>
00027 #include <commonc++/OutOfBoundsException.h++>
00028
00029 namespace ccxx {
00030
00038 template <typename T> class DynamicArray
00039 {
00040 public:
00041
00042 typedef T value_type;
00043 typedef T* iterator;
00044 typedef const T* const_iterator;
00045 typedef T& reference;
00046 typedef const T& const_reference;
00047
00052 DynamicArray(size_t size)
00053 : _size(0),
00054 _data(NULL)
00055 {
00056 resize(size, false);
00057 }
00058
00060 ~DynamicArray() throw()
00061 {
00062 delete[] _data;
00063 }
00064
00071 void resize(size_t size, bool copy = false)
00072 {
00073 if(size != _size)
00074 {
00075 T *old = _data;
00076 _size = size;
00077 _data = (_size > 0) ? new T[_size] : NULL;
00078
00079 if(old)
00080 {
00081 if(copy)
00082 std::memcpy(reinterpret_cast<void *>(_data),
00083 reinterpret_cast<void *>(old),
00084 _size * sizeof(T));
00085 delete[] old;
00086 }
00087 }
00088 }
00089
00094 T *release() throw()
00095 {
00096 T *data = _data;
00097 _data = NULL;
00098 _size = 0;
00099
00100 return(data);
00101 }
00102
00104 bool isNull() const throw()
00105 { return(_data == NULL); }
00106
00111 T *data() throw()
00112 { return(_data); }
00113
00118 const T *data() const throw()
00119 { return(_data); }
00120
00125 size_t size() const throw()
00126 { return(_size); }
00127
00129 T& operator[](int index) throw(OutOfBoundsException)
00130 {
00131 if((index < 0) || (index >= static_cast<int>(_size)))
00132 throw OutOfBoundsException();
00133
00134 return(_data[index]);
00135 }
00136
00138 T operator[](int index) const throw(OutOfBoundsException)
00139 {
00140 if((index < 0) || (index >= static_cast<int>(_size)))
00141 throw OutOfBoundsException();
00142
00143 return(_data[index]);
00144 }
00145
00147 operator T*() throw()
00148 { return(_data); }
00149
00151 operator const T*() const throw()
00152 { return(_data); }
00153
00155 bool operator!() const throw()
00156 { return(isNull()); }
00157
00158 iterator begin() throw()
00159 { return(_data); }
00160
00161 const_iterator begin() const throw()
00162 { return(_data); }
00163
00164 iterator end() throw()
00165 { return(_data + _size); }
00166
00167 const_iterator end() const throw()
00168 { return(_data + _size); }
00169
00170 private:
00171
00172 size_t _size;
00173 T *_data;
00174 };
00175
00176 };
00177
00178 #endif // __ccxx_DynamicArray_hxx
00179
00180