Go to the documentation of this file.00001 #ifndef __FBARRAY_H__
00002 #define __FBARRAY_H__
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00042 #include <kaydaradef.h>
00043 #ifndef FBSDK_DLL
00044
00047 #define FBSDK_DLL K_DLLIMPORT
00048 #endif
00049
00050 #include <fbsdk/fbversion.h>
00051
00052 #include <assert.h>
00053 #include <string.h>
00054
00055 #ifdef FBSDKUseNamespace
00056 namespace FBSDKNamespace {
00057 #endif
00058
00064 FBSDK_DLL void* FBRealloc(void* memblock, size_t size);
00065
00069 FBSDK_DLL void FBFree(void* memblock);
00070
00072
00074
00077 template <class Type> class FBArrayTemplate
00078 {
00079 public:
00083 inline FBArrayTemplate(int pItemPerBlock=10)
00084 {
00085 mArray = NULL;
00086 mArrayCount = 0;
00087 mBlockCount = 0;
00088 mItemPerBlock = pItemPerBlock;
00089 }
00090
00092 inline FBArrayTemplate(const FBArrayTemplate<Type>& pArrayTemplate)
00093 {
00094 mArray = NULL;
00095 mArrayCount = 0;
00096 mBlockCount = 0;
00097 mItemPerBlock = 10;
00098
00099 *this = pArrayTemplate;
00100 }
00101
00103 inline ~FBArrayTemplate()
00104 {
00105 Clear();
00106 }
00107
00113 inline int InsertAt ( int pIndex, Type pItem)
00114 {
00115 if (pIndex>mArrayCount)
00116 {
00117 pIndex = mArrayCount;
00118 }
00119
00120 if (mArrayCount>= mBlockCount*mItemPerBlock)
00121 {
00122
00123 mBlockCount++;
00124 mArray = (Type *)FBRealloc( mArray,(size_t)(mBlockCount*mItemPerBlock*sizeof(Type)));
00125 }
00126
00127 if (pIndex<mArrayCount)
00128 {
00129
00130 memmove (&(mArray[pIndex+1]),&(mArray[pIndex]),sizeof(Type)*(mArrayCount-pIndex));
00131 }
00132
00133 mArray[pIndex] = pItem;
00134 mArrayCount++;
00135
00136 return pIndex;
00137 }
00138
00142 inline void RemoveAt ( int pIndex )
00143 {
00144 assert( pIndex<mArrayCount );
00145 if (pIndex+1<mArrayCount) {
00146 memmove (&(mArray[pIndex]),&(mArray[pIndex+1]),sizeof(Type)*(mArrayCount-pIndex-1));
00147 }
00148 mArrayCount --;
00149 memset (&(mArray[mArrayCount]),0,sizeof(Type));
00150 }
00151
00152
00154 inline void RemoveLast() { RemoveAt( mArrayCount-1 ); }
00155
00160 inline bool Remove ( Type &pItem )
00161 {
00162 int Index = Find( pItem );
00163 if (Index>=0) {
00164 RemoveAt( Index );
00165 return true;
00166 }
00167 return false;
00168 }
00169
00174 inline bool RemoveIt ( Type pItem )
00175 {
00176 int Index = Find( pItem );
00177 if (Index>=0) {
00178 RemoveAt( Index );
00179 return true;
00180 }
00181 return false;
00182 }
00183
00185 inline void Clear()
00186 {
00187 if (mArray!=NULL) {
00188 FBFree(mArray);
00189 mArray = NULL;
00190 }
00191 mArrayCount = 0L;
00192 mBlockCount = 0L;
00193 }
00194
00199 inline Type &operator[](int pIndex) const
00200 {
00201 assert( pIndex<mArrayCount );
00202 return mArray[pIndex];
00203 }
00204
00209 inline void SetAt(int pIndex,Type pItem)
00210 {
00211 assert( pIndex<mArrayCount );
00212 mArray[pIndex] = pItem;
00213 }
00214
00219 inline void SetLast(Type pItem)
00220 {
00221 SetAt(mArrayCount-1,pItem );
00222 }
00223
00227 inline int GetCount () const
00228 {
00229 return mArrayCount;
00230 }
00231
00234 inline void SetCount(int pCount)
00235 {
00236 if (pCount > mArrayCount)
00237 {
00238 if( pCount )
00239 {
00240 const int lTempNewBlockCount = ( (int) (mArrayCount+pCount + mItemPerBlock - 1 ) / mItemPerBlock );
00241 const int lNewBlockCount = (lTempNewBlockCount > 1 ? lTempNewBlockCount : 1);
00242
00243 const int lOldArraySize = mArrayCount*sizeof(Type);
00244 const int lNewArraySize = lNewBlockCount*mItemPerBlock*sizeof(Type);
00245
00246 if( lNewBlockCount > (int) mBlockCount )
00247 {
00248 mArray = (Type *)FBRealloc( mArray, (size_t) lNewArraySize );
00249 mBlockCount = lNewBlockCount;
00250 }
00251
00252 memset( ((char *)mArray) + lOldArraySize, 0, (size_t) (lNewArraySize-lOldArraySize) );
00253 mArrayCount += pCount;
00254 }
00255 } else
00256 {
00257 mArrayCount = pCount;
00258 }
00259 }
00260
00265 inline Type GetAt(int pIndex)
00266 {
00267 assert( pIndex<mArrayCount );
00268 return mArray[pIndex];
00269 }
00270
00274 inline Type GetLast()
00275 {
00276 return mArray[mArrayCount-1];
00277 }
00278
00283 inline int Find( Type pItem )
00284 {
00285 int Count;
00286 for (Count=0; Count<mArrayCount; Count++) {
00287 if (mArray[Count]==pItem) {
00288 return Count;
00289 }
00290 }
00291 return -1;
00292 }
00293
00298 inline int Add( Type pItem )
00299 {
00300 return InsertAt( mArrayCount,pItem );
00301 }
00302
00307 inline Type *GetArray()
00308 {
00309 return mArray;
00310 }
00311
00316 inline FBArrayTemplate<Type>& operator=(const FBArrayTemplate<Type>& pArrayTemplate)
00317 {
00318 if (this != &pArrayTemplate)
00319 {
00320 Clear();
00321
00322 mItemPerBlock = pArrayTemplate.mItemPerBlock;
00323
00324 SetCount(pArrayTemplate.GetCount());
00325 memcpy(mArray, pArrayTemplate.mArray, sizeof(Type) * pArrayTemplate.GetCount());
00326 }
00327
00328 return (*this);
00329 }
00330
00331 private:
00332 Type *mArray;
00333 int mArrayCount;
00334 int mBlockCount;
00335 int mItemPerBlock;
00336 };
00337
00340 typedef class FBSDK_DLL FBArrayTemplate<bool *> FBArrayHBool;
00341 typedef class FBSDK_DLL FBArrayTemplate<char *> FBArrayHChar;
00342 typedef class FBSDK_DLL FBArrayTemplate<int *> FBArrayHkInt;
00343 typedef class FBSDK_DLL FBArrayTemplate<unsigned int *> FBArrayHkUInt;
00344 typedef class FBSDK_DLL FBArrayTemplate<float *> FBArrayHkFloat;
00345 typedef class FBSDK_DLL FBArrayTemplate<double *> FBArrayHkDouble;
00346 typedef class FBSDK_DLL FBArrayTemplate<void *> FBArrayHVoid;
00347
00348 typedef class FBSDK_DLL FBArrayTemplate<bool> FBArrayBool;
00349 typedef class FBSDK_DLL FBArrayTemplate<char> FBArrayChar;
00350 typedef class FBSDK_DLL FBArrayTemplate<int> FBArrayInt;
00351 typedef class FBSDK_DLL FBArrayTemplate<unsigned int> FBArrayUInt;
00352 typedef class FBSDK_DLL FBArrayTemplate<float> FBArrayFloat;
00353 typedef class FBSDK_DLL FBArrayTemplate<double> FBArrayDouble;
00354
00360 #define FB_DEFINE_ARRAY( DllTag, Type ) \
00361 typedef class DllTag FBArrayTemplate< FB##Type* > FBArray##Type;
00362
00368 #if defined(KARCH_DEV_INTEL)
00369 #define FBImplementArray( DllTag, Type )
00370 #else
00371 #define FBImplementArray( DllTag, Type ) \
00372 template class DllTag FBSDKNamespaceFunc( FBArrayTemplate ) < FB##Type* >;
00373 #endif
00374
00375
00376 #ifdef FBSDKUseNamespace
00377 }
00378 #endif
00379 #endif // __FBARRAY_H
00380