gwnavruntime/database/activenavfloorcollection.h Source File

activenavfloorcollection.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 // ---------- Primary contact: JUBA - secondary contact: NOBODY
8 #ifndef Navigation_ActiveNavFloorCollection_H
9 #define Navigation_ActiveNavFloorCollection_H
10 
13 
14 namespace Kaim
15 {
16 
17 // small container to store the activeNavFloors within a CellPos in the ActiveData
18 // all the NavFloor* of the same NavCell are contiguous in memory
19 // allocation are made in a dedicated heap
20 // no deep copy
21 class ActiveNavFloorCollection
22 {
23  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_NavData)
24 
25 public:
26  ActiveNavFloorCollection() : m_activeNavFloors(KY_NULL), m_count(0), m_capacity(0) {}
27  ~ActiveNavFloorCollection() { KY_LOG_ERROR_IF(m_activeNavFloors != KY_NULL, ("memory leak !")); }
28 
29  KY_INLINE bool IsEmpty() const { return m_count == 0; }
30  KY_INLINE KyUInt32 GetCount() const { return m_count; }
31 
32  void GetOwnershipOfData(ActiveNavFloorCollection& other)
33  {
34  ForceClear();
35 
36  m_activeNavFloors = other.m_activeNavFloors;
37  m_count = other.m_count;
38  m_capacity = other.m_capacity;
39 
40  other.m_activeNavFloors = KY_NULL;
41  other.m_count = 0;
42  other.m_capacity = 0;
43  }
44 
45  // resize to m_count + numberOfNavFloorInCell, set the NavCell* for each new NavFloor*
46  void InsertActiveNavFloorsOfNavCell(MemoryHeap* heap, NavCell* navCell)
47  {
48  const KyUInt32 numberOfNavFloorInCell = navCell->GetNavFloorCount();
49  const KyUInt32 previousCount = m_count;
50 
51  KyUInt32 newCapacity = Max<KyUInt32>(4, m_capacity);
52  if (m_count + numberOfNavFloorInCell > m_capacity)
53  {
54  while(previousCount + numberOfNavFloorInCell > newCapacity)
55  newCapacity *= 2;
56 
57  Reserve(heap, newCapacity);
58  }
59 
60  // += cause warning in vc8...
61  m_count = m_count + (KyUInt16)numberOfNavFloorInCell;
62  KY_ASSERT(m_count <= m_capacity);
63 
64  NavFloor** navFloors = GetNavFloors() + previousCount;
65  for(KyUInt32 idx = 0; idx < numberOfNavFloorInCell; ++idx)
66  navFloors[idx] = navCell->GetNavFloor(idx);
67  }
68 
69  NavFloor** GetNavFloors() const { return m_activeNavFloors; }
70 
71  void Reserve(MemoryHeap* heap, KyUInt32 newCapacity)
72  {
73  KY_DEBUG_ASSERTN(newCapacity > m_capacity, ("no shrinking !"));
74 
75  char* newData = (char*)KY_HEAP_ALLOC(heap, newCapacity * (KyUInt32)sizeof(NavFloor*), MemStat_NavData);
76 
77 #if defined(KY_BUILD_DEBUG)
78  memset(newData, 0xFE, newCapacity * (KyUInt32)sizeof(NavFloor*));
79 #endif
80 
81  if (m_capacity > 0)
82  {
83  if (m_count >0)
84  memcpy(newData, (char*)m_activeNavFloors, m_count * sizeof(NavFloor*));
85 
86  KY_FREE(m_activeNavFloors);
87  }
88 
89  m_capacity = (KyUInt16)newCapacity;
90  m_activeNavFloors = (NavFloor**)newData;
91  }
92 
93  void Clear()
94  {
95 #if defined(KY_BUILD_DEBUG)
96  if (m_capacity > 0)
97  memset((char*)m_activeNavFloors, 0, m_capacity * ((KyUInt32)sizeof(NavFloor*)));
98 #endif
99  m_count = 0;
100  }
101 
102  void ForceClear()
103  {
104  if (m_capacity > 0)
105  {
106  KY_FREE(m_activeNavFloors);
107 
108  m_count = 0;
109  m_capacity = 0;
110  m_activeNavFloors = KY_NULL;
111  }
112  else
113  {
114  KY_ASSERT(m_count == 0);
115  KY_ASSERT(m_capacity == 0);
116  KY_ASSERT(m_activeNavFloors == KY_NULL);
117  }
118  }
119 
120 public:
121  NavFloor** m_activeNavFloors;
122  KyUInt16 m_count;
123  KyUInt16 m_capacity;
124 };
125 
126 }
127 
128 #endif //Navigation_ActiveNavFloorCollection_H
129 
#define KY_NULL
Null value.
Definition: types.h:247
Definition: gamekitcrowddispersion.h:20
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:137
unsigned short KyUInt16
Type used internally to represent an unsigned 16-bit integer.
Definition: types.h:40
unsigned int KyUInt32
Type used internally to represent an unsigned 32-bit integer.
Definition: types.h:36