gwnavruntime/querysystem/workingmemcontainers/statusingridbase.h Source File

statusingridbase.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 
12 
13 namespace Kaim
14 {
15 
16 struct BitFieldInBuffer
17 {
18  BitFieldInBuffer() : m_offSet(KyUInt32MAXVAL) {}
19 
20  KyUInt32* GetWords() const { return (KyUInt32*)((char*)this + m_offSet); }
21  void SetWords(KyUInt32* words) { m_offSet = (KyUInt32)((char*)words - (char*)(this)); }
22 
23  bool IsValid() const { return m_offSet != KyUInt32MAXVAL; }
24 
25  bool IsBitSet(KyUInt32 idx) const { return BitFieldUtils::IsBitSet(GetWords(), idx); }
26  void SetBit(KyUInt32 idx) const { BitFieldUtils::SetBit(GetWords(), idx); }
27 
28 private:
29  KyUInt32 m_offSet; // relative to this &m_offSet == this
30 
31 private:
32  BitFieldInBuffer(const BitFieldInBuffer&);
33  BitFieldInBuffer& operator=(const BitFieldInBuffer&) { return *this; }
34 };
35 
36 template <class T>
37 struct ArrayInBuffer
38 {
39  ArrayInBuffer() : m_offSet(KyUInt32MAXVAL) {}
40 
41  T* GetArrayData() const { return (T*)((char*)this + m_offSet); }
42  void SetArrayData(T* buffer) { m_offSet = (KyUInt32)((char*)buffer - (char*)(this)); }
43 
44  bool IsValid() const { return m_offSet != KyUInt32MAXVAL; }
45 
46  T* Get(KyUInt32 idx) { return GetArrayData() + idx; }
47  const T* Get(KyUInt32 idx) const { return GetArrayData() + idx; }
48 
49 private:
50  KyUInt32 m_offSet; // relative to this &m_offSet == this
51 
52 private:
53  ArrayInBuffer(const ArrayInBuffer<T>&);
54  ArrayInBuffer& operator=(const ArrayInBuffer<T>&) { return *this; }
55 };
56 
57 
58 
59 // Used as implementation detail of TriangleStatusInGrid and NavFloorAndNavGraphEdgeStatusInGrid
60 // This functionality should be put directly in the WorkingMemContainerBase (WorkingMemBuffer)
61 class AllocatorBuffer
62 {
63 public:
64  AllocatorBuffer() {}
65 
66  AllocatorBuffer(WorkingMemory* workingMemory)
67  {
68  m_buffer.Init(workingMemory);
69  m_allocatedSize = 0;
70  }
71 
72  void Init(WorkingMemory* workingMemory)
73  {
74  m_buffer.Init(workingMemory);
75  m_allocatedSize = 0;
76  }
77 
78  void ReleaseWorkingMemoryBuffer() { m_buffer.ReleaseBuffer(); }
79 
80  void* AllocInBuffer(KyUInt32 sizeOfOneElementInBytes, KyUInt32 numberOfElements, KyUInt8 memsetValue)
81  {
82  KY_DEBUG_ASSERTN(sizeOfOneElementInBytes % 4 == 0, ("AllocateInBufferAndMemset only accepts sizeOfOneElementInBytes that are mulptiples of 4"));
83 
84  const KyUInt32 deltaSize = sizeOfOneElementInBytes * numberOfElements;
85  const KyUInt32 newAllocatedSize = m_allocatedSize + deltaSize;
86 
87  if (m_buffer.Realloc(m_allocatedSize, newAllocatedSize) == nullptr)
88  return nullptr;
89 
90  char* ptr = GetDataPtr() + m_allocatedSize;
91 
92  memset(ptr, memsetValue, deltaSize);
93 
94  m_allocatedSize = newAllocatedSize;
95  return ptr;
96  }
97 
98  template<typename T>
99  T* AllocInBuffer(KyUInt32 count, KyUInt8 memsetValue)
100  {
101  return (T*)AllocInBuffer(sizeof(T), count, memsetValue); // ugly, let's use T() ctr to init the memory
102  }
103 
104 
105  KyUInt32* AllocBitFieldWords(KyUInt32 bitCount)
106  {
107  return (KyUInt32*)AllocInBuffer(sizeof(KyUInt32), BitFieldUtils::GetWordsCount(bitCount), 0);
108  }
109 
110  char* GetDataPtr() const { return m_buffer.GetDataPtr(); }
111 
112  bool IsInitialized() const { return m_buffer.IsInitialized(); }
113 
114  KyUInt32 GetOffset(void* ptr) const { return (KyUInt32)((char*)ptr - GetDataPtr()); }
115 
116  template<typename T>
117  T* GetPtr(KyUInt32 offset) const { return (T*)(GetDataPtr() + offset); }
118 
119 public:
120  WorkingMemContainerBase m_buffer;
121  KyUInt32 m_allocatedSize; // in m_buffer
122 };
123 
124 
125 
126 }
127 
128 
129 
std::uint32_t KyUInt32
uint32_t
Definition: types.h:29
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
std::uint8_t KyUInt8
uint8_t
Definition: types.h:27
#define KyUInt32MAXVAL
KyUInt32 max value
Definition: types.h:68