Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

/ray/src/vm/nrarray.h

Go to the documentation of this file.
00001  /*
00002  * vm/nrarray.h
00003  * 
00004  * Simple non-resizeable array for arbitrary types with default constructor. 
00005  * 
00006  * Copyright (c) 2004 by Wolfgang Wieser ] wwieser (a) gmx <*> de [ 
00007  * 
00008  * This file may be distributed and/or modified under the terms of the 
00009  * GNU General Public License version 2 as published by the Free Software 
00010  * Foundation. (See COPYING.GPL for details.)
00011  * 
00012  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00013  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00014  * 
00015  */
00016 
00017 #ifndef _VM_ALLOCONCEARRAY_H_
00018 #define _VM_ALLOCONCEARRAY_H_ 1
00019 
00030 #include <vm/vmconfig.h>    /* MUST be first */
00031 
00032 namespace VM
00033 {
00034 
00047 template<typename T,typename N=size_t>struct NonResizeableArray
00048 {
00049     private:
00050         T *array;
00051         N size;
00052         
00053         inline void _alloc()
00054             {  array = size ? new T[size] : NULL;  }
00055         inline void _free()
00056             {  DELARRAY(array);  size=0;  }
00057         inline void _copy(const NonResizeableArray &a)
00058             {  for(N i=0; i<size; i++) array[i]=a.array[i];  }
00059     public:
00061         inline NonResizeableArray() : array(NULL),size(0) {}
00063         inline NonResizeableArray(N n) : size(n)  { _alloc(); }
00065         inline NonResizeableArray(const NonResizeableArray &a) : size(a.size)
00066             {  _alloc();  _copy(a);  }
00068         inline ~NonResizeableArray()
00069             {  _free();  }
00070         
00072         NonResizeableArray &operator=(const NonResizeableArray &a)
00073         {
00074             if(size!=a.size)  {  _free();  size=a.size;  _alloc();  }
00075             _copy(a);  return(*this);
00076         }
00077         
00079         inline T &operator[](N i)  {  return(array[i]);  }
00081         inline const T &operator[](N i) const  {  return(array[i]);  }
00082         
00084         inline N n() const  {  return(size);  }
00085         
00087         inline T *ptr()  {  return(array);  }
00089         inline const T *ptr() const  {  return(array);  }
00090         
00102         inline void alloc(N n,bool do_clear=1)
00103         {
00104             if(n!=size)  {  _free();  size=n;  _alloc();  }
00105             else if(do_clear)  clear();
00106         }
00107         
00109         inline void free()  {  _free();  }
00110         
00112         inline void clear()
00113             {  for(N i=0; i<size; i++) array[i]=T();  }
00115         inline void clear(N i)
00116             {  array[i]=T();  }
00117         
00118         // Set all elements to passed value. 
00119         inline void SetAll(const T &v)
00120             {  for(N i=0; i<size; i++) array[i]=v;  }
00121 };
00122 
00123 }  // end of namespace VM
00124 
00125 #endif  /* _VM_ALLOCONCEARRAY_H_ */

Generated on Sat Feb 19 22:33:47 2005 for Ray by doxygen 1.3.5