gwnavruntime/kernel/SF_ArrayStaticBuff.h Source File

SF_ArrayStaticBuff.h
Go to the documentation of this file.
1 /*
2 * Copyright 2015 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 /**************************************************************************
8 
9 PublicHeader: Kernel
10 Filename : KY_ArrayStaticBuff.h
11 Content :
12 Created : 2010
13 Authors : MaximShemanarev
14 
15 **************************************************************************/
16 
17 #ifndef INC_KY_Kernel_ArrayStaticBuff_H
18 #define INC_KY_Kernel_ArrayStaticBuff_H
19 
21 #include <string.h>
22 
23 namespace Kaim {
24 
25 //------------------------------------------------------------------------
26 template<class T, int StaticSize, int SID=Stat_Default_Mem> class ArrayStaticBuffPOD
27 {
28 public:
29  typedef T ValueType;
30 
31  ArrayStaticBuffPOD(MemoryHeap* heap = 0) :
32  pHeap(heap), Size(0), Reserved(StaticSize), Data(Static)
33  {}
34 
35  ~ArrayStaticBuffPOD()
36  {
37  Clear();
38  }
39 
40  void Clear()
41  {
42  if (Data != Static)
43  KY_FREE(Data);
44  Data = Static;
45  Size = 0;
46  }
47 
48  void PushBack(const T& val)
49  {
50  if (Size < StaticSize)
51  {
52  Static[Size++] = val;
53  return;
54  }
55  if (Size == StaticSize)
56  {
57  Reserved *= 2;
58  Data = (T*)(pHeap ? KY_HEAP_ALLOC(pHeap, Reserved * sizeof(T), SID):
59  KY_HEAP_AUTO_ALLOC_ID(this, Reserved * sizeof(T), SID));
60  memcpy(Data, Static, sizeof(Static));
61  }
62  else
63  if (Size >= Reserved)
64  {
65  Reserved *= 2;
66  Data = (T*)(pHeap ? Memory::ReallocInHeap(pHeap, Data, Reserved * sizeof(T)) :
67  Memory::Realloc( Data, Reserved * sizeof(T)));
68  }
69  Data[Size++] = val;
70  }
71 
72  UPInt GetSize() const
73  {
74  return Size;
75  }
76 
77  void CutAt(UPInt size)
78  {
79  if (size < Size)
80  Size = size;
81  }
82 
83  const T& operator [] (UPInt i) const { return Data[i]; }
84  T& operator [] (UPInt i) { return Data[i]; }
85  const T& At(UPInt i) const { return Data[i]; }
86  T& At(UPInt i) { return Data[i]; }
87  T ValueAt(UPInt i) const { return Data[i]; }
88 
89 private:
90  // Copying is prohibited
91  ArrayStaticBuffPOD(const ArrayStaticBuffPOD<T, StaticSize, SID>& v);
92  const ArrayStaticBuffPOD<T, StaticSize, SID>& operator = (const ArrayStaticBuffPOD<T, StaticSize, SID>& v);
93 
94  MemoryHeap* pHeap;
95  UPInt Size;
96  UPInt Reserved;
97  ValueType Static[StaticSize];
98  ValueType* Data;
99 };
100 
101 
102 template<class T, int StaticSize, int SID=Stat_Default_Mem> class ArrayStaticBuff
103 {
104 public:
105  typedef T ValueType;
106 
107  ArrayStaticBuff(MemoryHeap* heap = 0) :
108  pHeap(heap), Size(0), Reserved(StaticSize), Data(Static)
109  {
110  }
111 
112  ~ArrayStaticBuff()
113  {
114  Clear();
115  }
116 
117  void Clear()
118  {
119  if (Data != Static)
120  {
121  DestructArray(Data, Size);
122  KY_FREE(Data);
123  }
124  Data = Static;
125  Size = 0;
126  }
127 
128  void PushBack(const T& val)
129  {
130  if (Size < StaticSize)
131  {
132  Static[Size++] = val;
133  return;
134  }
135  if (Size == StaticSize)
136  {
137  Reserved *= 2;
138  Data = (T*)(pHeap ? KY_HEAP_ALLOC(pHeap, Reserved * sizeof(T), SID):
139  KY_HEAP_AUTO_ALLOC_ID(this, Reserved * sizeof(T), SID));
140  memcpy(Data, Static, sizeof(Static));
141  ConstructArray(Data + Size, Reserved - Size, T());
142  }
143  else if (Size >= Reserved)
144  {
145  Reserved *= 2;
146  Data = (T*)(pHeap ? Memory::ReallocInHeap(pHeap, Data, Reserved * sizeof(T)) :
147  Memory::Realloc( Data, Reserved * sizeof(T)));
148  ConstructArray(Data + Size, Reserved - Size, T());
149  }
150  Data[Size++] = val;
151  }
152 
153  UPInt GetSize() const
154  {
155  return Size;
156  }
157 
158  const T& operator [] (UPInt i) const { return Data[i]; }
159  T& operator [] (UPInt i) { return Data[i]; }
160  const T& At(UPInt i) const { return Data[i]; }
161  T& At(UPInt i) { return Data[i]; }
162  T ValueAt(UPInt i) const { return Data[i]; }
163 
164 private:
165  // Copying is prohibited
166  ArrayStaticBuff(const ArrayStaticBuff<T, StaticSize, SID>& v);
167  const ArrayStaticBuff<T, StaticSize, SID>& operator = (const ArrayStaticBuff<T, StaticSize, SID>& v);
168 
169  MemoryHeap* pHeap;
170  UPInt Size;
171  UPInt Reserved;
172  ValueType Static[StaticSize];
173  ValueType* Data;
174 };
175 
176 } // Scaleform
177 
178 #endif
Definition: gamekitcrowddispersion.h:20