HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_StackArray.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_StackArray.h ( UT Library, C++ )
7  *
8  * COMMENTS: A class to allocate small, dynamic arrays on the stack from
9  * a static memory pool
10  */
11 
12 #ifndef __UT_StackArray__
13 #define __UT_StackArray__
14 
15 #include "UT_API.h"
16 #include "UT_ValArray.h"
17 #include "UT_Array.h"
18 #include "UT_IntArray.h"
19 #include "UT_FloatArray.h"
20 #include "UT_ThreadSpecificValue.h"
21 
22 template <typename Array>
24 public:
25  int alloc()
26  {
27  int idx;
28 
29  if (myFreeList.entries())
30  {
31  idx = myFreeList.last();
32  myFreeList.removeLast();
33  myValue(idx)->entries(0);
34  }
35  else
36  {
37  idx = myValue.entries();
38  myValue.append(new Array);
39  }
40  return idx;
41  }
42  void release(int idx)
43  {
44  myFreeList.append(idx);
45  }
46  Array &get(int idx)
47  {
48  return *myValue(idx);
49  }
50 private:
51  UT_ValArray<Array *> myValue;
52  UT_IntArray myFreeList;
53 };
54 
55 #if !defined(WIN32) && !defined(MBSD)
56 
58 public:
60  {
61  myIdx = myData.get().alloc();
62  }
64  {
65  myData.get().release(myIdx);
66  }
67  template <typename T>
69  {
70  return *(UT_ValArray<T> *)&myData.get().get(myIdx);
71  }
72 
73 private:
76  int myIdx;
77 };
78 
79 template <typename Array>
81 public:
83  {
84  myIdx = myData.get().alloc();
85  }
87  {
88  myData.get().release(myIdx);
89  }
90  Array &get()
91  {
92  return myData.get().get(myIdx);
93  }
94 
95 private:
97  UT_StackArrayData<Array> > myData;
98  int myIdx;
99 };
100 
101 #else
102 
103 class UT_StackPtrArray {
104 public:
105  template <typename T>
106  UT_ValArray<T> &get()
107  {
108  return *(UT_ValArray<T> *)&myData;
109  }
110 
111 private:
112  UT_ValArray<void *> myData;
113 };
114 
115 template <typename Array>
116 class UT_StackArray {
117 public:
118  Array &get()
119  {
120  return myData;
121  }
122 
123 private:
124  Array myData;
125 };
126 
127 #endif
128 
129 // Macros to simplify creation of stack arrays
130 
131 #define UT_STACK_PTRARRAY(type, name) \
132  UT_StackPtrArray __##name##_stack__; \
133  UT_ValArray<type> &name = __##name##_stack__.get<type>()
134 
135 #define UT_STACK_INTARRAY(name) \
136  UT_StackArray<UT_IntArray> __##name##_stack__; \
137  UT_IntArray &name = __##name##_stack__.get()
138 
139 #define UT_STACK_FLTARRAY(name) \
140  UT_StackArray<UT_FloatArray> __##name##_stack__; \
141  UT_FloatArray &name = __##name##_stack__.get()
142 
143 #endif
T & last()
Definition: UT_Array.h:591
Definition: ImfArray.h:77
SYS_FORCE_INLINE void removeLast()
Definition: UT_Array.h:223
void release(int idx)
Definition: UT_StackArray.h:42
#define UT_API
Definition: UT_API.h:13
exint entries() const
Alias of size(). size() is preferred.
Definition: UT_Array.h:453
exint append(void)
Definition: UT_Array.h:95