Go to the documentation of this file.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
00027
00028
00029
00030
00031
00032
00033 #include "csextern.h"
00034 #include "csutil/array.h"
00035
00036 template <class T>
00037 class csPDelArrayElementHandler : public csArrayElementHandler<T>
00038 {
00039 public:
00040 static void Construct (T* address, T const& src)
00041 {
00042 *address = src;
00043 }
00044
00045 static void Destroy (T* address)
00046 {
00047 delete *address;
00048 }
00049
00050 static void InitRegion (T* address, size_t count)
00051 {
00052 memset (address, 0, count*sizeof (T));
00053 }
00054 };
00055
00063 template <class T,
00064 class MemoryAllocator = CS::Container::ArrayAllocDefault,
00065 class CapacityHandler = csArrayCapacityFixedGrow<16> >
00066 class csPDelArray :
00067 public csArray<T*, csPDelArrayElementHandler<T*>, MemoryAllocator,
00068 CapacityHandler>
00069 {
00070 typedef csArray<T*, csPDelArrayElementHandler<T*>, MemoryAllocator,
00071 CapacityHandler> superclass;
00072
00073 private:
00074 csPDelArray (const csPDelArray&);
00075 csPDelArray& operator= (const csPDelArray&);
00076
00077 public:
00082 csPDelArray (size_t ilimit = 0,
00083 const CapacityHandler& ch = CapacityHandler()) :
00084 superclass (ilimit, ch) {}
00085
00091 T* GetAndClear (size_t n)
00092 {
00093 T* ret = this->Get (n);
00094 this->InitRegion (n, 1);
00095 return ret;
00096 }
00097
00103 T* Extract (size_t n)
00104 {
00105 T* ret = GetAndClear (n);
00106 this->DeleteIndex (n);
00107 return ret;
00108 }
00109
00111 T* Pop ()
00112 {
00113 CS_ASSERT (this->GetSize () > 0);
00114 T* ret = GetAndClear (this->GetSize () - 1);
00115 Truncate (this->GetSize () - 1);
00116 return ret;
00117 }
00118
00126 void SetSize (size_t n)
00127 {
00128 superclass::SetSize (n, 0);
00129 }
00130
00135 void SetSize (size_t n, T const &what)
00136 {
00137 if (n <= this->GetSize ())
00138 {
00139 this->Truncate (n);
00140 }
00141 else
00142 {
00143 size_t old_len = this->GetSize ();
00144 superclass::SetSize (n);
00145 for (size_t i = old_len ; i < n ; i++) this->Get(i) = new T (what);
00146 }
00147 }
00148 };
00149
00150 #endif // __CS_PTRARR_H__
00151