autoarray.h

Go to the documentation of this file.
00001 /*
00002  *  Copyright (C) 2000-2001  The Exult Team
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 #include <exception>
00020 #include <string>
00021 
00022 #include "exult_types.h"
00023 
00024 template<class T>
00025 class autoarray
00026   {
00027 private:
00028   std::size_t size_;
00029   T *data_;
00030 public:
00031 #ifdef HAVE_NO_EXCEPTIONS
00032   inline static range_error(const std::string& what_arg) {
00033     std::cerr << "Range Error: " << what_arg << std::endl;
00034 #ifdef DEBUG
00035     *((int *)(0)) = 0;
00036 #else
00037     std::exit(-1);
00038 #endif
00039   }
00040 #else
00041   class range_error : public std::exception
00042     {
00043     std::string what_;
00044     public:
00045      range_error (const std::string& what_arg): what_ (what_arg) { }
00046      const char *what(void) const throw () { return what_.c_str(); }
00047      virtual ~range_error() throw () { }
00048     };
00049 #endif
00050   autoarray() : size_(0), data_(0) 
00051     {  }
00052   autoarray(std::size_t n) : size_(n),data_(n?new T[n]:0)
00053     {  }
00054 #ifdef HAVE_NO_EXCEPTIONS
00055   T &operator[](sint32 i)
00056 #else
00057   T &operator[](sint32 i)  throw(range_error)
00058 #endif
00059     {
00060     if(i>=(sint32)size_ || i < 0)
00061       throw range_error("out of bounds");
00062     if(data_)
00063       return data_[i];
00064     throw range_error("no data");
00065     }
00066   ~autoarray()
00067     {
00068     if(data_)
00069       delete [] data_;
00070     }
00071   autoarray(const autoarray &a) : size_(0),data_(0)
00072     {
00073     if(a.data_)
00074       {
00075       data_=new T[a.size_];
00076       memcpy(data_,a.data_,a.size_);
00077       size_=a.size_;
00078       }
00079     }
00080   autoarray &operator=(const autoarray &a)
00081     {
00082     if(data_)
00083       {
00084       delete [] data_;
00085       size_=0;
00086       }
00087     if(a.data_)
00088       {
00089       data_=new T[a.size_];
00090       memcpy(data_,a.data_,a.size_);
00091       size_=a.size_;
00092       }
00093     return *this;
00094     }
00095   void set_size(std::size_t new_size)
00096     {
00097     if(data_)
00098       {
00099       delete [] data_;
00100       }
00101     data_=new T[new_size];
00102     size_=new_size;
00103     }
00104   };
00105 

Generated on Mon Jul 9 14:42:44 2007 for ExultEngine by  doxygen 1.5.1