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 #include "array.h" 00024 00025 template <class T> 00026 class csPDelArrayElementHandler 00027 { 00028 public: 00029 static void Construct (T* address, T const& src) 00030 { 00031 *address = src; 00032 } 00033 00034 static void Destroy (T* address) 00035 { 00036 delete *address; 00037 } 00038 00039 static void InitRegion (T* address, int count) 00040 { 00041 memset (address, 0, count*sizeof (T)); 00042 } 00043 }; 00044 00052 template <class T> 00053 class csPDelArray : public csArray<T*, csPDelArrayElementHandler<T*> > 00054 { 00055 private: 00056 csPDelArray (const csPDelArray&) {} 00057 csPDelArray& operator= (const csPDelArray&) { return *this; } 00058 00059 typedef csArray<T*, csPDelArrayElementHandler<T*> > superclass; 00060 00061 public: 00066 csPDelArray (int ilimit = 0, int ithreshold = 0) 00067 : csArray<T*, csPDelArrayElementHandler<T*> > (ilimit, ithreshold) 00068 { 00069 } 00070 00076 T* GetAndClear (int n) 00077 { 00078 T* ret = Get (n); 00079 InitRegion (n, 1); 00080 return ret; 00081 } 00082 00088 T* Extract (int n) 00089 { 00090 T* ret = GetAndClear (n); 00091 DeleteIndex (n); 00092 return ret; 00093 } 00094 00096 T* Pop () 00097 { 00098 CS_ASSERT (Length () > 0); 00099 T* ret = GetAndClear (Length () - 1); 00100 Truncate (Length () - 1); 00101 return ret; 00102 } 00103 00106 void SetLength (int n, T const &what) 00107 { 00108 if (n <= Length ()) 00109 { 00110 Truncate (n); 00111 } 00112 else 00113 { 00114 int old_len = Length (); 00115 superclass::SetLength (n); 00116 for (int i = old_len ; i < n ; i++) Get(i) = new T (what); 00117 } 00118 } 00119 00121 void SetLength (int n, T* const &w) 00122 { 00123 superclass::SetLength(n, w); 00124 } 00125 00127 void SetLength (int n) 00128 { 00129 superclass::SetLength(n); 00130 } 00131 }; 00132 00133 #endif // __CS_PTRARR_H__ 00134
Generated for Crystal Space by doxygen 1.2.18