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)
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 Datum(int value);
191#endif
192 Datum(uint32_t value);
193 Datum(float value);
194 Datum(bool value);
195 Datum(const char* value);
196 Datum(const std::string& value);
197 Datum(glm::vec2 value);
198 Datum(glm::vec3 value);
199 Datum(glm::vec4 value);
200 Datum(Asset* value);
201 Datum(const AssetRef& value);
202 Datum(uint8_t value);
203 Datum(const SharedPtr<Node>& value);
204 Datum(const WeakPtr<Node>& value);
205 Datum(Node* value);
206 Datum(int16_t value);
207 Datum(const ScriptFunc& func);
208 Datum(const TransformKeyframe& value);
209
210 template<typename T>
211 Datum(const std::vector<T>& arr)
212 {
213 Reset();
214
215 for (uint32_t i = 0; i < arr.size(); ++i)
216 {
217 PushBack(arr[i]);
218 }
219
220 mForceScriptArray = true;
221 }
222
223 template<typename T>
224 Datum(const std::vector<SharedPtr<T > >& arr)
225 {
226 Reset();
227
228 for (uint32_t i = 0; i < arr.size(); ++i)
229 {
230 PushBack(arr[i].Get());
231 }
232
233 mForceScriptArray = true;
234 }
235
236 template<typename T>
237 Datum(const std::vector<WeakPtr<T > >& arr)
238 {
239 Reset();
240
241 for (uint32_t i = 0; i < arr.size(); ++i)
242 {
243 PushBack(arr[i].Get());
244 }
245
246 mForceScriptArray = true;
247 }
248
249 // Conversion operators
250 operator int32_t() const { return GetInteger(); }
251 operator uint32_t() const { return (uint32_t)GetInteger(); }
252 operator float() const { return (mType == DatumType::Integer) ? float(GetInteger()) : GetFloat(); }
253 operator bool() const { return GetBool(); }
254 operator const char*() const { return GetString().c_str(); }
255 operator std::string() const { return GetString(); }
256 operator glm::vec2() const { return (mType == DatumType::Color) ? glm::vec2(GetColor()) : GetVector2D(); }
257 operator glm::vec3() const { return (mType == DatumType::Color) ? glm::vec3(GetColor()) : GetVector(); }
258 operator glm::vec4() const { return GetColor(); }
259 operator Asset*() const { return GetAsset(); }
260 //operator AssetRef() const; Not sure if needed??
261 operator uint8_t() const { return (mType == DatumType::Integer) ? uint8_t(GetInteger()) : GetByte(); }
262 operator WeakPtr<Node>() const { return GetNode(); }
263 operator int16_t() const { return (mType == DatumType::Integer) ? int16_t(GetInteger()) : GetShort(); }
264 // ScriptFunc conversion is defined as a constructor in ScriptFunc class
265
266 DatumType GetType() const;
267 void SetType(DatumType type);
268
269 uint32_t GetCount() const;
270 void SetCount(uint32_t count);
271
272 uint32_t GetDataTypeSize() const;
273 uint32_t GetDataTypeSerializationSize(bool net) const;
274
275 bool IsExternal() const;
276 bool IsValid() const;
277
278 virtual void ReadStream(Stream& stream, uint32_t version, bool net, bool external);
279 virtual void WriteStream(Stream& stream, bool net) const;
280 virtual uint32_t GetSerializationSize(bool net) const;
281
282 void SetInteger(int32_t value, uint32_t index = 0);
283 void SetFloat(float value, uint32_t index = 0);
284 void SetBool(bool value, uint32_t index = 0);
285 void SetString(const std::string& value, uint32_t index = 0);
286 void SetVector2D(const glm::vec2& value, uint32_t index = 0);
287 void SetVector(const glm::vec3& value, uint32_t index = 0);
288 void SetColor(const glm::vec4& value, uint32_t index = 0);
289 void SetAsset(const Asset* value, uint32_t index = 0);
290 void SetByte(uint8_t value, uint32_t index = 0);
291 void SetTableDatum(const TableDatum& value, uint32_t index = 0);
292 void SetNode(const WeakPtr<Node>& value, uint32_t index = 0);
293 void SetShort(int16_t value, uint32_t index = 0);
294 void SetFunction(const ScriptFunc& value, uint32_t index = 0);
295 void SetTransformKeyframe(const TransformKeyframe& value, uint32_t index = 0);
296
297 void SetValue(const void* value, uint32_t index = 0, uint32_t count = 1);
298 void SetValueRaw(const void* value, uint32_t index = 0);
299
300 void SetExternal(int32_t* data, uint32_t count = 1);
301 void SetExternal(float* data, uint32_t count = 1);
302 void SetExternal(bool* data, uint32_t count = 1);
303 void SetExternal(std::string* data, uint32_t count = 1);
304 void SetExternal(glm::vec2* data, uint32_t count = 1);
305 void SetExternal(glm::vec3* data, uint32_t count = 1);
306 void SetExternal(glm::vec4* data, uint32_t count = 1);
307 void SetExternal(AssetRef* data, uint32_t count = 1);
308 void SetExternal(uint8_t* data, uint32_t count = 1);
309 void SetExternal(TableDatum* data, uint32_t count = 1);
310 void SetExternal(WeakPtr<Node>* data, uint32_t count = 1);
311 void SetExternal(int16_t* data, uint32_t count = 1);
312 void SetExternal(ScriptFunc* data, uint32_t count = 1);
313 void SetExternal(TransformKeyframe* data, uint32_t count = 1);
314
315 int32_t GetInteger(uint32_t index = 0) const;
316 float GetFloat(uint32_t index = 0) const;
317 bool GetBool(uint32_t index = 0) const;
318 const std::string& GetString(uint32_t index = 0) const;
319 const glm::vec2& GetVector2D(uint32_t index = 0) const;
320 const glm::vec3& GetVector(uint32_t index = 0) const;
321 const glm::vec4& GetColor(uint32_t index = 0) const;
322 Asset* GetAsset(uint32_t index = 0) const;
323 uint8_t GetByte(uint32_t index = 0) const;
324 TableDatum& GetTableDatum(uint32_t index = 0);
325 const TableDatum& GetTableDatum(uint32_t index = 0) const;
326 WeakPtr<Node> GetNode(uint32_t index = 0) const;
327 WeakPtr<Node3D> GetNode3D(uint32_t index = 0) const;
328 int16_t GetShort(uint32_t index = 0) const;
329 const ScriptFunc& GetFunction(uint32_t index = 0) const;
330 const TransformKeyframe& GetTransformKeyframe(uint32_t index = 0) const;
331
332 int32_t& GetIntegerRef(uint32_t index = 0);
333 float& GetFloatRef(uint32_t index = 0);
334 bool& GetBoolRef(uint32_t index = 0);
335 std::string& GetStringRef(uint32_t index = 0);
336 glm::vec2& GetVector2DRef(uint32_t index = 0);
337 glm::vec3& GetVectorRef(uint32_t index = 0);
338 glm::vec4& GetColorRef(uint32_t index = 0);
339 AssetRef& GetAssetRef(uint32_t index = 0);
340 uint8_t& GetByteRef(uint32_t index = 0);
341 WeakPtr<Node>& GetNodeRef(uint32_t index = 0);
342 int16_t& GetShortRef(uint32_t index = 0);
343 ScriptFunc& GetFunctionRef(uint32_t index = 0);
344 TransformKeyframe& GetTransformKeyframeRef(uint32_t index = 0);
345
346 void PushBack(int32_t value);
347 void PushBack(float value);
348 void PushBack(bool value);
349 void PushBack(const std::string& value);
350 void PushBack(const char* value);
351 void PushBack(const glm::vec2& value);
352 void PushBack(const glm::vec3& value);
353 void PushBack(const glm::vec4& value);
354 void PushBack(Asset* value);
355 void PushBack(const AssetRef& value);
356 void PushBack(uint8_t value);
357 TableDatum* PushBackTableDatum(const TableDatum& value);
358 void PushBack(const SharedPtr<Node>& value);
359 void PushBack(const WeakPtr<Node>& value);
360 void PushBack(Node* node);
361 void PushBack(int16_t value);
362 void PushBack(const ScriptFunc& value);
363 void PushBack(const TransformKeyframe& value);
364
365 void Erase(uint32_t index);
366
367 TableDatum* FindTableDatum(const char* key);
368 TableDatum* FindTableDatum(int32_t key);
369 TableDatum* GetField(const char* key);
370 TableDatum* GetField(int32_t key);
371 TableDatum* AddTableField(int32_t key);
372 TableDatum* AddTableField(const char* key);
373
374 // Table get/set convenience functions
375 int32_t GetIntegerField(const char* key);
376 float GetFloatField(const char* key);
377 bool GetBoolField(const char* key);
378 std::string GetStringField(const char* key);
379 glm::vec2 GetVector2DField(const char* key);
380 glm::vec3 GetVectorField(const char* key);
381 glm::vec4 GetColorField(const char* key);
382 Asset* GetAssetField(const char* key);
383 WeakPtr<Node> GetNodeField(const char* key);
384 TableDatum& GetTableField(const char* key);
385 ScriptFunc GetFunctionField(const char* key);
386
387 int32_t GetIntegerField(int32_t key);
388 float GetFloatField(int32_t key);
389 bool GetBoolField(int32_t key);
390 std::string GetStringField(int32_t key);
391 glm::vec2 GetVector2DField(int32_t key);
392 glm::vec3 GetVectorField(int32_t key);
393 glm::vec4 GetColorField(int32_t key);
394 Asset* GetAssetField(int32_t key);
396 TableDatum& GetTableField(int32_t key);
398
399 void SetIntegerField(const char* key, int32_t value);
400 void SetFloatField(const char* key, float value);
401 void SetBoolField(const char* key, bool value);
402 void SetStringField(const char* key, const std::string& value);
403 void SetVector2DField(const char* key, glm::vec2 value);
404 void SetVectorField(const char* key, glm::vec3 value);
405 void SetColorField(const char* key, glm::vec4 value);
406 void SetAssetField(const char* key, Asset* value);
407 void SetNodeField(const char* key, const WeakPtr<Node>& value);
408 void SetTableField(const char* key, const TableDatum& value);
409 void SetFunctionField(const char* key, const ScriptFunc& value);
410
411 void SetIntegerField(int32_t key, int32_t value);
412 void SetFloatField(int32_t key, float value);
413 void SetBoolField(int32_t key, bool value);
414 void SetStringField(int32_t key, const std::string& value);
415 void SetVector2DField(int32_t key, glm::vec2 value);
416 void SetVectorField(int32_t key, glm::vec3 value);
417 void SetColorField(int32_t key, glm::vec4 value);
418 void SetAssetField(int32_t key, Asset* value);
419 void SetNodeField(int32_t key, const WeakPtr<Node>& value);
420 void SetTableField(int32_t key, const TableDatum& value);
421 void SetFunctionField(int32_t key, const ScriptFunc& value);
422
423 bool HasField(const char* key);
424 bool HasField(int32_t key);
425
426 // Assignment
427 Datum& operator=(const Datum& src);
428
429 Datum& operator=(int32_t src);
430 Datum& operator=(float src);
431 Datum& operator=(bool src);
432 Datum& operator=(const std::string& src);
433 Datum& operator=(const char* src);
434 Datum& operator=(const glm::vec2 src);
435 Datum& operator=(const glm::vec3& src);
436 Datum& operator=(const glm::vec4& src);
437 Datum& operator=(Asset* src);
438 Datum& operator=(uint8_t src);
439 //Datum& operator=(Node* src);
440 Datum& operator=(const WeakPtr<Node>& src);
441 Datum& operator=(int16_t src);
442 Datum& operator=(const ScriptFunc& src);
443
444 // Equivalence
445 bool operator==(const Datum& other) const;
446
447 bool operator==(const int32_t& other) const;
448 bool operator==(const float& other) const;
449 bool operator==(const bool& other) const;
450 bool operator==(const std::string& other) const;
451 bool operator==(const char* other) const;
452 bool operator==(const glm::vec2& other) const;
453 bool operator==(const glm::vec3& other) const;
454 bool operator==(const glm::vec4& other) const;
455 bool operator==(const Asset*& other) const;
456 bool operator==(const uint32_t& other) const;
457 bool operator==(const uint8_t& other) const;
458 bool operator==(const Node*& other) const;
459 bool operator==(const WeakPtr<Node>& other) const;
460 bool operator==(const SharedPtr<Node>& other) const;
461 bool operator==(const int16_t& other) const;
462 bool operator==(const ScriptFunc& other) const;
463
464 bool operator!=(const Datum& other) const;
465
466 bool operator!=(const int32_t& other) const;
467 bool operator!=(const float& other) const;
468 bool operator!=(const bool& other) const;
469 bool operator!=(const std::string& other) const;
470 bool operator!=(const char* other) const;
471 bool operator!=(const glm::vec2& other) const;
472 bool operator!=(const glm::vec3& other) const;
473 bool operator!=(const glm::vec4& other) const;
474 bool operator!=(const Asset*& other) const;
475 bool operator!=(const uint32_t& other) const;
476 bool operator!=(const uint8_t& other) const;
477 bool operator!=(const Node*& other) const;
478 bool operator!=(const WeakPtr<Node>& other) const;
479 bool operator!=(const SharedPtr<Node>& other) const;
480 bool operator!=(const int16_t& other) const;
481 bool operator!=(const ScriptFunc& other) const;
482
483 virtual bool IsProperty() const;
484 virtual bool IsTableDatum() const;
485 virtual void DeepCopy(const Datum& src, bool forceInternalStorage);
486
487 void* GetValue(uint32_t index);
488 virtual void Destroy();
489
490 static const Datum sNullDatum;
491
492protected:
493
494 void Reserve(uint32_t capacity);
495
496 void PreSet(uint32_t index, DatumType type);
497 void PreSetExternal(DatumType type);
498 void PostSetExternal(DatumType type, uint32_t count);
499 void PrePushBack(DatumType type);
500 void PreGet(uint32_t index, DatumType type) const;
501 void PreAssign(DatumType type);
502
503 void ConstructData(DatumData& dataUnion, uint32_t index);
504 void DestructData(DatumData& dataUnion, uint32_t index);
505 void CopyData(DatumData& dst, uint32_t dstIndex, DatumData& src, uint32_t srcIndex);
506
507 virtual void Reset();
508
509public:
510
512 bool mExternal : 1;
514 bool mIsNetDatum : 1;
515 Object* mOwner = nullptr;
516 DatumData mData = {};
517 DatumChangeHandlerFP mChangeHandler = nullptr;
518 uint8_t mCount = 0;
519 uint8_t mCapacity = 0;
520};
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:119
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:224
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:512
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:237
bool mForceScriptArray
Definition Datum.h:513
WeakPtr< Node3D > GetNode3D(uint32_t index=0) const
glm::vec4 GetColorField(int32_t key)
bool mIsNetDatum
Definition Datum.h:514
Datum(const std::vector< T > &arr)
Definition Datum.h:211
static const Datum sNullDatum
Definition Datum.h:490
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