gwnavruntime/blob/baseblobbuilder.h Source File

baseblobbuilder.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 // primary contact: GUAL - secondary contact: NOBODY
9 #ifndef Navigation_BaseBlobBuilder_H
10 #define Navigation_BaseBlobBuilder_H
11 
12 
15 #include <assert.h>
16 
17 
18 namespace Kaim
19 {
20 
21 
28 template<class T>
29 class BaseBlobBuilder : public NewOverrideBase<MemStat_BlobBuilder>
30 {
31 public:
32  typedef T BlobType;
33 
34  BaseBlobBuilder(KyInt32 memStat = Stat_Default_Mem, MemoryHeap* heap = KY_NULL)
36  , m_blob(KY_NULL)
37  , m_buildingPart(BUILDING_SHALLOW_PART)
38  , m_shallowBlobSize(0)
39  , m_memStat(memStat)
40  , m_heap(heap)
41  {}
42 
45  bool IsWriteMode() { return m_blobBuffer->IsWriteMode(); }
46 
51  T* Build(BlobHandler<T>& blobHandler);
52 
54  void BuildFlatBlob(T& blob);
55 
56  virtual ~BaseBlobBuilder() {}
57 
58 private:
67  virtual void DoBuild() = 0;
68 
69 public:
71  void DoBuildAllocatedBlob(BlobBuffer* blobBuffer, T* blob);
72 
74  void DoAllocAndBuildReferencedBlob(BlobBuffer* blobBuffer, BlobRef<T>* blobRef);
75 
77  BlobBuffer* GetBlobBufferToBuildThis()
78  {
79  const bool isBuildingShallowPart = (m_buildingPart == BUILDING_SHALLOW_PART);
80 
81  KY_DEBUG_ASSERTN(isBuildingShallowPart,
82  ("In BaseBlobBuilder::DoBuild(), BUILD_REFERENCED_BLOB must be called after all calls to BLOB_ARRAY, BLOB_ARRAY_COPY, BLOB_STRING, BLOB_BUILD"));
83 
84  return (isBuildingShallowPart) ? m_blobBuffer : KY_NULL;
85  }
86 
87  BlobBuffer* GetBlobBufferToBuildReference()
88  {
89  EndShallowPart();
90  m_buildingPart = BUILDING_DEEP_PART;
91  return m_blobBuffer;
92  }
93 
94  void EndShallowPart()
95  {
96  if (m_buildingPart == BUILDING_SHALLOW_PART)
97  {
98  assert(m_shallowBlobSize == 0); // should not be called twice
99  m_shallowBlobSize = m_blobBuffer->m_offset;
100  }
101  else // (m_buildingPart == BUILDING_DEEP_PART)
102  {
103  m_blobBuffer->SetBlobRefInfoFromCurrentOffset();
104  }
105  }
106 
107 public:
109  BlobBuffer* m_blobBuffer;
110 
112  T* m_blob;
113 
114  enum BuildingPart { BUILDING_SHALLOW_PART = 0, BUILDING_DEEP_PART = 1 };
115  BuildingPart m_buildingPart;
116 
117  KyUInt32 m_shallowBlobSize;
118 
119  KyInt32 m_memStat;
120  MemoryHeap* m_heap;
121 };
122 
123 
129 #define BLOB_SET(blob, value) \
130 if (this->IsWriteMode()) (blob) = (value)
131 
138 #define BLOB_ARRAY(blobArray, count) \
139 this->GetBlobBufferToBuildThis()->AllocArray(this->IsWriteMode() ? &(blobArray) : KY_NULL, count)
140 
150 #define BLOB_ARRAY_COPY(blobArray, src, count) \
151 this->GetBlobBufferToBuildThis()->AllocAndCopyArray(this->IsWriteMode() ? &(blobArray) : KY_NULL, (count) != 0 ? (src) : KY_NULL, (KyUInt32)(count))
152 
154 #define BLOB_ARRAY_COPY_2(blobArray, ky_array) \
155 BLOB_ARRAY_COPY(blobArray, ky_array.GetDataPtr(), ky_array.GetSize())
156 
165 #define BLOB_STRING(str, src) \
166 this->GetBlobBufferToBuildThis()->AllocAndCopyArray(this->IsWriteMode() ? &(str) : KY_NULL, src, (KyUInt32)SFstrlen(src) + 1)
167 
174 #define BLOB_BUILD(blob, builder) \
175 builder.DoBuildAllocatedBlob(this->GetBlobBufferToBuildThis(), this->IsWriteMode() ? &(blob) : KY_NULL)
176 // alias
177 #define BUILD_BLOB(blob, builder) BLOB_BUILD(blob, builder)
178 
181 #define BUILD_REFERENCED_BLOB(blobRef, builder) \
182 builder.DoAllocAndBuildReferencedBlob(this->GetBlobBufferToBuildReference(), this->IsWriteMode() ? &(blobRef) : KY_NULL)
183 
186 #define COPY_REFERENCED_BLOB(blobRef, srcBlob, srcBlobDeepSize, srcBlobShallowSize) \
187 this->GetBlobBufferToBuildReference()->AllocAndCopyReferencedBlob( \
188  this->IsWriteMode() ? &(blobRef) : KY_NULL, srcBlob, srcBlobDeepSize, srcBlobShallowSize)
190 #define COPY_REFERENCED_BLOB_FROM_HANDLER(blobRef, blobHandler) \
191 this->GetBlobBufferToBuildReference()->AllocAndCopyReferencedBlobFromBlobHandler( \
192  this->IsWriteMode() ? &(blobRef) : KY_NULL, blobHandler)
193 
194 
195 // --------------------------------- inline implementation ---------------------------------
196 
197 template<class T>
198 T* BaseBlobBuilder<T>::Build(BlobHandler<T>& blobHandler)
199 {
200  BlobBuffer blobBuffer;
201  m_blobBuffer = &blobBuffer;
202 
203  // COUNT
204  m_blobBuffer->Alloc<T>();
205  m_buildingPart = BUILDING_SHALLOW_PART;
206  m_shallowBlobSize = 0;
207  DoBuild();
208 
209  // ALLOCATE
210  m_blobBuffer->SwitchToWriteMode(blobHandler, m_shallowBlobSize, m_heap, m_memStat);
211 
212  // WRITE
213  m_blob = m_blobBuffer->Alloc<T>();
214  m_buildingPart = BUILDING_SHALLOW_PART;
215  m_shallowBlobSize = 0;
216  DoBuild();
217 
218  return (T*)m_blob;
219 }
220 
221 
222 template<class T>
224 {
225  m_blob = &blob;
226  DoBuild();
227 }
228 
229 
230 template<class T>
231 void BaseBlobBuilder<T>::DoBuildAllocatedBlob(BlobBuffer* blobBuffer, T* blob)
232 {
233  m_blobBuffer = blobBuffer;
234  m_blob = blob;
235  DoBuild();
236 }
237 
238 
239 template<class T>
240 void BaseBlobBuilder<T>::DoAllocAndBuildReferencedBlob(BlobBuffer* blobBuffer, BlobRef<T>* blobRef)
241 {
242  m_blobBuffer = blobBuffer;
243 
244  m_blobBuffer->BeginBlobRefBuffer(blobRef);
245 
246  m_blob = m_blobBuffer->Alloc<T>();
247  DoBuild();
249  m_blobBuffer->SetBlobRefInfoFromCurrentOffset();
250 }
251 
252 
253 }
254 
255 
256 #endif
void DoAllocAndBuildReferencedBlob(BlobBuffer *blobBuffer, BlobRef< T > *blobRef)
For internal use. Use BUILD_REFERENCED_BLOB instead.
Definition: baseblobbuilder.h:257
int KyInt32
Type used internally to represent a 32-bit integer.
Definition: types.h:35
#define KY_NULL
Null value.
Definition: types.h:247
BlobBuffer * GetBlobBufferToBuildThis()
For internal use. Check if m_buildingPart == BUILDING_SHALLOW_PART and return this.
Definition: baseblobbuilder.h:81
The BlobHandler class is a top-level mechanism for serializing blobs between objects in memory and fi...
Definition: blobhandler.h:45
void DoBuildAllocatedBlob(BlobBuffer *blobBuffer, T *blob)
For internal use. Use BLOB_BUILD instead.
Definition: baseblobbuilder.h:248
A BlobRef is a type of reference that is compatible with the blob serialization framework.
Definition: blobref.h:58
BlobBuffer * m_blobBuffer
For internal use. Use BLOB_SET and BLOB_ARRAY instead.
Definition: baseblobbuilder.h:114
T * m_blob
The blob maintained by this builder. Only modify using the macros listed under DoBuild().
Definition: baseblobbuilder.h:117
Definition: gamekitcrowddispersion.h:20
bool IsWriteMode()
Indicates whether the builder is operating in COUNT mode or in WRITE mode.
Definition: baseblobbuilder.h:47
void BuildFlatBlob(T &blob)
Simple way to use DoBuild in case of flat blob (that is, a blob that does not have a BlobArray or Blo...
Definition: baseblobbuilder.h:240
BaseBlobBuilder is an abstract base class that builds a blob within a contiguous block of memory...
Definition: baseblobbuilder.h:30
unsigned int KyUInt32
Type used internally to represent an unsigned 32-bit integer.
Definition: types.h:36
virtual void DoBuild()=0
Implement this function in any class that derives from BaseBlobBuilder.
T * Build(BlobHandler< T > &blobHandler)
This method:Calls DoBuild() in COUNT mode to determine the amount of memory needed for the blob to be...
Definition: baseblobbuilder.h:215