00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __CS_PTRARR_H__
00021 #define __CS_PTRARR_H__
00022
00023 #include "csutil/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