Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

RainAutoArray.h

Go to the documentation of this file.
00001 #ifndef __AUTOARRAY
00002 #define __AUTOARRAY
00003 
00004 
00005 
00006 
00007 #include "RainDefines.h"
00008 
00009 
00010 
00011 template<typename type>
00012 class CRainAutoArray
00013 {
00014 public:
00015         CRainAutoArray()
00016         {
00017                 m_pArray=NULL;
00018                 m_uCount=0;
00019         }
00020 
00021         ~CRainAutoArray()
00022         {
00023                 Cleanup();
00024         }
00025 
00026 
00027         bool Init(UINT uCount)
00028         {
00029                 Cleanup();
00030 
00031                 if(0==uCount)
00032                 {
00033                         m_pArray=NULL;
00034                         return true;
00035                 }
00036 
00037 
00038                 m_pArray=new type[uCount];
00039                 if(!m_pArray)
00040                 {
00041                         return false;
00042                 }
00043                 else
00044                 {
00045                         m_uCount=uCount;
00046                         return true;
00047                 }
00048         }
00049 
00050         void Cleanup()
00051         {
00052                 SAFE_ARRAY_DELETE(m_pArray);
00053                 m_uCount=0;
00054         }
00055 
00056 
00057         inline UINT GetCount()
00058         {
00059                 return m_uCount;
00060         }
00061 
00062         inline type* GetPtr()
00063         {
00064                 return m_pArray;
00065         }
00066 
00067         inline bool IsNonNull()
00068         {
00069                 return (NULL!=m_pArray);
00070         }
00071 
00072         inline bool CopyIn(type* pSrc,UINT uCount)
00073         {
00074                 if(uCount>m_uCount)
00075                 {
00076                         if(!Init(uCount))
00077                         {
00078                                 return false;
00079                         }
00080                 }
00081                 
00082                 for(UINT uCtr=0;uCtr<uCount;uCtr++)
00083                 {
00084                         m_pArray[uCtr]=pSrc[uCtr];
00085                 }
00086 
00087                 return true;
00088                 
00089         }
00090 
00091 
00092         bool WriteToStream(IStream* pStream)
00093         {
00094                 assert(pStream!=NULL);
00095                 
00096 
00097                 DWORD dwBytesToBeWritten;
00098                 DWORD dwBytesWritten;
00099 
00100                 //write number of things that will follow
00101                 dwBytesToBeWritten=sizeof(DWORD);
00102 
00103                 UINT uData=GetCount();
00104 
00105                 if(FAILED(pStream->Write(&uData, 
00106                                                                 dwBytesToBeWritten, 
00107                                                                 &dwBytesWritten)))
00108                 {
00109                         return false;
00110                 }
00111 
00112                 if(dwBytesWritten!=dwBytesToBeWritten)
00113                 {
00114                         return false;
00115                 }
00116 
00117 
00118                 //WriteData
00119                 dwBytesToBeWritten=sizeof(type)*GetCount();
00120                 
00121 
00122                 if(FAILED(pStream->Write(GetPtr(), 
00123                                                                 dwBytesToBeWritten,
00124                                                                 &dwBytesWritten)))
00125                 {
00126                         return false;
00127                 }
00128 
00129                 if(dwBytesWritten!=dwBytesToBeWritten)
00130                 {
00131                         return false;
00132                 }
00133 
00134                 return true;
00135         }
00136 
00137 
00138         bool LoadFromStream(IStream* pStream)
00139         {
00140                 assert(NULL!=pStream);
00141 
00142                 DWORD   dwBytesToRead, dwBytesRead;
00143                 
00144                 
00145                 //read number of elements to follow
00146                 dwBytesToRead=sizeof(DWORD);
00147                 DWORD   dwCount;
00148                         
00149                 if(FAILED(pStream->Read(&dwCount, dwBytesToRead, &dwBytesRead)))
00150                 {
00151                         return false;
00152                 }
00153 
00154                 if(dwBytesRead!=dwBytesToRead)
00155                 {
00156                         return false;
00157                 }
00158 
00159 
00160                 //allocate enough space
00161                 if(!Init(dwCount))
00162                 {
00163                         return false;
00164                 }
00165 
00166                 dwBytesToRead=sizeof(type)*GetCount();
00167 
00168                 //Read the actual data
00169                 if(FAILED(pStream->Read(GetPtr(), dwBytesToRead, &dwBytesRead)))
00170                 {
00171                         return false;
00172                 }
00173 
00174                 if(dwBytesRead!=dwBytesToRead)
00175                 {
00176                         return false;
00177                 }
00178 
00179                 return true;
00180         }
00181 
00182 
00183         inline void GetValAt(UINT uIndex, type* pOut)
00184         {
00185                 assert(NULL!=pOut);
00186                 assert(uIndex<GetCount());
00187 
00188                 *pOut=m_pArray[uIndex];
00189         }
00190 
00191 
00192         void GetFilteredVal(float fAmt, type* pOut)
00193         {
00194                 assert(pOut);
00195                 assert(fAmt<=1.0f);
00196                 assert(fAmt>=0.0f);
00197                 assert(m_uCount>0);
00198 
00199                 if(m_uCount==1)
00200                 {
00201                         *pOut=m_pArray[0];
00202                         return;
00203                 }
00204 
00205                 UINT uHighIndex=(UINT) (ceil(fAmt*m_uCount));
00206                 UINT uLowIndex=(UINT) (floor(fAmt*m_uCount));
00207 
00208                 if(uHighIndex=m_uCount)
00209                 {
00210                         uHighIndex--;
00211                 }
00212 
00213 
00214                 float fLerpAmt=(m_uCount*fAmt)-(float)uLowIndex;
00215                 
00216                 BlakeUtilLerp(fLerpAmt, &(m_pArray[uLowIndex]), &(m_pArray[uHighIndex]), pOut);
00217         }
00218 
00219         
00220 
00221 
00222         //type casting
00223         inline operator type*()
00224         {
00225                 return m_pArray;
00226         }
00227 
00228                 
00229 private:                
00230         type* m_pArray;
00231         UINT  m_uCount; 
00232 };
00233 
00234 
00235 
00236 #endif

Generated on Thu May 23 17:51:00 2002 by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001