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
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              // must allocate or reallocate block of items
00123               mBlockCount++;
00124               mArray = (Type *)FBRealloc( mArray,(size_t)(mBlockCount*mItemPerBlock*sizeof(Type)));
00125         }
00126 
00127         if (pIndex<mArrayCount) 
00128         {
00129             // This is an insert
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)); // Cleanup last element to make sure we don't access it later
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