gwnavruntime/blob/blobfield32.h Source File

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