DynamicArray.h++

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002    commonc++ - A C++ Common Class Library
00003    Copyright (C) 2005-2012  Mark A Lindner
00004 
00005    This file is part of commonc++.
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011 
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Library General Public License for more details.
00016 
00017    You should have received a copy of the GNU Library General Public
00018    License along with this library; if not, write to the Free
00019    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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 }; // namespace ccxx
00177 
00178 #endif // __ccxx_DynamicArray_hxx
00179 
00180 /* end of header file */
Generated on Sat Nov 26 16:49:07 2011 for libcommonc++ by  doxygen 1.6.3