Polyphase Game Engine
Loading...
Searching...
No Matches
Datum.h
Go to the documentation of this file.
1#pragma once
2
3#include "PolyphaseAPI.h"
4#include "Stream.h"
5#include "EngineTypes.h"
6#include "Maths.h"
7
8#include <string>
9
10// Conflict with Windows preprocessor
11#ifdef GetObject
12#undef GetObject
13#endif
14
15#define NULL_DATUM Datum::sNullDatum
16
17typedef bool(*DatumChangeHandlerFP)(class Datum* prop, uint32_t index, const void* newValue);
18
19class Asset;
20class AssetRef;
21class TableDatum;
22class ScriptFunc;
23class Object;
24class Node;
25class PointCloud;
26
27enum class DatumType : uint8_t
28{
29 Integer,
30 Float,
31 Bool,
32 String,
34 Vector,
35 Color,
36 Asset,
37 Byte,
38 Table,
39 Node,
40 Short,
42 Node3D,
43 Widget,
44 Text,
45 Quad,
46 Audio3D,
47 Scene,
51
52 // Widget subtypes
53 SpinBox,
54 Window,
63 Button,
64 Slider,
66 Canvas,
68
69 // Node3D subtypes
70 Voxel3D,
76 Box3D,
78
79 // Node subtypes
82
83 // Asset subtypes
85 TileSet,
86 TileMap,
89
90 Count
91};
92
93inline bool IsNodeDatumType(DatumType type)
94{
95 return type == DatumType::Node ||
96 type == DatumType::Node3D ||
97 type == DatumType::Audio3D ||
98 type == DatumType::Widget ||
99 type == DatumType::Text ||
100 type == DatumType::Quad ||
101 type == DatumType::Spline3D ||
102 // Widget subtypes
103 type == DatumType::SpinBox ||
104 type == DatumType::Window ||
105 type == DatumType::DialogWindow ||
106 type == DatumType::InputField ||
107 type == DatumType::ProgressBar ||
108 type == DatumType::CheckBox ||
112 type == DatumType::ArrayWidget ||
113 type == DatumType::Button ||
114 type == DatumType::Slider ||
115 type == DatumType::LineEdit ||
116 type == DatumType::Canvas ||
117 type == DatumType::ComboBox ||
118 // Node3D subtypes
119 type == DatumType::Voxel3D ||
120 type == DatumType::Terrain3D ||
121 type == DatumType::TileMap2D ||
122 type == DatumType::NavMesh3D ||
123 type == DatumType::Camera3D ||
125 type == DatumType::Box3D ||
126 type == DatumType::Particle3D ||
127 // Node subtypes
130}
131
133{
134 return type == DatumType::Asset ||
135 type == DatumType::Scene ||
136 type == DatumType::Material ||
137 type == DatumType::TileSet ||
138 type == DatumType::TileMap ||
139 type == DatumType::Timeline ||
141}
142
144{
145 int32_t* i;
146 float* f;
147 bool* b;
148 std::string* s;
149 glm::vec2* v2;
150 glm::vec3* v3;
151 glm::vec4* v4;
153 //ActorRef* ac;
154 uint8_t* by;
157 int16_t* sh;
160 void* vp;
161};
162
164{
165public:
166
167 Datum();
168 Datum(
169 DatumType type,
170 Object* owner,
171 void* data,
172 uint32_t count = 1u,
173 DatumChangeHandlerFP changeHandler = nullptr);
174 Datum(const Datum& src);
175 virtual ~Datum();
176
177 // Conversion constructors
178 Datum(int32_t value);
179#if PLATFORM_3DS
180 Datum(int value);
181#endif
182 Datum(uint32_t value);
183 Datum(float value);
184 Datum(bool value);
185 Datum(const char* value);
186 Datum(const std::string& value);
187 Datum(glm::vec2 value);
188 Datum(glm::vec3 value);
189 Datum(glm::vec4 value);
190 Datum(Asset* value);
191 Datum(const AssetRef& value);
192 Datum(uint8_t value);
193 Datum(const SharedPtr<Node>& value);
194 Datum(const WeakPtr<Node>& value);
195 Datum(Node* value);
196 Datum(int16_t value);
197 Datum(const ScriptFunc& func);
198
199 template<typename T>
200 Datum(const std::vector<T>& arr)
201 {
202 Reset();
203
204 for (uint32_t i = 0; i < arr.size(); ++i)
205 {
206 PushBack(arr[i]);
207 }
208
209 mForceScriptArray = true;
210 }
211
212 template<typename T>
213 Datum(const std::vector<SharedPtr<T > >& arr)
214 {
215 Reset();
216
217 for (uint32_t i = 0; i < arr.size(); ++i)
218 {
219 PushBack(arr[i].Get());
220 }
221
222 mForceScriptArray = true;
223 }
224
225 template<typename T>
226 Datum(const std::vector<WeakPtr<T > >& arr)
227 {
228 Reset();
229
230 for (uint32_t i = 0; i < arr.size(); ++i)
231 {
232 PushBack(arr[i].Get());
233 }
234
235 mForceScriptArray = true;
236 }
237
238 // Conversion operators
239 operator int32_t() const { return GetInteger(); }
240 operator uint32_t() const { return (uint32_t)GetInteger(); }
241 operator float() const { return (mType == DatumType::Integer) ? float(GetInteger()) : GetFloat(); }
242 operator bool() const { return GetBool(); }
243 operator const char*() const { return GetString().c_str(); }
244 operator std::string() const { return GetString(); }
245 operator glm::vec2() const { return (mType == DatumType::Color) ? glm::vec2(GetColor()) : GetVector2D(); }
246 operator glm::vec3() const { return (mType == DatumType::Color) ? glm::vec3(GetColor()) : GetVector(); }
247 operator glm::vec4() const { return GetColor(); }
248 operator Asset*() const { return GetAsset(); }
249 //operator AssetRef() const; Not sure if needed??
250 operator uint8_t() const { return (mType == DatumType::Integer) ? uint8_t(GetInteger()) : GetByte(); }
251 operator WeakPtr<Node>() const { return GetNode(); }
252 operator int16_t() const { return (mType == DatumType::Integer) ? int16_t(GetInteger()) : GetShort(); }
253 // ScriptFunc conversion is defined as a constructor in ScriptFunc class
254
255 DatumType GetType() const;
256 void SetType(DatumType type);
257
258 uint32_t GetCount() const;
259 void SetCount(uint32_t count);
260
261 uint32_t GetDataTypeSize() const;
262 uint32_t GetDataTypeSerializationSize(bool net) const;
263
264 bool IsExternal() const;
265 bool IsValid() const;
266
267 virtual void ReadStream(Stream& stream, uint32_t version, bool net, bool external);
268 virtual void WriteStream(Stream& stream, bool net) const;
269 virtual uint32_t GetSerializationSize(bool net) const;
270
271 void SetInteger(int32_t value, uint32_t index = 0);
272 void SetFloat(float value, uint32_t index = 0);
273 void SetBool(bool value, uint32_t index = 0);
274 void SetString(const std::string& value, uint32_t index = 0);
275 void SetVector2D(const glm::vec2& value, uint32_t index = 0);
276 void SetVector(const glm::vec3& value, uint32_t index = 0);
277 void SetColor(const glm::vec4& value, uint32_t index = 0);
278 void SetAsset(const Asset* value, uint32_t index = 0);
279 void SetByte(uint8_t value, uint32_t index = 0);
280 void SetTableDatum(const TableDatum& value, uint32_t index = 0);
281 void SetNode(const WeakPtr<Node>& value, uint32_t index = 0);
282 void SetShort(int16_t value, uint32_t index = 0);
283 void SetFunction(const ScriptFunc& value, uint32_t index = 0);
284
285 void SetValue(const void* value, uint32_t index = 0, uint32_t count = 1);
286 void SetValueRaw(const void* value, uint32_t index = 0);
287
288 void SetExternal(int32_t* data, uint32_t count = 1);
289 void SetExternal(float* data, uint32_t count = 1);
290 void SetExternal(bool* data, uint32_t count = 1);
291 void SetExternal(std::string* data, uint32_t count = 1);
292 void SetExternal(glm::vec2* data, uint32_t count = 1);
293 void SetExternal(glm::vec3* data, uint32_t count = 1);
294 void SetExternal(glm::vec4* data, uint32_t count = 1);
295 void SetExternal(AssetRef* data, uint32_t count = 1);
296 void SetExternal(uint8_t* data, uint32_t count = 1);
297 void SetExternal(TableDatum* data, uint32_t count = 1);
298 void SetExternal(WeakPtr<Node>* data, uint32_t count = 1);
299 void SetExternal(int16_t* data, uint32_t count = 1);
300 void SetExternal(ScriptFunc* data, uint32_t count = 1);
301
302 int32_t GetInteger(uint32_t index = 0) const;
303 float GetFloat(uint32_t index = 0) const;
304 bool GetBool(uint32_t index = 0) const;
305 const std::string& GetString(uint32_t index = 0) const;
306 const glm::vec2& GetVector2D(uint32_t index = 0) const;
307 const glm::vec3& GetVector(uint32_t index = 0) const;
308 const glm::vec4& GetColor(uint32_t index = 0) const;
309 Asset* GetAsset(uint32_t index = 0) const;
310 uint8_t GetByte(uint32_t index = 0) const;
311 TableDatum& GetTableDatum(uint32_t index = 0);
312 const TableDatum& GetTableDatum(uint32_t index = 0) const;
313 WeakPtr<Node> GetNode(uint32_t index = 0) const;
314 WeakPtr<Node3D> GetNode3D(uint32_t index = 0) const;
315 int16_t GetShort(uint32_t index = 0) const;
316 const ScriptFunc& GetFunction(uint32_t index = 0) const;
317
318 int32_t& GetIntegerRef(uint32_t index = 0);
319 float& GetFloatRef(uint32_t index = 0);
320 bool& GetBoolRef(uint32_t index = 0);
321 std::string& GetStringRef(uint32_t index = 0);
322 glm::vec2& GetVector2DRef(uint32_t index = 0);
323 glm::vec3& GetVectorRef(uint32_t index = 0);
324 glm::vec4& GetColorRef(uint32_t index = 0);
325 AssetRef& GetAssetRef(uint32_t index = 0);
326 uint8_t& GetByteRef(uint32_t index = 0);
327 WeakPtr<Node>& GetNodeRef(uint32_t index = 0);
328 int16_t& GetShortRef(uint32_t index = 0);
329 ScriptFunc& GetFunctionRef(uint32_t index = 0);
330
331 void PushBack(int32_t value);
332 void PushBack(float value);
333 void PushBack(bool value);
334 void PushBack(const std::string& value);
335 void PushBack(const char* value);
336 void PushBack(const glm::vec2& value);
337 void PushBack(const glm::vec3& value);
338 void PushBack(const glm::vec4& value);
339 void PushBack(Asset* value);
340 void PushBack(const AssetRef& value);
341 void PushBack(uint8_t value);
342 TableDatum* PushBackTableDatum(const TableDatum& value);
343 void PushBack(const SharedPtr<Node>& value);
344 void PushBack(const WeakPtr<Node>& value);
345 void PushBack(Node* node);
346 void PushBack(int16_t value);
347 void PushBack(const ScriptFunc& value);
348
349 void Erase(uint32_t index);
350
351 TableDatum* FindTableDatum(const char* key);
352 TableDatum* FindTableDatum(int32_t key);
353 TableDatum* GetField(const char* key);
354 TableDatum* GetField(int32_t key);
355 TableDatum* AddTableField(int32_t key);
356 TableDatum* AddTableField(const char* key);
357
358 // Table get/set convenience functions
359 int32_t GetIntegerField(const char* key);
360 float GetFloatField(const char* key);
361 bool GetBoolField(const char* key);
362 std::string GetStringField(const char* key);
363 glm::vec2 GetVector2DField(const char* key);
364 glm::vec3 GetVectorField(const char* key);
365 glm::vec4 GetColorField(const char* key);
366 Asset* GetAssetField(const char* key);
367 WeakPtr<Node> GetNodeField(const char* key);
368 TableDatum& GetTableField(const char* key);
369 ScriptFunc GetFunctionField(const char* key);
370
371 int32_t GetIntegerField(int32_t key);
372 float GetFloatField(int32_t key);
373 bool GetBoolField(int32_t key);
374 std::string GetStringField(int32_t key);
375 glm::vec2 GetVector2DField(int32_t key);
376 glm::vec3 GetVectorField(int32_t key);
377 glm::vec4 GetColorField(int32_t key);
378 Asset* GetAssetField(int32_t key);
380 TableDatum& GetTableField(int32_t key);
382
383 void SetIntegerField(const char* key, int32_t value);
384 void SetFloatField(const char* key, float value);
385 void SetBoolField(const char* key, bool value);
386 void SetStringField(const char* key, const std::string& value);
387 void SetVector2DField(const char* key, glm::vec2 value);
388 void SetVectorField(const char* key, glm::vec3 value);
389 void SetColorField(const char* key, glm::vec4 value);
390 void SetAssetField(const char* key, Asset* value);
391 void SetNodeField(const char* key, const WeakPtr<Node>& value);
392 void SetTableField(const char* key, const TableDatum& value);
393 void SetFunctionField(const char* key, const ScriptFunc& value);
394
395 void SetIntegerField(int32_t key, int32_t value);
396 void SetFloatField(int32_t key, float value);
397 void SetBoolField(int32_t key, bool value);
398 void SetStringField(int32_t key, const std::string& value);
399 void SetVector2DField(int32_t key, glm::vec2 value);
400 void SetVectorField(int32_t key, glm::vec3 value);
401 void SetColorField(int32_t key, glm::vec4 value);
402 void SetAssetField(int32_t key, Asset* value);
403 void SetNodeField(int32_t key, const WeakPtr<Node>& value);
404 void SetTableField(int32_t key, const TableDatum& value);
405 void SetFunctionField(int32_t key, const ScriptFunc& value);
406
407 bool HasField(const char* key);
408 bool HasField(int32_t key);
409
410 // Assignment
411 Datum& operator=(const Datum& src);
412
413 Datum& operator=(int32_t src);
414 Datum& operator=(float src);
415 Datum& operator=(bool src);
416 Datum& operator=(const std::string& src);
417 Datum& operator=(const char* src);
418 Datum& operator=(const glm::vec2 src);
419 Datum& operator=(const glm::vec3& src);
420 Datum& operator=(const glm::vec4& src);
421 Datum& operator=(Asset* src);
422 Datum& operator=(uint8_t src);
423 //Datum& operator=(Node* src);
424 Datum& operator=(const WeakPtr<Node>& src);
425 Datum& operator=(int16_t src);
426 Datum& operator=(const ScriptFunc& src);
427
428 // Equivalence
429 bool operator==(const Datum& other) const;
430
431 bool operator==(const int32_t& other) const;
432 bool operator==(const float& other) const;
433 bool operator==(const bool& other) const;
434 bool operator==(const std::string& other) const;
435 bool operator==(const char* other) const;
436 bool operator==(const glm::vec2& other) const;
437 bool operator==(const glm::vec3& other) const;
438 bool operator==(const glm::vec4& other) const;
439 bool operator==(const Asset*& other) const;
440 bool operator==(const uint32_t& other) const;
441 bool operator==(const uint8_t& other) const;
442 bool operator==(const Node*& other) const;
443 bool operator==(const WeakPtr<Node>& other) const;
444 bool operator==(const SharedPtr<Node>& other) const;
445 bool operator==(const int16_t& other) const;
446 bool operator==(const ScriptFunc& other) const;
447
448 bool operator!=(const Datum& other) const;
449
450 bool operator!=(const int32_t& other) const;
451 bool operator!=(const float& other) const;
452 bool operator!=(const bool& other) const;
453 bool operator!=(const std::string& other) const;
454 bool operator!=(const char* other) const;
455 bool operator!=(const glm::vec2& other) const;
456 bool operator!=(const glm::vec3& other) const;
457 bool operator!=(const glm::vec4& other) const;
458 bool operator!=(const Asset*& other) const;
459 bool operator!=(const uint32_t& other) const;
460 bool operator!=(const uint8_t& other) const;
461 bool operator!=(const Node*& other) const;
462 bool operator!=(const WeakPtr<Node>& other) const;
463 bool operator!=(const SharedPtr<Node>& other) const;
464 bool operator!=(const int16_t& other) const;
465 bool operator!=(const ScriptFunc& other) const;
466
467 virtual bool IsProperty() const;
468 virtual bool IsTableDatum() const;
469 virtual void DeepCopy(const Datum& src, bool forceInternalStorage);
470
471 void* GetValue(uint32_t index);
472 virtual void Destroy();
473
474 static const Datum sNullDatum;
475
476protected:
477
478 void Reserve(uint32_t capacity);
479
480 void PreSet(uint32_t index, DatumType type);
481 void PreSetExternal(DatumType type);
482 void PostSetExternal(DatumType type, uint32_t count);
483 void PrePushBack(DatumType type);
484 void PreGet(uint32_t index, DatumType type) const;
485 void PreAssign(DatumType type);
486
487 void ConstructData(DatumData& dataUnion, uint32_t index);
488 void DestructData(DatumData& dataUnion, uint32_t index);
489 void CopyData(DatumData& dst, uint32_t dstIndex, DatumData& src, uint32_t srcIndex);
490
491 virtual void Reset();
492
493public:
494
496 bool mExternal : 1;
498 bool mIsNetDatum : 1;
499 Object* mOwner = nullptr;
500 DatumData mData = {};
501 DatumChangeHandlerFP mChangeHandler = nullptr;
502 uint8_t mCount = 0;
503 uint8_t mCapacity = 0;
504};
DatumType
Definition Datum.h:28
@ DirectionalLight3D
@ ListViewItemWidget
@ DebugResourcesWidget
bool(* DatumChangeHandlerFP)(class Datum *prop, uint32_t index, const void *newValue)
Definition Datum.h:17
bool IsNodeDatumType(DatumType type)
Definition Datum.h:93
bool IsAssetDatumType(DatumType type)
Definition Datum.h:132
Export macros for Polyphase Engine symbols.
#define POLYPHASE_API
Definition PolyphaseAPI.h:31
Definition AssetRef.h:18
Definition Asset.h:113
Definition Datum.h:164
void SetFloatField(int32_t key, float value)
void SetAssetField(int32_t key, Asset *value)
void SetFunctionField(int32_t key, const ScriptFunc &value)
std::string GetStringField(int32_t key)
WeakPtr< Node > GetNodeField(int32_t key)
void SetVectorField(const char *key, glm::vec3 value)
void SetIntegerField(const char *key, int32_t value)
Datum(const std::vector< SharedPtr< T > > &arr)
Definition Datum.h:213
void SetNodeField(int32_t key, const WeakPtr< Node > &value)
glm::vec3 GetVectorField(int32_t key)
void SetFloatField(const char *key, float value)
void SetColorField(const char *key, glm::vec4 value)
glm::vec4 GetColorField(const char *key)
glm::vec3 GetVectorField(const char *key)
void SetNodeField(const char *key, const WeakPtr< Node > &value)
glm::vec2 GetVector2DField(int32_t key)
bool mExternal
Definition Datum.h:496
void SetStringField(int32_t key, const std::string &value)
WeakPtr< Node > GetNodeField(const char *key)
ScriptFunc GetFunctionField(int32_t key)
void SetStringField(const char *key, const std::string &value)
std::string GetStringField(const char *key)
void SetIntegerField(int32_t key, int32_t value)
ScriptFunc GetFunctionField(const char *key)
glm::vec2 GetVector2DField(const char *key)
void SetBoolField(const char *key, bool value)
bool GetBoolField(const char *key)
Asset * GetAssetField(int32_t key)
bool GetBoolField(int32_t key)
void SetFunctionField(const char *key, const ScriptFunc &value)
void SetVectorField(int32_t key, glm::vec3 value)
Asset * GetAssetField(const char *key)
void SetVector2DField(int32_t key, glm::vec2 value)
void SetAssetField(const char *key, Asset *value)
void SetBoolField(int32_t key, bool value)
Datum(const std::vector< WeakPtr< T > > &arr)
Definition Datum.h:226
bool mForceScriptArray
Definition Datum.h:497
WeakPtr< Node3D > GetNode3D(uint32_t index=0) const
glm::vec4 GetColorField(int32_t key)
bool mIsNetDatum
Definition Datum.h:498
Datum(const std::vector< T > &arr)
Definition Datum.h:200
static const Datum sNullDatum
Definition Datum.h:474
void SetVector2DField(const char *key, glm::vec2 value)
void SetColorField(int32_t key, glm::vec4 value)
Definition Node.h:67
Definition Object.h:13
Definition PointCloud.h:11
Definition ScriptFunc.h:10
Definition SmartPointer.h:33
Definition Stream.h:21
Definition TableDatum.h:8
Definition SmartPointer.h:312
Definition Datum.h:144
TableDatum * t
Definition Datum.h:155
ScriptFunc * fn
Definition Datum.h:158
void * vp
Definition Datum.h:160
PointCloud ** pc
Definition Datum.h:159
uint8_t * by
Definition Datum.h:154
float * f
Definition Datum.h:146
bool * b
Definition Datum.h:147
WeakPtr< Node > * n
Definition Datum.h:156
glm::vec3 * v3
Definition Datum.h:150
AssetRef * as
Definition Datum.h:152
std::string * s
Definition Datum.h:148
glm::vec4 * v4
Definition Datum.h:151
glm::vec2 * v2
Definition Datum.h:149
int32_t * i
Definition Datum.h:145
int16_t * sh
Definition Datum.h:157