karrayul.h

Go to the documentation of this file.
00001 
00004 #ifndef _FBXSDK_KARRAYUL_H_
00005 #define _FBXSDK_KARRAYUL_H_
00006 
00007 /**************************************************************************************   
00008  Copyright © 1999 - 2007 Autodesk, Inc. and/or its licensors.
00009  All Rights Reserved.
00010 
00011  The coded instructions, statements, computer programs, and/or related material
00012  (collectively the "Data") in these files contain unpublished information
00013  proprietary to Autodesk, Inc. and/or its licensors, which is protected by
00014  Canada and United States of America federal copyright law and by international
00015  treaties.
00016 
00017  The Data may not be disclosed or distributed to third parties, in whole or in
00018  part, without the prior written consent of Autodesk, Inc. ("Autodesk").
00019 
00020  THE DATA IS PROVIDED "AS IS" AND WITHOUT WARRANTY.
00021  ALL WARRANTIES ARE EXPRESSLY EXCLUDED AND DISCLAIMED. AUTODESK MAKES NO
00022  WARRANTY OF ANY KIND WITH RESPECT TO THE DATA, EXPRESS, IMPLIED OR ARISING
00023  BY CUSTOM OR TRADE USAGE, AND DISCLAIMS ANY IMPLIED WARRANTIES OF TITLE,
00024  NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE OR USE.
00025  WITHOUT LIMITING THE FOREGOING, AUTODESK DOES NOT WARRANT THAT THE OPERATION
00026  OF THE DATA WILL BE UNINTERRUPTED OR ERROR FREE.
00027 
00028  IN NO EVENT SHALL AUTODESK, ITS AFFILIATES, PARENT COMPANIES, LICENSORS
00029  OR SUPPLIERS ("AUTODESK GROUP") BE LIABLE FOR ANY LOSSES, DAMAGES OR EXPENSES
00030  OF ANY KIND (INCLUDING WITHOUT LIMITATION PUNITIVE OR MULTIPLE DAMAGES OR OTHER
00031  SPECIAL, DIRECT, INDIRECT, EXEMPLARY, INCIDENTAL, LOSS OF PROFITS, REVENUE
00032  OR DATA, COST OF COVER OR CONSEQUENTIAL LOSSES OR DAMAGES OF ANY KIND),
00033  HOWEVER CAUSED, AND REGARDLESS OF THE THEORY OF LIABILITY, WHETHER DERIVED
00034  FROM CONTRACT, TORT (INCLUDING, BUT NOT LIMITED TO, NEGLIGENCE), OR OTHERWISE,
00035  ARISING OUT OF OR RELATING TO THE DATA OR ITS USE OR ANY OTHER PERFORMANCE,
00036  WHETHER OR NOT AUTODESK HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH LOSS
00037  OR DAMAGE.
00038    
00039 **************************************************************************************/
00040 #include <kbaselib_h.h>
00041 
00042 #ifndef K_PLUGIN
00043     #include <klib/kdebug.h>
00044 #endif
00045 
00046 #include <string.h>
00047 #include <stdlib.h>
00048 
00049 #define KARRAYUL_BLOCKSIZE 16
00050 
00051 #include <object/i/iobject.h>
00052 
00053 #include <kbaselib_forward.h>
00054 #include <kbaselib_nsbegin.h> // namespace
00055 
00056     /***********************************************************************
00057         CLASS KStaticArray
00058     ************************************************************************/
00059 
00060     template< class Type > class KBaseStaticArray
00061     {
00062         protected:
00063         int  mCount;    
00064         Type *mArrayBuf;
00065 
00066         public:
00067         inline int GetCount() { return mCount; }
00068 
00070         inline Type &operator[](int pIndex) 
00071         {
00072         #ifndef K_PLUGIN
00073             K_ASSERT_MSG( pIndex >= 0   , "Buffer underflow, appelle ton plombier.");
00074             K_ASSERT_MSG( pIndex < mCount,"Buffer overflow, appelle ton plombier.");
00075         #endif
00076             return mArrayBuf[pIndex];
00077         }
00078     };
00079 
00080     // Static Array
00081     template< class Type, int Count > class KStaticArray : public KBaseStaticArray<Type>
00082     {
00083         public:
00084         Type mArray[Count];
00085             inline KStaticArray(){ this->mArrayBuf = mArray; this->mCount = Count;}    
00086     };
00087 
00088     template< class Type, int Count1, int Count2 > class KStaticArray2d
00089     {
00090         public:
00091 #if defined(KARCH_DEV_MSC) && (_MSC_VER <= 1200)// VC6
00092         KStaticArray<Type,Count2> *mArray;
00093 
00094         KStaticArray2d() { mArray = new KStaticArray<Type,Count2>(Count1); }
00095         ~KStaticArray2d() { delete[] mArray; }
00096 #else
00097         KStaticArray<Type,Count2> mArray[Count1];
00098 #endif
00099 
00100         // Access pointer at given index.
00101         inline KStaticArray< Type, Count2 > &operator[](int pIndex) 
00102         {
00103         #ifndef K_PLUGIN
00104             K_ASSERT_MSG( pIndex >= 0   , "Buffer underflow, appelle ton plombier.");
00105             K_ASSERT_MSG( pIndex < Count1,"Buffer overflow, appelle ton plombier.");
00106         #endif
00107             return mArray[pIndex];
00108         }
00109     };
00110 
00111     /***********************************************************************
00112         CLASS KArrayTemplate
00113     ************************************************************************/
00114 
00115     class KBASELIB_DLL KBaseArray
00116     {
00117     protected:
00118         int mArrayCount;
00119         int mBlockCount;
00120         int mItemPerBlock; 
00121         int mItemSize; 
00122 
00127         KBaseArray(int pItemPerBlock, int pItemSize);
00128 
00130         ~KBaseArray();
00131 
00139         int     InsertAt(int pIndex, void *pItem);  
00140 
00147         inline void*    GetAt(int pIndex)                           { return &(mBaseArray[(pIndex)*mItemSize]); }
00148 
00155         void    RemoveAt(int pIndex);
00156 
00162         bool    ValidateIndex( int pIndex ) const;
00163 
00164     public:
00168         inline int GetCount() const { return mArrayCount; }
00169 
00171         void    Clear();
00172 
00174         void    Empty();
00175 
00181         int     Reserve(int pCapacity);
00182 
00184         //  Differ from SetCount because array capacity can be lowewed.
00185 
00191         void    SetCount (int pCount);
00192         void    Resize(int pItemCount);
00193         void    AddMultiple(int pItemCount);
00194 
00196     //
00197     //  WARNING!
00198     //
00199     //  Anything beyond these lines may not be documented accurately and is 
00200     //  subject to change without notice.
00201     //
00203 
00204     #ifndef DOXYGEN_SHOULD_SKIP_THIS
00205 
00206     //private:
00207 
00208         char* mBaseArray;
00209     // Utility functions for the DeleteAndClear function template
00210 
00214         inline bool DeleteOrDestroy( void *pPtr )   { return false; } // return false, Delete can be call on the item
00215         bool DeleteOrDestroy( HIObject pObject );
00216         bool DeleteOrDestroy( KPlug* pPlug );
00217 
00218     #endif // #ifndef DOXYGEN_SHOULD_SKIP_THIS
00219 
00220     };
00221 
00222 
00224     template < class Type > class KArrayTemplate : public KBaseArray
00225     {
00226 
00227     public:
00228         Type *&mArray;
00229 
00233         inline KArrayTemplate(int pItemPerBlock=KARRAYUL_BLOCKSIZE):KBaseArray(pItemPerBlock,sizeof(Type))
00234         ,mArray((Type *&)KBaseArray::mBaseArray)
00235         {
00236         }
00237 
00239         inline KArrayTemplate(const KArrayTemplate<Type>& pArrayTemplate):KBaseArray(KARRAYUL_BLOCKSIZE,sizeof(Type))
00240         ,mArray((Type *&)KBaseArray::mBaseArray)
00241         { 
00242             *this = pArrayTemplate; 
00243         }
00244 
00245         inline ~KArrayTemplate() {}
00246         
00253         inline int InsertAt(int pIndex, Type pItem) 
00254         { 
00255             return KBaseArray::InsertAt( pIndex,&pItem ); 
00256         }
00257         
00262         inline Type RemoveAt(int pIndex)
00263         {
00264             Type tmpItem = GetAt(pIndex);
00265             KBaseArray::RemoveAt( pIndex );
00266             return tmpItem;
00267         }
00268         
00272         inline Type RemoveLast()
00273         {
00274             return RemoveAt(mArrayCount-1);
00275         }
00276 
00281         inline bool RemoveIt(Type pItem)
00282         {
00283             int Index = Find (pItem);
00284             if (Index>=0) 
00285             {
00286                 RemoveAt (Index);
00287                 return true;
00288             }
00289             return false;
00290         }
00291         
00293         inline Type &operator[](int pIndex) const
00294         {
00295         #ifdef _DEBUG
00296             if (!ValidateIndex( pIndex )) 
00297             {
00298                 return (Type &)(KBaseArray::mBaseArray[(0)*sizeof(Type)]);
00299             }
00300         #endif
00301             return (Type &)(KBaseArray::mBaseArray[(pIndex)*sizeof(Type)]);
00302         }
00303 
00305         inline void SetAt(int pIndex, Type pItem)
00306         {
00307         #ifdef _DEBUG
00308             if (!ValidateIndex( pIndex )) 
00309             {
00310                 return;
00311             }
00312         #endif
00313             GetArray()[pIndex] = pItem;
00314         }
00315         
00317         inline void SetLast(Type pItem)
00318         {
00319             SetAt (mArrayCount-1, pItem);
00320         }
00321 
00323         inline Type GetAt(int pIndex) const
00324         {
00325         #ifdef _DEBUG
00326             if (!ValidateIndex( pIndex )) 
00327             {
00328                 return (Type &)(KBaseArray::mBaseArray[(0)*sizeof(Type)]);
00329             }
00330         #endif
00331             return (Type &)(KBaseArray::mBaseArray[(pIndex)*sizeof(Type)]);
00332         }
00333 
00337         inline Type GetFirst() const
00338         { 
00339         #ifndef K_PLUGIN
00340             K_ASSERT( mArrayCount >= 1 );
00341         #endif
00342             return GetAt(0);
00343         }
00344 
00348         inline Type GetLast() const
00349         { 
00350         #ifndef K_PLUGIN
00351             K_ASSERT( mArrayCount >= 1 );
00352         #endif
00353             return GetAt(mArrayCount-1);
00354         }
00355 
00360         inline int Find(Type pItem) const
00361         {
00362             return FindAfter( -1, pItem );
00363         }
00364 
00370         inline int FindAfter(int pAfterIndex, Type pItem) const
00371         {
00372         #ifndef K_PLUGIN
00373         #ifdef _DEBUG       
00374             if ( pAfterIndex > mArrayCount || pAfterIndex < -1 ) 
00375             {
00376                 K_ASSERT_MSG_NOW (_T("ArrayUL : Search Begin Index out of range")); 
00377                 return -1;
00378             }
00379         #endif
00380         #endif
00381             int Count;
00382             for ( Count=pAfterIndex+1; Count<mArrayCount; Count++) 
00383             {
00384                 if (GetAt(Count)==pItem) 
00385                 {
00386                     return Count;
00387                 }
00388             }
00389             return -1;
00390         }
00391         
00397         inline int FindBefore(int pBeforeIndex, Type pItem) const
00398         {
00399         #ifndef K_PLUGIN
00400         #ifdef _DEBUG       
00401             if ( pBeforeIndex > mArrayCount || pBeforeIndex <= 0 ) 
00402             {
00403                 K_ASSERT_MSG_NOW (_T("ArrayUL : Search Begin Index out of range")); 
00404                 return -1;
00405             }
00406         #endif
00407         #endif
00408             int Count;
00409             for ( Count=pBeforeIndex-1; Count>=0; Count--) 
00410             {
00411                 if (GetAt(Count)==pItem) 
00412                 {
00413                     return Count;
00414                 }
00415             }
00416             return -1;
00417         }
00418         
00422         inline int Add(Type pItem)
00423         {
00424             return InsertAt(mArrayCount, pItem);
00425         }
00426 
00430         inline int AddUnique(Type pItem)
00431         {
00432             int lReturnIndex = Find(pItem);
00433             if (lReturnIndex == -1)
00434             {
00435                 lReturnIndex = Add(pItem);
00436             }
00437             return lReturnIndex;
00438         }
00439 
00443         inline void AddMultiple( kUInt pItemCount )
00444         {
00445             KBaseArray::AddMultiple( pItemCount );
00446         }
00447 
00448         inline void AddArray(KArrayTemplate<Type> &pArray)
00449         {
00450             int lSourceIndex, lCount = pArray.GetCount();
00451             if( lCount == 0 ) return;
00452             int lDestinationIndex = GetCount();
00453             AddMultiple(lCount);
00454             for( lSourceIndex = 0; lSourceIndex < lCount; lSourceIndex++)
00455             {
00456                 SetAt(lDestinationIndex++, pArray[lSourceIndex]);
00457             }
00458         }
00459 
00460         inline void AddArrayNoDuplicate(KArrayTemplate<Type> &pArray)
00461         {
00462             int i, lCount = pArray.GetCount();
00463             for( i = 0; i < lCount; i++)
00464             {
00465                 Type lItem = pArray[i];
00466                 if (Find(lItem) == -1)
00467                 {
00468                     Add(lItem);
00469                 }
00470             }
00471         }
00472         inline void RemoveArray(KArrayTemplate<Type> &pArray)
00473         {
00474             int lRemoveIndex, lRemoveCount = pArray.GetCount();
00475             for( lRemoveIndex = 0; lRemoveIndex < lRemoveCount; lRemoveIndex++)
00476             {
00477                 RemoveIt(pArray[lRemoveIndex]);
00478             }
00479         }
00480 
00482         inline Type* GetArray()
00483         {
00484             return (Type*)KBaseArray::mBaseArray;
00485         }
00486 
00488         inline KArrayTemplate<Type>& operator=(const KArrayTemplate<Type>& pArrayTemplate)
00489         {
00490             Clear();
00491 
00492             mItemPerBlock = pArrayTemplate.mItemPerBlock;
00493 
00494             int i, lCount = pArrayTemplate.GetCount();
00495 
00496             for (i = 0; i < lCount; i++)
00497             {
00498                 Add(pArrayTemplate[i]);
00499             }
00500 
00501             return (*this);
00502         }
00503 
00504         #ifdef K_PLUGIN
00506             inline operator Type* ()
00507             {
00508                 return GetArray();
00509             }
00510         #endif
00511     };
00512 
00514     //
00515     //  WARNING!
00516     //
00517     //  Anything beyond these lines may not be documented accurately and is 
00518     //  subject to change without notice.
00519     //
00521 
00522     #ifndef DOXYGEN_SHOULD_SKIP_THIS
00523 
00524     template <class Type> inline void DeleteAndClear(KArrayTemplate<Type>& Array)
00525     {
00526         kUInt lItemCount = Array.GetCount();
00527         while( lItemCount )
00528         {
00529             lItemCount--;
00530             Type& Item = (Array.operator[](lItemCount));
00531             if (!Array.DeleteOrDestroy( Item ))
00532             {
00533                 delete Item;
00534             }
00535             Item = NULL;
00536         }
00537         Array.Clear();
00538     }
00539 
00540     typedef class KBASELIB_DLL KArrayTemplate<int *>    KArrayHkInt;
00541     typedef class KBASELIB_DLL KArrayTemplate<kUInt *>  KArrayHkUInt;
00542     typedef class KBASELIB_DLL KArrayTemplate<double *> KArrayHkDouble;
00543     typedef class KBASELIB_DLL KArrayTemplate<float *>  KArrayHkFloat;
00544     typedef class KBASELIB_DLL KArrayTemplate<void *>   KArrayVoid;
00545     typedef class KBASELIB_DLL KArrayTemplate<char *>   KArrayChar;
00546     typedef class KBASELIB_DLL KArrayTemplate<int>      KArraykInt;
00547     typedef class KBASELIB_DLL KArrayTemplate<kUInt>    KArraykUInt;
00548     typedef class KBASELIB_DLL KArrayTemplate<float>    KArraykFloat;
00549     typedef class KBASELIB_DLL KArrayTemplate<double>   KArraykDouble;
00550 
00551     typedef class KBASELIB_DLL KArrayTemplate<kReference>   KArrayUL;
00552 
00553     #endif // #ifndef DOXYGEN_SHOULD_SKIP_THIS
00554 
00555 #include <kbaselib_nsend.h>
00556 
00557 #endif // #define _FBXSDK_KARRAYUL_H_
00558 
00559