MeshKit  1.0
Array.h
Go to the documentation of this file.
00001 #ifndef GFXTOOLS_ARRAY_INCLUDED // -*- C++ -*-
00002 #define GFXTOOLS_ARRAY_INCLUDED
00003 
00004 #include <cstring>
00005 
00006 template<class T>
00007 class array {
00008 protected:
00009     T *data;
00010     int len;
00011 public:
00012     array() { data=NULL; len=0; }
00013     array(int l) { init(l); }
00014     ~array() { free(); }
00015 
00016 
00017     inline void init(int l);
00018     inline void free();
00019     inline void resize(int l);
00020 
00021     inline T& ref(int i);
00022     inline T& operator[](int i) { return data[i]; }
00023     inline T& operator()(int i) { return ref(i); }
00024 
00025     inline const T& ref(int i) const;
00026     inline const T& operator[](int i) const { return data[i]; }
00027     inline const T& operator()(int i) const { return ref(i); }
00028 
00029 
00030     inline int length() const { return len; }
00031     inline int maxLength() const { return len; }
00032 };
00033 
00034 template<class T>
00035 inline void array<T>::init(int l)
00036 {
00037     data = new T[l];
00038     len = l;
00039 }
00040 
00041 template<class T>
00042 inline void array<T>::free()
00043 {
00044     if( data )
00045     {
00046         delete[] data;
00047         data = NULL;
00048     }
00049 }
00050 
00051 template<class T>
00052 inline T& array<T>::ref(int i)
00053 {
00054 #ifdef SAFETY
00055     assert( data );
00056     assert( i>=0 && i<len );
00057 #endif
00058     return data[i];
00059 }
00060 
00061 template<class T>
00062 inline const T& array<T>::ref(int i) const
00063 {
00064 #ifdef SAFETY
00065     assert( data );
00066     assert( i>=0 && i<len );
00067 #endif
00068     return data[i];
00069 }
00070 
00071 template<class T>
00072 inline void array<T>::resize(int l)
00073 {
00074     T *old = data;
00075     data = new T[l];
00076     int minl = (len<l)?len:l;
00077     data = (T *)memcpy(data,old,minl*sizeof(T));
00078     len = l;
00079     delete[] old;
00080 }
00081 
00082 // GFXTOOLS_ARRAY_INCLUDED
00083 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines