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;
27
28enum class DatumType : uint8_t
29{
30 Integer,
31 Float,
32 Bool,
33 String,
35 Vector,
36 Color,
37 Asset,
38 Byte,
39 Table,
40 Node,
41 Short,
43 Node3D,
44 Widget,
45 Text,
46 Quad,
47 Audio3D,
48 Scene,
52
53 // Widget subtypes
54 SpinBox,
55 Window,
64 Button,
65 Slider,
67 Canvas,
69
70 // Node3D subtypes
71 Voxel3D,
77 Box3D,
79
80 // Node subtypes
83
84 // Asset subtypes
86 TileSet,
87 TileMap,
90
91 // Animation structs
93
94 Count
95};
96
97inline bool IsNodeDatumType(DatumType type)
98{
99 return type == DatumType::Node ||
100 type == DatumType::Node3D ||
101 type == DatumType::Audio3D ||
102 type == DatumType::Widget ||
103 type == DatumType::Text ||
104 type == DatumType::Quad ||
105 type == DatumType::Spline3D ||
106 // Widget subtypes
107 type == DatumType::SpinBox ||
108 type == DatumType::Window ||
109 type == DatumType::DialogWindow ||
110 type == DatumType::InputField ||
111 type == DatumType::ProgressBar ||
112 type == DatumType::CheckBox ||
116 type == DatumType::ArrayWidget ||
117 type == DatumType::Button ||
118 type == DatumType::Slider ||
119 type == DatumType::LineEdit ||
120 type == DatumType::Canvas ||
121 type == DatumType::ComboBox ||
122 // Node3D subtypes
123 type == DatumType::Voxel3D ||
124 type == DatumType::Terrain3D ||
125 type == DatumType::TileMap2D ||
126 type == DatumType::NavMesh3D ||
127 type == DatumType::Camera3D ||
129 type == DatumType::Box3D ||
130 type == DatumType::Particle3D ||
131 // Node subtypes
134}
135
137{
138 return type == DatumType::Asset ||
139 type == DatumType::Scene ||
140 type == DatumType::Material ||
141 type == DatumType::TileSet ||
142 type == DatumType::TileMap ||
143 type == DatumType::Timeline ||
145}
146
148{
149 int32_t* i;
150 float* f;
151 bool* b;
152 std::string* s;
153 glm::vec2* v2;
154 glm::vec3* v3;
155 glm::vec4* v4;
157 //ActorRef* ac;
158 uint8_t* by;
161 int16_t* sh;
165 void* vp;
166};
167
169{
170public:
171
172 Datum();
173 Datum(
174 DatumType type,
175 Object* owner,
176 void* data,
177 uint32_t count = 1u,
178 DatumChangeHandlerFP changeHandler = nullptr);
179 Datum(const Datum& src);
180 virtual ~Datum();
181
182 // Conversion constructors
183 Datum(int32_t value);
184#if PLATFORM_3DS || (defined(POLYPHASE_PLATFORM_ADDON) && !defined(POLYPHASE_ADDON_INT32_IS_INT))
185 // Toolchains where `int` and `int32_t` are distinct types (devkitARM/3DS,
186 // PSPSDK psp-gcc which makes int32_t a `long`, mips64-elf-gcc -mabi=o64
187 // which does the same) need an explicit Datum(int) overload so call
188 // sites like `Datum(0)` resolve unambiguously. On Windows/Linux/Android
189 // they're the same underlying type and the int32_t overload covers both.
190 // Addon toolchains where int32_t IS `int` (e.g. PS3 powerpc64-ps3-elf)
191 // define POLYPHASE_ADDON_INT32_IS_INT to opt out — the extra overload
192 // would otherwise collide with Datum(int32_t).
193 Datum(int value);
194#endif
195 Datum(uint32_t value);
196 Datum(float value);
197 Datum(bool value);
198 Datum(const char* value);
199 Datum(const std::string& value);
200 Datum(glm::vec2 value);
201 Datum(glm::vec3 value);
202 Datum(glm::vec4 value);
203 Datum(Asset* value);
204 Datum(const AssetRef& value);
205 Datum(uint8_t value);
206 Datum(const SharedPtr<Node>& value);
207 Datum(const WeakPtr<Node>& value);
208 Datum(Node* value);
209 Datum(int16_t value);
210 Datum(const ScriptFunc& func);
211 Datum(const TransformKeyframe& value);
212
213 template<typename T>
214 Datum(const std::vector<T>& arr)
215 {
216 Reset();
217
218 for (uint32_t i = 0; i < arr.size(); ++i)
219 {
220 PushBack(arr[i]);
221 }
222
223 mForceScriptArray = true;
224 }
225
226 template<typename T>
227 Datum(const std::vector<SharedPtr<T > >& arr)
228 {
229 Reset();
230
231 for (uint32_t i = 0; i < arr.size(); ++i)
232 {
233 PushBack(arr[i].Get());
234 }
235
236 mForceScriptArray = true;
237 }
238
239 template<typename T>
240 Datum(const std::vector<WeakPtr<T > >& arr)
241 {
242 Reset();
243
244 for (uint32_t i = 0; i < arr.size(); ++i)
245 {
246 PushBack(arr[i].Get());
247 }
248
249 mForceScriptArray = true;
250 }
251
252 // Conversion operators
253 operator int32_t() const { return GetInteger(); }
254 operator uint32_t() const { return (uint32_t)GetInteger(); }
255 operator float() const { return (mType == DatumType::Integer) ? float(GetInteger()) : GetFloat(); }
256 operator bool() const { return GetBool(); }
257 operator const char*() const { return GetString().c_str(); }
258 operator std::string() const { return GetString(); }
259 operator glm::vec2() const { return (mType == DatumType::Color) ? glm::vec2(GetColor()) : GetVector2D(); }
260 operator glm::vec3() const { return (mType == DatumType::Color) ? glm::vec3(GetColor()) : GetVector(); }
261 operator glm::vec4() const { return GetColor(); }
262 operator Asset*() const { return GetAsset(); }
263 //operator AssetRef() const; Not sure if needed??
264 operator uint8_t() const { return (mType == DatumType::Integer) ? uint8_t(GetInteger()) : GetByte(); }
265 operator WeakPtr<Node>() const { return GetNode(); }
266 operator int16_t() const { return (mType == DatumType::Integer) ? int16_t(GetInteger()) : GetShort(); }
267 // ScriptFunc conversion is defined as a constructor in ScriptFunc class
268
269 DatumType GetType() const;
270 void SetType(DatumType type);
271
272 uint32_t GetCount() const;
273 void SetCount(uint32_t count);
274
275 uint32_t GetDataTypeSize() const;
276 uint32_t GetDataTypeSerializationSize(bool net) const;
277
278 bool IsExternal() const;
279 bool IsValid() const;
280
281 virtual void ReadStream(Stream& stream, uint32_t version, bool net, bool external);
282 virtual void WriteStream(Stream& stream, bool net) const;
283 virtual uint32_t GetSerializationSize(bool net) const;
284
285 void SetInteger(int32_t value, uint32_t index = 0);
286 void SetFloat(float value, uint32_t index = 0);
287 void SetBool(bool value, uint32_t index = 0);
288 void SetString(const std::string& value, uint32_t index = 0);
289 void SetVector2D(const glm::vec2& value, uint32_t index = 0);
290 void SetVector(const glm::vec3& value, uint32_t index = 0);
291 void SetColor(const glm::vec4& value, uint32_t index = 0);
292 void SetAsset(const Asset* value, uint32_t index = 0);
293 void SetByte(uint8_t value, uint32_t index = 0);
294 void SetTableDatum(const TableDatum& value, uint32_t index = 0);
295 void SetNode(const WeakPtr<Node>& value, uint32_t index = 0);
296 void SetShort(int16_t value, uint32_t index = 0);
297 void SetFunction(const ScriptFunc& value, uint32_t index = 0);
298 void SetTransformKeyframe(const TransformKeyframe& value, uint32_t index = 0);
299
300 void SetValue(const void* value, uint32_t index = 0, uint32_t count = 1);
301 void SetValueRaw(const void* value, uint32_t index = 0);
302
303 void SetExternal(int32_t* data, uint32_t count = 1);
304 void SetExternal(float* data, uint32_t count = 1);
305 void SetExternal(bool* data, uint32_t count = 1);
306 void SetExternal(std::string* data, uint32_t count = 1);
307 void SetExternal(glm::vec2* data, uint32_t count = 1);
308 void SetExternal(glm::vec3* data, uint32_t count = 1);
309 void SetExternal(glm::vec4* data, uint32_t count = 1);
310 void SetExternal(AssetRef* data, uint32_t count = 1);
311 void SetExternal(uint8_t* data, uint32_t count = 1);
312 void SetExternal(TableDatum* data, uint32_t count = 1);
313 void SetExternal(WeakPtr<Node>* data, uint32_t count = 1);
314 void SetExternal(int16_t* data, uint32_t count = 1);
315 void SetExternal(ScriptFunc* data, uint32_t count = 1);
316 void SetExternal(TransformKeyframe* data, uint32_t count = 1);
317
318 int32_t GetInteger(uint32_t index = 0) const;
319 float GetFloat(uint32_t index = 0) const;
320 bool GetBool(uint32_t index = 0) const;
321 const std::string& GetString(uint32_t index = 0) const;
322 const glm::vec2& GetVector2D(uint32_t index = 0) const;
323 const glm::vec3& GetVector(uint32_t index = 0) const;
324 const glm::vec4& GetColor(uint32_t index = 0) const;
325 Asset* GetAsset(uint32_t index = 0) const;
326 uint8_t GetByte(uint32_t index = 0) const;
327 TableDatum& GetTableDatum(uint32_t index = 0);
328 const TableDatum& GetTableDatum(uint32_t index = 0) const;
329 WeakPtr<Node> GetNode(uint32_t index = 0) const;
330 WeakPtr<Node3D> GetNode3D(uint32_t index = 0) const;
331 int16_t GetShort(uint32_t index = 0) const;
332 const ScriptFunc& GetFunction(uint32_t index = 0) const;
333 const TransformKeyframe& GetTransformKeyframe(uint32_t index = 0) const;
334
335 int32_t& GetIntegerRef(uint32_t index = 0);
336 float& GetFloatRef(uint32_t index = 0);
337 bool& GetBoolRef(uint32_t index = 0);
338 std::string& GetStringRef(uint32_t index = 0);
339 glm::vec2& GetVector2DRef(uint32_t index = 0);
340 glm::vec3& GetVectorRef(uint32_t index = 0);
341 glm::vec4& GetColorRef(uint32_t index = 0);
342 AssetRef& GetAssetRef(uint32_t index = 0);
343 uint8_t& GetByteRef(uint32_t index = 0);
344 WeakPtr<Node>& GetNodeRef(uint32_t index = 0);
345 int16_t& GetShortRef(uint32_t index = 0);
346 ScriptFunc& GetFunctionRef(uint32_t index = 0);
347 TransformKeyframe& GetTransformKeyframeRef(uint32_t index = 0);
348
349 void PushBack(int32_t value);
350 void PushBack(float value);
351 void PushBack(bool value);
352 void PushBack(const std::string& value);
353 void PushBack(const char* value);
354 void PushBack(const glm::vec2& value);
355 void PushBack(const glm::vec3& value);
356 void PushBack(const glm::vec4& value);
357 void PushBack(Asset* value);
358 void PushBack(const AssetRef& value);
359 void PushBack(uint8_t value);
360 TableDatum* PushBackTableDatum(const TableDatum& value);
361 void PushBack(const SharedPtr<Node>& value);
362 void PushBack(const WeakPtr<Node>& value);
363 void PushBack(Node* node);
364 void PushBack(int16_t value);
365 void PushBack(const ScriptFunc& value);
366 void PushBack(const TransformKeyframe& value);
367
368 void Erase(uint32_t index);
369
370 TableDatum* FindTableDatum(const char* key);
371 TableDatum* FindTableDatum(int32_t key);
372 TableDatum* GetField(const char* key);
373 TableDatum* GetField(int32_t key);
374 TableDatum* AddTableField(int32_t key);
375 TableDatum* AddTableField(const char* key);
376
377 // Table get/set convenience functions
378 int32_t GetIntegerField(const char* key);
379 float GetFloatField(const char* key);
380 bool GetBoolField(const char* key);
381 std::string GetStringField(const char* key);
382 glm::vec2 GetVector2DField(const char* key);
383 glm::vec3 GetVectorField(const char* key);
384 glm::vec4 GetColorField(const char* key);
385 Asset* GetAssetField(const char* key);
386 WeakPtr<Node> GetNodeField(const char* key);
387 TableDatum& GetTableField(const char* key);
388 ScriptFunc GetFunctionField(const char* key);
389
390 int32_t GetIntegerField(int32_t key);
391 float GetFloatField(int32_t key);
392 bool GetBoolField(int32_t key);
393 std::string GetStringField(int32_t key);
394 glm::vec2 GetVector2DField(int32_t key);
395 glm::vec3 GetVectorField(int32_t key);
396 glm::vec4 GetColorField(int32_t key);
397 Asset* GetAssetField(int32_t key);
399 TableDatum& GetTableField(int32_t key);
401
402 void SetIntegerField(const char* key, int32_t value);
403 void SetFloatField(const char* key, float value);
404 void SetBoolField(const char* key, bool value);
405 void SetStringField(const char* key, const std::string& value);
406 void SetVector2DField(const char* key, glm::vec2 value);
407 void SetVectorField(const char* key, glm::vec3 value);
408 void SetColorField(const char* key, glm::vec4 value);
409 void SetAssetField(const char* key, Asset* value);
410 void SetNodeField(const char* key, const WeakPtr<Node>& value);
411 void SetTableField(const char* key, const TableDatum& value);
412 void SetFunctionField(const char* key, const ScriptFunc& value);
413
414 void SetIntegerField(int32_t key, int32_t value);
415 void SetFloatField(int32_t key, float value);
416 void SetBoolField(int32_t key, bool value);
417 void SetStringField(int32_t key, const std::string& value);
418 void SetVector2DField(int32_t key, glm::vec2 value);
419 void SetVectorField(int32_t key, glm::vec3 value);
420 void SetColorField(int32_t key, glm::vec4 value);
421 void SetAssetField(int32_t key, Asset* value);
422 void SetNodeField(int32_t key, const WeakPtr<Node>& value);
423 void SetTableField(int32_t key, const TableDatum& value);
424 void SetFunctionField(int32_t key, const ScriptFunc& value);
425
426 bool HasField(const char* key);
427 bool HasField(int32_t key);
428
429 // Assignment
430 Datum& operator=(const Datum& src);
431
432 Datum& operator=(int32_t src);
433 Datum& operator=(float src);
434 Datum& operator=(bool src);
435 Datum& operator=(const std::string& src);
436 Datum& operator=(const char* src);
437 Datum& operator=(const glm::vec2 src);
438 Datum& operator=(const glm::vec3& src);
439 Datum& operator=(const glm::vec4& src);
440 Datum& operator=(Asset* src);
441 Datum& operator=(uint8_t src);
442 //Datum& operator=(Node* src);
443 Datum& operator=(const WeakPtr<Node>& src);
444 Datum& operator=(int16_t src);
445 Datum& operator=(const ScriptFunc& src);
446
447 // Equivalence
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 bool operator!=(const Datum& other) const;
468
469 bool operator!=(const int32_t& other) const;
470 bool operator!=(const float& other) const;
471 bool operator!=(const bool& other) const;
472 bool operator!=(const std::string& other) const;
473 bool operator!=(const char* other) const;
474 bool operator!=(const glm::vec2& other) const;
475 bool operator!=(const glm::vec3& other) const;
476 bool operator!=(const glm::vec4& other) const;
477 bool operator!=(const Asset*& other) const;
478 bool operator!=(const uint32_t& other) const;
479 bool operator!=(const uint8_t& other) const;
480 bool operator!=(const Node*& other) const;
481 bool operator!=(const WeakPtr<Node>& other) const;
482 bool operator!=(const SharedPtr<Node>& other) const;
483 bool operator!=(const int16_t& other) const;
484 bool operator!=(const ScriptFunc& other) const;
485
486 virtual bool IsProperty() const;
487 virtual bool IsTableDatum() const;
488 virtual void DeepCopy(const Datum& src, bool forceInternalStorage);
489
490 void* GetValue(uint32_t index);
491 virtual void Destroy();
492
493 static const Datum sNullDatum;
494
495protected:
496
497 void Reserve(uint32_t capacity);
498
499 void PreSet(uint32_t index, DatumType type);
500 void PreSetExternal(DatumType type);
501 void PostSetExternal(DatumType type, uint32_t count);
502 void PrePushBack(DatumType type);
503 void PreGet(uint32_t index, DatumType type) const;
504 void PreAssign(DatumType type);
505
506 void ConstructData(DatumData& dataUnion, uint32_t index);
507 void DestructData(DatumData& dataUnion, uint32_t index);
508 void CopyData(DatumData& dst, uint32_t dstIndex, DatumData& src, uint32_t srcIndex);
509
510 virtual void Reset();
511
512public:
513
515 bool mExternal : 1;
517 bool mIsNetDatum : 1;
518 Object* mOwner = nullptr;
519 DatumData mData = {};
520 DatumChangeHandlerFP mChangeHandler = nullptr;
521 uint8_t mCount = 0;
522 uint8_t mCapacity = 0;
523};
DatumType
Definition Datum.h:29
@ DirectionalLight3D
@ TransformKeyframe
@ ListViewItemWidget
@ DebugResourcesWidget
bool(* DatumChangeHandlerFP)(class Datum *prop, uint32_t index, const void *newValue)
Definition Datum.h:17
bool IsNodeDatumType(DatumType type)
Definition Datum.h:97
bool IsAssetDatumType(DatumType type)
Definition Datum.h:136
Export macros for Polyphase Engine symbols.
#define POLYPHASE_API
Definition PolyphaseAPI.h:31
Definition AssetRef.h:18
Definition Asset.h:120
Definition Datum.h:169
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:227
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:515
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:240
bool mForceScriptArray
Definition Datum.h:516
WeakPtr< Node3D > GetNode3D(uint32_t index=0) const
glm::vec4 GetColorField(int32_t key)
bool mIsNetDatum
Definition Datum.h:517
Datum(const std::vector< T > &arr)
Definition Datum.h:214
static const Datum sNullDatum
Definition Datum.h:493
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 TimelineTypes.h:18
Definition Datum.h:148
TableDatum * t
Definition Datum.h:159
ScriptFunc * fn
Definition Datum.h:162
void * vp
Definition Datum.h:165
PointCloud ** pc
Definition Datum.h:163
uint8_t * by
Definition Datum.h:158
float * f
Definition Datum.h:150
bool * b
Definition Datum.h:151
TransformKeyframe * tk
Definition Datum.h:164
WeakPtr< Node > * n
Definition Datum.h:160
glm::vec3 * v3
Definition Datum.h:154
AssetRef * as
Definition Datum.h:156
std::string * s
Definition Datum.h:152
glm::vec4 * v4
Definition Datum.h:155
glm::vec2 * v2
Definition Datum.h:153
int32_t * i
Definition Datum.h:149
int16_t * sh
Definition Datum.h:161