HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_StackBuffer.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_StackBuffer.h (C++)
7  *
8  * COMMENTS: A special buffer class that allocates from the stack if
9  * below the given threshold.
10  *
11  */
12 
13 #ifndef __UT_StackBuffer_h__
14 #define __UT_StackBuffer_h__
15 
16 #include <SYS/SYS_Types.h>
17 
18 template <typename T, size_t MAXBYTES = 256>
20 {
21 public:
22  UT_StackBuffer(size_t num_elems)
23  : mySize(num_elems)
24  {
25  if (num_elems <= MAXELEMS)
26  myData = myBuffer;
27  else
28  myData = new T[num_elems];
29  }
31  {
32  if (myData != myBuffer)
33  delete [] myData;
34  }
35 
36  operator T *()
37  {
38  return myData;
39  }
40  operator const T *() const
41  {
42  return (const T *)myData;
43  }
44 
45  T *array() { return myData; }
46  const T *array() const { return myData; }
47 
48  // Some compilers get confused when there are both the array subscript
49  // operators below and the implicit cast to pointer above,
50  // even though it seems obvious that the operator[] should take
51  // precedence when doing buf[integer].
52 #if 0
53  T &operator[](exint i) { return myData[i]; }
54  const T &operator[](exint i) const { return myData[i]; }
55 #endif
56 
57  T &operator()(exint i) { return myData[i]; }
58  const T &operator()(exint i) const { return myData[i]; }
59 
60  exint size() const { return mySize; }
61 
62  typedef T *iterator;
63  typedef const T *const_iterator;
64  iterator begin() { return myData; }
65  iterator end() { return myData+mySize; }
66  const_iterator begin() const { return myData; }
67  const_iterator end() const { return myData+mySize; }
68 
69 private:
70  static const int MAXELEMS = (MAXBYTES + sizeof(T) - 1)/sizeof(T);
71 
72  T *myData;
73  exint mySize;
74  T myBuffer[MAXELEMS];
75 };
76 
77 #endif // __UT_StackBuffer_h__
iterator end()
UT_StackBuffer(size_t num_elems)
const T & operator()(exint i) const
png_uint_32 i
Definition: png.h:2877
const_iterator end() const
exint size() const
int64 exint
Definition: SYS_Types.h:109
T & operator()(exint i)
const T * const_iterator
const_iterator begin() const
iterator begin()
const T * array() const