gwnavruntime/database/activenavfloorcollection.h Source File

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