00001 #ifndef __SIZEABLE_ARRAY_H
00002 #define __SIZEABLE_ARRAY_H
00003
00004
00005 #include "GXStandardDefines.h"
00006 #include "GXAutoArray.h"
00007
00008
00009 template<typename type>
00010 class CGXSizeableArray
00011 {
00012 public:
00013 CGXSizeableArray()
00014 {
00015 m_uGrowAmt=10;
00016 m_pInitCallback=NULL;
00017 }
00018
00019 inline operator type* ()
00020 {
00021 return m_Array;
00022 }
00023
00024
00025
00026 void Cleanup()
00027 {
00028 m_Array.Cleanup();
00029 }
00030
00031 UINT GetElementCount()
00032 {
00033 return m_Array.GetCount();
00034 }
00035
00036
00037 type GetElementAt(UINT uIndex)
00038 {
00039 assert(uIndex<m_Array.GetCount());
00040
00041 return m_Array.GetPtr()[uIndex];
00042 }
00043
00044 bool Grow(UINT uMaxElementCount)
00045 {
00046 CGXAutoArray<type> TempArray;
00047
00048 bool bGrowing=(uMaxElementCount>m_Array.GetCount());
00049
00050 if(!TempArray.Init(m_Array.GetCount()))
00051 {
00052 return false;
00053 }
00054
00055 if(!TempArray.CopyIn(m_Array.GetPtr(), m_Array.GetCount()))
00056 {
00057 return false;
00058 }
00059
00060 if(!m_Array.Init(uMaxElementCount))
00061 {
00062 return false;
00063 }
00064
00065 if(m_pInitCallback)
00066 {
00067 for(UINT uCtr=0;uCtr<uMaxElementCount;uCtr++)
00068 {
00069 m_pInitCallback(&(m_Array.GetPtr()[uCtr]));
00070 }
00071 }
00072
00073 if(bGrowing)
00074 {
00075 if(!m_Array.CopyIn(TempArray.GetPtr(), TempArray.GetCount()))
00076 {
00077 return false;
00078 }
00079 }
00080 else
00081 {
00082 if(!m_Array.CopyIn(TempArray.GetPtr(), m_Array.GetCount()))
00083 {
00084 return false;
00085 }
00086 }
00087
00088 return true;
00089 }
00090
00091
00092 bool InsertElement(UINT uIndex, type& Element)
00093 {
00094 if(uIndex>=GetElementCount())
00095 {
00096 if(!Grow(uIndex+m_uGrowAmt))
00097 {
00098 return false;
00099 }
00100 }
00101
00102 m_Array.GetPtr()[uIndex]=Element;
00103
00104 return true;
00105 }
00106
00107 bool SetGrowAmt(UINT uAmt)
00108 {
00109 if(0==uAmt)
00110 {
00111 return false;
00112 }
00113 else
00114 {
00115 m_uGrowAmt=uAmt;
00116 return true;
00117 }
00118 }
00119
00120 void SetInitCallback(void (*pCallback)(type*))
00121 {
00122 m_pInitCallback=pCallback;
00123 }
00124
00125
00126
00127 bool SetElementAt(type* pIn, UINT uIndex)
00128 {
00129 if(uIndex>=m_Array.GetCount())
00130 {
00131 return false;
00132 }
00133
00134 m_Array.GetPtr()[uIndex]=*pIn;
00135
00136 return true;
00137 }
00138
00139
00140
00141
00142 private:
00143 CGXAutoArray<type> m_Array;
00144 UINT m_uGrowAmt;
00145
00146 void (*m_pInitCallback)(type*);
00147 };
00148
00149
00150 template <typename type>
00151 void Zeroer(type* pType)
00152 {
00153 *pType=NULL;
00154 }
00155
00156
00157
00158
00159 #endif