fbarray.h

Go to the documentation of this file.
00001 #ifndef __FBARRAY_H__
00002 #define __FBARRAY_H__
00003 /**************************************************************************
00004  Copyright (c) 1994 - 2009 Autodesk, Inc. and/or its licensors.
00005  All Rights Reserved.
00006  
00007  The coded instructions, statements, computer programs, and/or related 
00008  material (collectively the "Data") in these files contain unpublished 
00009  information proprietary to Autodesk, Inc. and/or its licensors, which is 
00010  protected by Canada and United States of America federal copyright law 
00011  and by international treaties.
00012  
00013  The Data may not be disclosed or distributed to third parties, in whole 
00014  or in part, without the prior written consent of Autodesk, Inc. 
00015  ("Autodesk").
00016  
00017  THE DATA IS PROVIDED "AS IS" AND WITHOUT WARRANTY.
00018  ALL WARRANTIES ARE EXPRESSLY EXCLUDED AND DISCLAIMED. AUTODESK MAKES NO 
00019  WARRANTY OF ANY KIND WITH RESPECT TO THE DATA, EXPRESS, IMPLIED OR 
00020  ARISING BY CUSTOM OR TRADE USAGE, AND DISCLAIMS ANY IMPLIED WARRANTIES 
00021  OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR 
00022  PURPOSE OR USE. WITHOUT LIMITING THE FOREGOING, AUTODESK DOES NOT 
00023  WARRANT THAT THE OPERATION OF THE DATA WILL BE UNINTERRUPTED OR ERROR 
00024  FREE.
00025  
00026  IN NO EVENT SHALL AUTODESK, ITS AFFILIATES, PARENT COMPANIES, LICENSORS 
00027  OR SUPPLIERS ("AUTODESK GROUP") BE LIABLE FOR ANY LOSSES, DAMAGES OR 
00028  EXPENSES OF ANY KIND (INCLUDING WITHOUT LIMITATION PUNITIVE OR MULTIPLE 
00029  DAMAGES OR OTHER SPECIAL, DIRECT, INDIRECT, EXEMPLARY, INCIDENTAL, LOSS 
00030  OF PROFITS, REVENUE OR DATA, COST OF COVER OR CONSEQUENTIAL LOSSES OR 
00031  DAMAGES OF ANY KIND), HOWEVER CAUSED, AND REGARDLESS OF THE THEORY OF 
00032  LIABILITY, WHETHER DERIVED FROM CONTRACT, TORT (INCLUDING, BUT NOT 
00033  LIMITED TO, NEGLIGENCE), OR OTHERWISE, ARISING OUT OF OR RELATING TO THE 
00034  DATA OR ITS USE OR ANY OTHER PERFORMANCE, WHETHER OR NOT AUTODESK HAS 
00035  BEEN ADVISED OF THE POSSIBILITY OF SUCH LOSS OR DAMAGE.
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 // FBArrayTemplate
00075 template <class Type> class FBArrayTemplate 
00076 {
00077   public:
00081     inline FBArrayTemplate(int pItemPerBlock=10) 
00082     {
00083         mArray          = NULL;
00084         mArrayCount     = 0;
00085         mBlockCount     = 0;
00086         mItemPerBlock   = pItemPerBlock;    
00087     }
00088 
00090     inline ~FBArrayTemplate()
00091     {
00092         Clear();
00093     }
00094 
00100     inline int InsertAt ( int pIndex, Type pItem)
00101     {
00102         if (pIndex>mArrayCount) 
00103         {
00104             pIndex = mArrayCount;
00105         }
00106 
00107         if (mArrayCount>= mBlockCount*mItemPerBlock) 
00108         {
00109              // must allocate or reallocate block of items
00110               mBlockCount++;
00111               mArray = (Type *)FBRealloc( mArray,(size_t)(mBlockCount*mItemPerBlock*sizeof(Type)));
00112         }
00113 
00114         if (pIndex<mArrayCount) 
00115         {
00116             // This is an insert
00117             memmove (&(mArray[pIndex+1]),&(mArray[pIndex]),sizeof(Type)*(mArrayCount-pIndex));
00118         }
00119 
00120         mArray[pIndex] = pItem;
00121         mArrayCount++;
00122 
00123         return pIndex;
00124     }
00125 
00129     inline void RemoveAt ( int pIndex )
00130     {
00131         assert( pIndex<mArrayCount );
00132         if (pIndex+1<mArrayCount) {
00133             memmove (&(mArray[pIndex]),&(mArray[pIndex+1]),sizeof(Type)*(mArrayCount-pIndex-1));
00134         }
00135         mArrayCount --;
00136         memset (&(mArray[mArrayCount]),0,sizeof(Type)); // Cleanup last element to make sure we don't access it later
00137     }
00138     
00139     
00141     inline void RemoveLast() { RemoveAt( mArrayCount-1 ); } 
00142 
00147     inline bool Remove ( Type &pItem )
00148     {
00149       int Index = Find( pItem );
00150         if (Index>=0) {
00151             RemoveAt( Index );
00152             return true;
00153         }
00154         return false;
00155     }
00156 
00161     inline bool RemoveIt ( Type pItem )
00162     {
00163       int Index = Find( pItem );
00164         if (Index>=0) {
00165             RemoveAt( Index );
00166             return true;
00167         }
00168         return false;
00169     }
00170 
00172     inline void Clear()
00173     {
00174         if (mArray!=NULL) {
00175             FBFree(mArray);
00176             mArray = NULL;
00177         }
00178         mArrayCount  = 0L;
00179         mBlockCount  = 0L;
00180     }
00181 
00186     inline Type &operator[](int pIndex) const
00187     {
00188         assert( pIndex<mArrayCount );
00189         return mArray[pIndex];
00190     }
00191 
00196     inline void SetAt(int pIndex,Type pItem)
00197     {
00198         assert( pIndex<mArrayCount );
00199         mArray[pIndex] = pItem;
00200     }
00201 
00206     inline void SetLast(Type pItem) 
00207     { 
00208         SetAt(mArrayCount-1,pItem ); 
00209     }
00210 
00214     inline int GetCount () const
00215     { 
00216         return mArrayCount; 
00217     }
00218 
00221     inline void SetCount(int pCount)
00222     {
00223         if (pCount > mArrayCount)
00224         {
00225             if( pCount )
00226             {
00227                 const int lTempNewBlockCount = ( (int) (mArrayCount+pCount + mItemPerBlock - 1 ) / mItemPerBlock );
00228                 const int lNewBlockCount = (lTempNewBlockCount > 1 ? lTempNewBlockCount : 1);
00229 
00230                 const int lOldArraySize = mArrayCount*sizeof(Type);
00231                 const int lNewArraySize = lNewBlockCount*mItemPerBlock*sizeof(Type);
00232 
00233                 if( lNewBlockCount > (int) mBlockCount )
00234                 {           
00235                     mArray = (Type *)FBRealloc( mArray, (size_t) lNewArraySize );
00236                     mBlockCount = lNewBlockCount;
00237                 }
00238                 
00239                 memset( ((char *)mArray) + lOldArraySize, 0, (size_t) (lNewArraySize-lOldArraySize) );
00240                 mArrayCount += pCount;
00241             }
00242         } else 
00243         {
00244             mArrayCount = pCount;
00245         }
00246     }
00247 
00252     inline Type GetAt(int pIndex) 
00253     {       
00254         assert( pIndex<mArrayCount );
00255         return mArray[pIndex]; 
00256     }
00257 
00261     inline Type GetLast()           
00262     { 
00263         return mArray[mArrayCount-1]; 
00264     }
00265 
00270     inline int Find( Type pItem )
00271     {
00272       int Count;
00273         for (Count=0; Count<mArrayCount; Count++) {
00274             if (mArray[Count]==pItem) {
00275                 return Count;
00276             }
00277         }
00278         return -1;
00279     }
00280 
00285     inline int Add( Type pItem ) 
00286     { 
00287         return InsertAt( mArrayCount,pItem ); 
00288     }
00289 
00294     inline Type *GetArray() 
00295     { 
00296         return mArray; 
00297     }
00298 
00303     inline FBArrayTemplate<Type>& operator=(const FBArrayTemplate<Type>& pArrayTemplate)
00304     {
00305         Clear();
00306 
00307         mItemPerBlock = pArrayTemplate.mItemPerBlock;
00308 
00309         int i, lCount = pArrayTemplate.GetCount();
00310 
00311         for (i = 0; i < lCount; i++)
00312         {
00313             Add(pArrayTemplate[i]);
00314         }
00315 
00316         return (*this);
00317     }
00318 
00319   private:
00320     Type      *mArray;          
00321     int       mArrayCount;      
00322     int       mBlockCount;      
00323     int       mItemPerBlock;
00324 };
00325 
00331 #define FB_DEFINE_ARRAY( DllTag, Type ) \
00332     typedef class DllTag FBArrayTemplate< HFB##Type > FBArray##Type;
00333 
00339 #if defined(KARCH_DEV_INTEL)
00340     #define FBImplementArray( DllTag, Type ) 
00341 #else
00342     #define FBImplementArray( DllTag, Type ) \
00343         template class DllTag FBSDKNamespaceFunc( FBArrayTemplate ) < HFB##Type >;
00344 #endif
00345 
00346 
00347 #ifdef FBSDKUseNamespace
00348     }
00349 #endif
00350 #endif // __FBARRAY_H
00351