csutil/parray.h
00001 /* 00002 Crystal Space Pointer Array 00003 Copyright (C) 2003 by Jorrit Tyberghein 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public 00016 License along with this library; if not, write to the Free 00017 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00018 */ 00019 00020 #ifndef __CS_PTRARR_H__ 00021 #define __CS_PTRARR_H__ 00022 00023 //----------------------------------------------------------------------------- 00024 // Note *1*: The explicit "this->" is needed by modern compilers (such as gcc 00025 // 3.4.x) which distinguish between dependent and non-dependent names in 00026 // templates. See: http://gcc.gnu.org/onlinedocs/gcc/Name-lookup.html 00027 //----------------------------------------------------------------------------- 00028 00029 #include "csextern.h" 00030 #include "array.h" 00031 00032 template <class T> 00033 class csPDelArrayElementHandler 00034 { 00035 public: 00036 static void Construct (T* address, T const& src) 00037 { 00038 *address = src; 00039 } 00040 00041 static void Destroy (T* address) 00042 { 00043 delete *address; 00044 } 00045 00046 static void InitRegion (T* address, int count) 00047 { 00048 memset (address, 0, count*sizeof (T)); 00049 } 00050 }; 00051 00059 template <class T> 00060 class csPDelArray : public csArray<T*, csPDelArrayElementHandler<T*> > 00061 { 00062 typedef csArray<T*, csPDelArrayElementHandler<T*> > superclass; 00063 00064 private: 00065 csPDelArray (const csPDelArray&); // Illegal; unimplemented. 00066 csPDelArray& operator= (const csPDelArray&); // Illegal; unimplemented. 00067 00068 public: 00073 csPDelArray (int ilimit = 0, int ithreshold = 0) : 00074 csArray<T*, csPDelArrayElementHandler<T*> > (ilimit, ithreshold) {} 00075 00081 T* GetAndClear (int n) 00082 { 00083 T* ret = this->Get (n); // see *1* 00084 this->InitRegion (n, 1); 00085 return ret; 00086 } 00087 00093 T* Extract (int n) 00094 { 00095 T* ret = GetAndClear (n); 00096 this->DeleteIndex (n); // see *1* 00097 return ret; 00098 } 00099 00101 T* Pop () 00102 { 00103 CS_ASSERT (this->Length () > 0); 00104 T* ret = GetAndClear (this->Length () - 1); // see *1* 00105 Truncate (this->Length () - 1); 00106 return ret; 00107 } 00108 00111 void SetLength (int n, T const &what) 00112 { 00113 if (n <= this->Length ()) // see *1* 00114 { 00115 this->Truncate (n); 00116 } 00117 else 00118 { 00119 int old_len = this->Length (); // see *1* 00120 superclass::SetLength (n); 00121 for (int i = old_len ; i < n ; i++) this->Get(i) = new T (what); 00122 } 00123 } 00124 00126 void SetLength (int n, T* const &w) 00127 { 00128 superclass::SetLength(n, w); 00129 } 00130 00132 void SetLength (int n) 00133 { 00134 superclass::SetLength(n); 00135 } 00136 }; 00137 00138 #endif // __CS_PTRARR_H__ 00139
Generated for Crystal Space by doxygen 1.2.18