gwnavruntime/blob/blobfield32.h Source File

blobfield32.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 
8 #pragma once
9 
10 
16 
17 
18 namespace Kaim
19 {
20 
21 
22 class BlobField32
23 {
24  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
25 public:
26  static const KyUInt32 Type_Unset = 0;
27  static const KyUInt32 Type_KyUInt32 = 1;
28  static const KyUInt32 Type_KyInt32 = 2;
29  static const KyUInt32 Type_KyFloat32 = 3;
30  static const KyUInt32 Type_Bool = 4;
31 
32 public:
33  BlobField32() : m_type(Type_Unset), m_value(0) {}
34 
35  KyUInt32& ValueAsUInt32() { return m_value; }
36  KyInt32& ValueAsInt32() { return *(KyInt32*)&m_value; }
37  KyFloat32& ValueAsFloat32() { char* value = (char*)&m_value; return *(KyFloat32*)value; }
38 
39 public:
40  BlobArray<char> m_name;
41  KyUInt32 m_type;
42  KyUInt32 m_value;
43 };
44 inline void SwapEndianness(Endianness::Target e, BlobField32& self)
45 {
46  SwapEndianness(e, self.m_name);
47  SwapEndianness(e, self.m_type);
48  SwapEndianness(e, self.m_value);
49 }
50 
51 
52 class BlobField32Mapping
53 {
54  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
55 public:
56  BlobField32Mapping() : m_type(BlobField32::Type_Unset), m_name(nullptr), m_value(nullptr) {}
57 
58  BlobField32Mapping(KyUInt32 type, const char* name, bool* value) : m_type(type), m_name(name), m_value((char*)value) {}
59  BlobField32Mapping(KyUInt32 type, const char* name, KyUInt32* value) : m_type(type), m_name(name), m_value((char*)value) {}
60  BlobField32Mapping(KyUInt32 type, const char* name, KyInt32* value) : m_type(type), m_name(name), m_value((char*)value) {}
61  BlobField32Mapping(KyUInt32 type, const char* name, KyFloat32* value) : m_type(type), m_name(name), m_value((char*)value) {}
62 
63  bool& ValueAsBool() { return *((bool*)m_value); }
64  KyUInt32& ValueAsUInt32() { return *((KyUInt32*)m_value); }
65  KyInt32& ValueAsInt32() { return *((KyInt32*)m_value); }
66  KyFloat32& ValueAsFloat32() { return *((KyFloat32*)m_value); }
67 
68  bool GetValueAsBool() { return *((bool*)m_value); }
69  KyUInt32 GetValueAsUInt32() { return *((KyUInt32*)m_value); }
70  KyInt32 GetValueAsInt32() { return *((KyInt32*)m_value); }
71  KyFloat32 GetValueAsFloat32() { return *((KyFloat32*)m_value); }
72 
73  void SetValueAsBool(bool value) { *((bool*)m_value) = value; }
74  void SetValueAsUInt32(KyUInt32 value) { *((KyUInt32*)m_value) = value; }
75  void SetValueAsInt32(KyInt32 value) { *((KyInt32*)m_value) = value; }
76  void SetValueAsFloat32(KyFloat32 value) { *((KyFloat32*)m_value) = value; }
77 
78  void SetValueFromBlobValue32(KyUInt32 blobValue32)
79  {
80  if (m_type == BlobField32::Type_Bool)
81  *((bool*)m_value) = (blobValue32 != 1); // 32 bits copy to bool
82  else
83  *((KyUInt32*)m_value) = blobValue32; // 32 bits copy
84  }
85 
86 public:
87  KyUInt32 m_type;
88  const char* m_name;
89  char* m_value; // char* to avoid strict aliasing optimizations
90 };
91 
92 
93 template <class OSTREAM>
94 inline OSTREAM& operator<<(OSTREAM& os, BlobField32Mapping& mapping)
95 {
96  os << mapping.m_name << " : ";
97 
98  switch(mapping.m_type)
99  {
100  case BlobField32::Type_KyUInt32 : os <<(*(KyUInt32*) (mapping.m_value)); break;
101  case BlobField32::Type_KyInt32 : os <<(*(KyInt32*) (mapping.m_value)); break;
102  case BlobField32::Type_KyFloat32 : os <<(*(KyFloat32*)(mapping.m_value)); break;
103  case BlobField32::Type_Bool : os <<(*(bool*) (mapping.m_value)); break;
104  }
105 
106  return os;
107 }
108 
109 
110 class BlobField32Builder : public BaseBlobBuilder<BlobField32>
111 {
112 public:
113  BlobField32Builder(BlobField32Mapping& mapping) : m_mapping(&mapping) {}
114 
115 private:
116  virtual void DoBuild()
117  {
118  BLOB_SET(m_blob->m_type, m_mapping->m_type);
119  BLOB_STRING(m_blob->m_name, m_mapping->m_name);
120  BLOB_SET(m_blob->m_value, m_mapping->ValueAsUInt32());
121  }
122 
123  BlobField32Mapping* m_mapping;
124 };
125 
126 
127 
128 
129 class BlobMultiField32
130 {
131  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
132 public:
133  BlobMultiField32() : m_valueType(BlobField32::Type_Unset) {}
134 
135  KyUInt32 GetCount() const { return m_values.GetCount(); }
136 
137  const KyUInt32& ValueAsUInt32( KyUInt32 idx) const { return m_values.GetValues()[idx]; }
138  const KyInt32& ValueAsInt32( KyUInt32 idx) const { char* v = (char*)(&m_values.GetValues()[idx]); return *(KyInt32*)v; }
139  const KyFloat32& ValueAsFloat32(KyUInt32 idx) const { char* v = (char*)(&m_values.GetValues()[idx]); return *(KyFloat32*)v; }
140 
141  KyUInt32 GetValueAsUInt32( KyUInt32 idx) const { return m_values.GetValues()[idx]; }
142  KyInt32 GetValueAsInt32( KyUInt32 idx) const { char* v = (char*)(&m_values.GetValues()[idx]); return *(KyInt32*)v; }
143  KyFloat32 GetValueAsFloat32(KyUInt32 idx) const { char* v = (char*)(&m_values.GetValues()[idx]); return *(KyFloat32*)v; }
144 
145  void SetValueAsUInt32( KyUInt32 idx, KyUInt32 value) { m_values.GetValues()[idx] = value; }
146  void SetValueAsInt32( KyUInt32 idx, KyInt32 value) { char* v = (char*)&value; m_values.GetValues()[idx] = *((KyUInt32*)v); }
147  void SetValueAsFloat32(KyUInt32 idx, KyFloat32 value) { char* v = (char*)&value; m_values.GetValues()[idx] = *((KyUInt32*)v); }
148 
149 public:
150  BlobArray<char> m_name;
151  BlobArray<char> m_category;
152  KyUInt32 m_valueType;
153  BlobArray<KyUInt32> m_values;
154 };
155 inline void SwapEndianness(Endianness::Target e, BlobMultiField32& self)
156 {
157  SwapEndianness(e, self.m_name);
158  SwapEndianness(e, self.m_category);
159  SwapEndianness(e, self.m_valueType);
160  SwapEndianness(e, self.m_values);
161 }
162 
163 
164 class BlobMultiField32Mapping
165 {
166  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
167 public:
168  BlobMultiField32Mapping()
169  : m_valueType(BlobField32::Type_Unset), m_name(nullptr), m_category(nullptr) {}
170 
171  BlobMultiField32Mapping(KyUInt32 type, const char* name, const char* category, KyUInt32 size)
172  : m_valueType(type), m_name(name), m_category(category), m_values(size) {}
173 
174 
175  KyUInt32 GetCount() const { return m_values.GetCount(); }
176 
177  void MapUInt32(KyUInt32 index, const KyUInt32& value) { m_values[index] = (char*)&value; }
178  void MapInt32(KyUInt32 index, const KyInt32& value) { m_values[index] = (char*)&value; }
179  void MapFloat32(KyUInt32 index, const KyFloat32& value) { m_values[index] = (char*)&value; }
180 
181  KyUInt32& ValueAsUInt32(KyUInt32 index) { return *((KyUInt32*)m_values[index]); }
182  KyInt32& ValueAsInt32(KyUInt32 index) { return *((KyInt32*)m_values[index]); }
183  KyFloat32& ValueAsFloat32(KyUInt32 index) { return *((KyFloat32*)m_values[index]); }
184 
185  void SetValueFromBlobValue32(KyUInt32 index, KyUInt32 blobValue32)
186  {
187  if (m_valueType == BlobField32::Type_Bool)
188  *((bool*)m_values[index]) = (blobValue32 != 1); // 32 bits copy to bool
189  else
190  *((KyUInt32*)m_values[index]) = blobValue32; // 32 bits copy
191  }
192 
193 public:
194  KyUInt32 m_valueType;
195  const char* m_name;
196  const char* m_category;
197  KyArrayPOD<char*> m_values;
198 };
199 
200 
201 template <class OSTREAM>
202 inline OSTREAM& operator<<(OSTREAM& os, BlobMultiField32Mapping& mapping)
203 {
204  os << mapping.m_name << "(" << mapping.m_category << ") : ";
205 
206  for (KyUInt32 idx = 0; idx < mapping.GetCount(); ++idx)
207  {
208  switch(mapping.m_valueType)
209  {
210  case BlobField32::Type_KyUInt32 : os << ((KyUInt32) (mapping.ValueAsUInt32(idx))); break;
211  case BlobField32::Type_KyInt32 : os << ((KyInt32) (mapping.ValueAsInt32(idx))); break;
212  case BlobField32::Type_KyFloat32 : os << ((KyFloat32)(mapping.ValueAsFloat32(idx))); break;
213  }
214 
215  os << ", ";
216  }
217 
218  return os;
219 }
220 
221 
222 
223 class BlobMultiField32Builder : public BaseBlobBuilder<BlobMultiField32>
224 {
225 public:
226  BlobMultiField32Builder(BlobMultiField32Mapping& mapping) : m_mapping(&mapping) {}
227 
228 private:
229  virtual void DoBuild()
230  {
231  BLOB_STRING(m_blob->m_name, m_mapping->m_name);
232  BLOB_STRING(m_blob->m_category, m_mapping->m_category);
233  BLOB_SET(m_blob->m_valueType, m_mapping->m_valueType);
234 
235  KyUInt32 count = m_mapping->GetCount();
236  KyUInt32* blobValues = BLOB_ARRAY(m_blob->m_values, count);
237  if (IsWriteMode() == true)
238  {
239  for (KyUInt32 idx = 0; idx < count; ++idx)
240  {
241  blobValues[idx] = m_mapping->ValueAsUInt32(idx);
242  }
243  }
244  }
245 
246  BlobMultiField32Mapping* m_mapping;
247 };
248 
249 
250 }
251 
252 
253 
#define BLOB_SET(blob, value)
Use this macro only in implementations of BaseBlobBuilder::DoBuild().
Definition: baseblobbuilder.h:130
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
Target
Enumerates the possible endianness types relative to the current platform.
Definition: endianness.h:27
BlobField32 * m_blob
The blob maintained by this builder. Only modify using the macros listed under DoBuild().
Definition: baseblobbuilder.h:113
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
bool IsWriteMode()
Indicates whether the builder is operating in COUNT mode or in WRITE mode.
Definition: baseblobbuilder.h:43
#define BLOB_STRING(str, src)
Use this macro only in implementations of BaseBlobBuilder::DoBuild().
Definition: baseblobbuilder.h:166
std::int32_t KyInt32
int32_t
Definition: types.h:24
#define BLOB_ARRAY(blobArray, count)
Use this macro only in implementations of BaseBlobBuilder::DoBuild().
Definition: baseblobbuilder.h:139
float KyFloat32
float
Definition: types.h:32