fbsdk/fbarray.h Source File

fbarray.h
Go to the documentation of this file.
1 #ifndef __FBARRAY_H__
2 #define __FBARRAY_H__
3 /**************************************************************************
4  Copyright (c) 1994 - 2009 Autodesk, Inc. and/or its licensors.
5  All Rights Reserved.
6 
7  The coded instructions, statements, computer programs, and/or related
8  material (collectively the "Data") in these files contain unpublished
9  information proprietary to Autodesk, Inc. and/or its licensors, which is
10  protected by Canada and United States of America federal copyright law
11  and by international treaties.
12 
13  The Data may not be disclosed or distributed to third parties, in whole
14  or in part, without the prior written consent of Autodesk, Inc.
15  ("Autodesk").
16 
17  THE DATA IS PROVIDED "AS IS" AND WITHOUT WARRANTY.
18  ALL WARRANTIES ARE EXPRESSLY EXCLUDED AND DISCLAIMED. AUTODESK MAKES NO
19  WARRANTY OF ANY KIND WITH RESPECT TO THE DATA, EXPRESS, IMPLIED OR
20  ARISING BY CUSTOM OR TRADE USAGE, AND DISCLAIMS ANY IMPLIED WARRANTIES
21  OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR
22  PURPOSE OR USE. WITHOUT LIMITING THE FOREGOING, AUTODESK DOES NOT
23  WARRANT THAT THE OPERATION OF THE DATA WILL BE UNINTERRUPTED OR ERROR
24  FREE.
25 
26  IN NO EVENT SHALL AUTODESK, ITS AFFILIATES, PARENT COMPANIES, LICENSORS
27  OR SUPPLIERS ("AUTODESK GROUP") BE LIABLE FOR ANY LOSSES, DAMAGES OR
28  EXPENSES OF ANY KIND (INCLUDING WITHOUT LIMITATION PUNITIVE OR MULTIPLE
29  DAMAGES OR OTHER SPECIAL, DIRECT, INDIRECT, EXEMPLARY, INCIDENTAL, LOSS
30  OF PROFITS, REVENUE OR DATA, COST OF COVER OR CONSEQUENTIAL LOSSES OR
31  DAMAGES OF ANY KIND), HOWEVER CAUSED, AND REGARDLESS OF THE THEORY OF
32  LIABILITY, WHETHER DERIVED FROM CONTRACT, TORT (INCLUDING, BUT NOT
33  LIMITED TO, NEGLIGENCE), OR OTHERWISE, ARISING OUT OF OR RELATING TO THE
34  DATA OR ITS USE OR ANY OTHER PERFORMANCE, WHETHER OR NOT AUTODESK HAS
35  BEEN ADVISED OF THE POSSIBILITY OF SUCH LOSS OR DAMAGE.
36 
37 **************************************************************************/
38 
42 #include <kaydaradef.h>
43 #ifndef FBSDK_DLL
44 
47  #define FBSDK_DLL K_DLLIMPORT
48 #endif
49 
50 #include <fbsdk/fbversion.h>
51 
52 #include <assert.h>
53 #include <string.h>
54 
55 #ifdef FBSDKUseNamespace
56  namespace FBSDKNamespace {
57 #endif
58 
64 FBSDK_DLL void* FBRealloc(void* memblock, size_t size);
65 
69 FBSDK_DLL void FBFree(void* memblock);
70 
72 // FBArrayTemplate
74 
77 template <class Type> class FBArrayTemplate
78 {
79  public:
83  inline FBArrayTemplate(int pItemPerBlock=10)
84  {
85  mArray = NULL;
86  mArrayCount = 0;
87  mBlockCount = 0;
88  mItemPerBlock = pItemPerBlock;
89  }
90 
92  inline FBArrayTemplate(const FBArrayTemplate<Type>& pArrayTemplate)
93  {
94  mArray = NULL;
95  mArrayCount = 0;
96  mBlockCount = 0;
97  mItemPerBlock = 10;
98 
99  *this = pArrayTemplate;
100  }
101 
104  {
105  Clear();
106  }
107 
113  inline int InsertAt ( int pIndex, Type pItem)
114  {
115  if (pIndex>mArrayCount)
116  {
117  pIndex = mArrayCount;
118  }
119 
120  if (mArrayCount>= mBlockCount*mItemPerBlock)
121  {
122  // must allocate or reallocate block of items
123  mBlockCount++;
124  mArray = (Type *)FBRealloc( mArray,(size_t)(mBlockCount*mItemPerBlock*sizeof(Type)));
125  }
126 
127  if (pIndex<mArrayCount)
128  {
129  // This is an insert
130  memmove (&(mArray[pIndex+1]),&(mArray[pIndex]),sizeof(Type)*(mArrayCount-pIndex));
131  }
132 
133  mArray[pIndex] = pItem;
134  mArrayCount++;
135 
136  return pIndex;
137  }
138 
142  inline void RemoveAt ( int pIndex )
143  {
144  assert( pIndex<mArrayCount );
145  if (pIndex+1<mArrayCount) {
146  memmove (&(mArray[pIndex]),&(mArray[pIndex+1]),sizeof(Type)*(mArrayCount-pIndex-1));
147  }
148  mArrayCount --;
149  memset (&(mArray[mArrayCount]),0,sizeof(Type)); // Cleanup last element to make sure we don't access it later
150  }
151 
152 
154  inline void RemoveLast() { RemoveAt( mArrayCount-1 ); }
155 
160  inline bool Remove ( Type &pItem )
161  {
162  int Index = Find( pItem );
163  if (Index>=0) {
164  RemoveAt( Index );
165  return true;
166  }
167  return false;
168  }
169 
174  inline bool RemoveIt ( Type pItem )
175  {
176  int Index = Find( pItem );
177  if (Index>=0) {
178  RemoveAt( Index );
179  return true;
180  }
181  return false;
182  }
183 
185  inline void Clear()
186  {
187  if (mArray!=NULL) {
188  FBFree(mArray);
189  mArray = NULL;
190  }
191  mArrayCount = 0L;
192  mBlockCount = 0L;
193  }
194 
199  inline Type &operator[](int pIndex) const
200  {
201  assert( pIndex<mArrayCount );
202  return mArray[pIndex];
203  }
204 
209  inline void SetAt(int pIndex,Type pItem)
210  {
211  assert( pIndex<mArrayCount );
212  mArray[pIndex] = pItem;
213  }
214 
219  inline void SetLast(Type pItem)
220  {
221  SetAt(mArrayCount-1,pItem );
222  }
223 
227  inline int GetCount () const
228  {
229  return mArrayCount;
230  }
231 
234  inline void SetCount(int pCount)
235  {
236  if (pCount > mArrayCount)
237  {
238  if( pCount )
239  {
240  const int lTempNewBlockCount = ( (int) (mArrayCount+pCount + mItemPerBlock - 1 ) / mItemPerBlock );
241  const int lNewBlockCount = (lTempNewBlockCount > 1 ? lTempNewBlockCount : 1);
242 
243  const int lOldArraySize = mArrayCount*sizeof(Type);
244  const int lNewArraySize = lNewBlockCount*mItemPerBlock*sizeof(Type);
245 
246  if( lNewBlockCount > (int) mBlockCount )
247  {
248  mArray = (Type *)FBRealloc( mArray, (size_t) lNewArraySize );
249  mBlockCount = lNewBlockCount;
250  }
251 
252  memset( ((char *)mArray) + lOldArraySize, 0, (size_t) (lNewArraySize-lOldArraySize) );
253  mArrayCount += pCount;
254  }
255  } else
256  {
257  mArrayCount = pCount;
258  }
259  }
260 
265  inline Type GetAt(int pIndex)
266  {
267  assert( pIndex<mArrayCount );
268  return mArray[pIndex];
269  }
270 
274  inline Type GetLast()
275  {
276  return mArray[mArrayCount-1];
277  }
278 
283  inline int Find( Type pItem )
284  {
285  int Count;
286  for (Count=0; Count<mArrayCount; Count++) {
287  if (mArray[Count]==pItem) {
288  return Count;
289  }
290  }
291  return -1;
292  }
293 
298  inline int Add( Type pItem )
299  {
300  return InsertAt( mArrayCount,pItem );
301  }
302 
307  inline Type *GetArray()
308  {
309  return mArray;
310  }
311 
317  {
318  if (this != &pArrayTemplate)
319  {
320  Clear();
321 
322  mItemPerBlock = pArrayTemplate.mItemPerBlock;
323 
324  SetCount(pArrayTemplate.GetCount());
325  memcpy(mArray, pArrayTemplate.mArray, sizeof(Type) * pArrayTemplate.GetCount());
326  }
327 
328  return (*this);
329  }
330 
331  private:
332  Type *mArray;
333  int mArrayCount;
334  int mBlockCount;
335  int mItemPerBlock;
336 };
337 
341 typedef class FBSDK_DLL FBArrayTemplate<char *> FBArrayHChar;
343 typedef class FBSDK_DLL FBArrayTemplate<unsigned int *> FBArrayHkUInt;
344 typedef class FBSDK_DLL FBArrayTemplate<float *> FBArrayHkFloat;
345 typedef class FBSDK_DLL FBArrayTemplate<double *> FBArrayHkDouble;
346 typedef class FBSDK_DLL FBArrayTemplate<void *> FBArrayHVoid;
347 
351 typedef class FBSDK_DLL FBArrayTemplate<unsigned int> FBArrayUInt;
354 
360 #define FB_DEFINE_ARRAY( DllTag, Type ) \
361  typedef class DllTag FBArrayTemplate< FB##Type* > FBArray##Type;
362 
368 #if defined(KARCH_DEV_INTEL)
369  #define FBImplementArray( DllTag, Type )
370 #else
371  #define FBImplementArray( DllTag, Type ) \
372  template class DllTag FBSDKNamespaceFunc( FBArrayTemplate ) < FB##Type* >;
373 #endif
374 
375 
376 #ifdef FBSDKUseNamespace
377  }
378 #endif
379 #endif // __FBARRAY_H
380 
Type & operator[](int pIndex) const
[] operator overload.
Definition: fbarray.h:199
int
Definition: code.h:72
Type GetLast()
Get last item of the array.
Definition: fbarray.h:274
Type GetAt(int pIndex)
Get item at pIndex.
Definition: fbarray.h:265
class FBArrayTemplate< unsigned int > FBArrayUInt
Definition: fbarray.h:351
FBArrayTemplate(const FBArrayTemplate< Type > &pArrayTemplate)
Copy constructor.
Definition: fbarray.h:92
bool
Definition: asdl.h:9
#define NULL
Definition: kaydara.h:169
void RemoveAt(int pIndex)
Remove item at pIndex.
Definition: fbarray.h:142
void SetCount(int pCount)
Set the number of items in the array.
Definition: fbarray.h:234
#define FBSDK_DLL
Ensure that FBSDK_DLL is defined only once...
Definition: fbarray.h:47
void FBFree(void *memblock)
General free function, actually calling standard function "free".
void Clear()
Empty the array of all items.
Definition: fbarray.h:185
class FBArrayTemplate< bool > FBArrayBool
Definition: fbarray.h:348
bool Remove(Type &pItem)
Remove pItem from the array.
Definition: fbarray.h:160
bool RemoveIt(Type pItem)
Remove pItem from the array.
Definition: fbarray.h:174
Type * GetArray()
Get a pointer to the array of items.
Definition: fbarray.h:307
Py_ssize_t Py_ssize_t size
Definition: bufferobject.h:20
class FBArrayTemplate< int > FBArrayInt
Definition: fbarray.h:350
class FBArrayTemplate< char * > FBArrayHChar
Definition: fbarray.h:341
class FBArrayTemplate< int * > FBArrayHkInt
Definition: fbarray.h:342
void RemoveLast()
Remove the last item in the array.
Definition: fbarray.h:154
define declarations to determine cross-platform namespace conventions.
void SetAt(int pIndex, Type pItem)
Set item at pIndex to pItem.
Definition: fbarray.h:209
~FBArrayTemplate()
Destructor.
Definition: fbarray.h:103
#define Index(a0, a1)
Definition: Python-ast.h:517
class FBArrayTemplate< double > FBArrayDouble
Definition: fbarray.h:353
int Add(Type pItem)
Add an item to the end of the array.
Definition: fbarray.h:298
int GetCount() const
Get the number of items in the array.
Definition: fbarray.h:227
int InsertAt(int pIndex, Type pItem)
Insert pItem at pIndex.
Definition: fbarray.h:113
Template class to contain an array of items.
Definition: fbarray.h:77
#define FBSDKNamespace
FBSDKNamespace define.
Definition: fbversion.h:64
class FBArrayTemplate< bool * > FBArrayHBool
Pre-defined common used array types.
Definition: fbarray.h:340
class FBArrayTemplate< float > FBArrayFloat
Definition: fbarray.h:352
FBArrayTemplate(int pItemPerBlock=10)
Constructor.
Definition: fbarray.h:83
void SetLast(Type pItem)
Set the last item of the array.
Definition: fbarray.h:219
FBArrayTemplate< Type > & operator=(const FBArrayTemplate< Type > &pArrayTemplate)
Copy array of pointers without copying the associated objects.
Definition: fbarray.h:316
class FBArrayTemplate< float * > FBArrayHkFloat
Definition: fbarray.h:344
class FBArrayTemplate< unsigned int * > FBArrayHkUInt
Definition: fbarray.h:343
int Find(Type pItem)
Find the index of pItem in the array.
Definition: fbarray.h:283
class FBArrayTemplate< double * > FBArrayHkDouble
Definition: fbarray.h:345
class FBArrayTemplate< void * > FBArrayHVoid
Definition: fbarray.h:346
void * FBRealloc(void *memblock, size_t size)
General allocation function, actually calling standard function "realloc".
class FBArrayTemplate< char > FBArrayChar
Definition: fbarray.h:349