KarmaEngine
Game Engine for practical learning and research purposes
Loading...
Searching...
No Matches
Field.h
Go to the documentation of this file.
1
10
11#pragma once
12
13#include "UObjectGlobals.h"
14
15namespace Karma
16{
17 class UObject;
18 class UClass;
19 class FField;
20 class FFieldClass;
21
31 {
36 union FFieldObjectUnion
37 {
38 FField* Field;
39 UObject* Object;
40 } Container;
41
42 bool bIsUObject;
43
44 public:
45
52 : bIsUObject(false)
53 {
54 Container.Field = nullptr;
55 }
56
63 FFieldVariant(const FField* InField)
64 : bIsUObject(false)
65 {
66 Container.Field = const_cast<FField*>(InField);
67 }
68
75 template <
76 typename T,
77 decltype(ImplicitConv<const UObject*>(std::declval<T>()))* = nullptr
78 >
79 FFieldVariant(T&& InObject)
80 : bIsUObject(true)
81 {
82 Container.Object = const_cast<UObject*>(ImplicitConv<const UObject*>(InObject));
83 }
84
91 inline bool IsUObject() const
92 {
93 return bIsUObject;
94 }
95
102 inline bool IsValid() const
103 {
104 return !!Container.Object;
105 }
106
113 bool IsValidLowLevel() const;
114
121 inline operator bool() const
122 {
123 return IsValid();
124 }
125
129 bool IsA(const UClass* InClass) const;
130
134 bool IsA(const FFieldClass* InClass) const;
135
136 template <typename T>
137 bool IsA() const
138 {
139 static_assert(sizeof(T) > 0, "T must not be an incomplete type");
140 return IsA(T::StaticClass());
141 }
142
143 /*
144 template <typename T>
145 typename TEnableIf<TIsDerivedFrom<T, UObject>::IsDerived, T*>::Type Get() const
146 {
147 static_assert(sizeof(T) > 0, "T must not be an incomplete type");
148 if (IsA(T::StaticClass()))
149 {
150 return static_cast<T*>(Container.Object);
151 }
152 return nullptr;
153 }
154
155 template <typename T>
156 typename TEnableIf<!TIsDerivedFrom<T, UObject>::IsDerived, T*>::Type Get() const
157 {
158 static_assert(sizeof(T) > 0, "T must not be an incomplete type");
159 if (IsA(T::StaticClass()))
160 {
161 return static_cast<T*>(Container.Field);
162 }
163 return nullptr;
164 }
165 */
166
174 {
175 if (bIsUObject)
176 {
177 return Container.Object;
178 }
179 else
180 {
181 return nullptr;
182 }
183 }
184
192 {
193 if (!bIsUObject)
194 {
195 return Container.Field;
196 }
197 else
198 {
199 return nullptr;
200 }
201 }
202
210 {
211 return Container.Field;
212 }
213
220 {
221 return Container.Object;
222 }
223
229 void* GetRawPointer() const
230 {
231 return Container.Field;
232 }
233
234 FFieldVariant GetOwnerVariant() const;
235 UClass* GetOwnerClass() const;
236 const std::string& GetFullName() const;
237 const std::string& GetPathName() const;
238 const std::string& GetName() const;
239 const std::string& GetClassName() const;
240 const std::string& GetFName() const;
241 bool IsNative() const;
242 //UPackage* GetOutermost() const;
243
244 bool operator==(const FFieldVariant& Other) const
245 {
246 return Container.Field == Other.Container.Field;
247 }
248
249 bool operator!=(const FFieldVariant& Other) const
250 {
251 return Container.Field != Other.Container.Field;
252 }
253/*
254#if WITH_EDITORONLY_DATA
255 bool HasMetaData(const FName& Key) const;
256#endif*/
257
259 /*friend uint32_t GetTypeHash(const FFieldVariant& InFieldVariant)
260 {
261 return GetTypeHash(InFieldVariant.GetRawPointer());
262 }
263
264 KARMA_API friend FArchive& operator << (FArchive& Ar, FFieldVariant& InOutField);*/
265 };
266
272 {
274 std::string m_Name;
275
277 uint64_t m_Id;
278
280 uint64_t m_CastFlags;
281
283 EClassFlags ClassFlags;
284
286 FFieldClass* SuperClass;
287
289 FField* DefaultObject;
290
292 FField* (*ConstructFn)(const FFieldVariant&, const std::string&, EObjectFlags);
293
295 //FThreadSafeCounter UnqiueNameIndexCounter;
296
298 FField* ConstructDefaultObject();
299
300 public:
302
310
317 static std::unordered_map<std::string, FFieldClass*>& GetNameToFieldClassMap();
318
324 explicit FFieldClass(const char* InCPPName, uint64_t InId, uint64_t InCastFlags, FFieldClass* InSuperClass, FField* (*ConstructFnPtr)(const FFieldVariant&, const std::string&, EObjectFlags));
325
332
338 inline const std::string& GetName() const
339 {
340 return m_Name;
341 }
342
348 inline uint64_t GetId() const
349 {
350 return m_Id;
351 }
352
358 inline uint64_t GetCastFlags() const
359 {
360 return m_CastFlags;
361 }
362 inline bool HasAnyCastFlags(const uint64_t InCastFlags) const
363 {
364 return !!(m_CastFlags & InCastFlags);
365 }
366 inline bool HasAllCastFlags(const uint64_t InCastFlags) const
367 {
368 return (m_CastFlags & InCastFlags) == InCastFlags;
369 }
370
379 inline bool IsChildOf(const FFieldClass* InClass) const
380 {
381 const uint64_t OtherClassId = InClass->GetId();
382 return OtherClassId ? !!(m_CastFlags & OtherClassId) : IsChildOf_Walk(InClass);
383 }
384
385 const std::string& GetDescription() const;
386 const std::string& GetDisplayNameText() const;
387
388 FField* Construct(const FFieldVariant& InOwner, const std::string& InName, EObjectFlags InFlags = RF_NoFlags) const
389 {
390 return ConstructFn(InOwner, InName, InFlags);
391 }
392
393 FFieldClass* GetSuperClass() const
394 {
395 return SuperClass;
396 }
397
404 {
405 if (!DefaultObject)
406 {
407 DefaultObject = ConstructDefaultObject();
408 KR_CORE_ASSERT(DefaultObject, "Default object is null");
409 }
410 return DefaultObject;
411 }
412
413 /*
414 bool HasAnyClassFlags(EClassFlags FlagsToCheck) const
415 {
416 return EnumHasAnyFlags(ClassFlags, FlagsToCheck) != 0;
417 }
418
419 int32_t GetNextUniqueNameIndex()
420 {
421 return UnqiueNameIndexCounter.Increment();
422 }
423
424 friend FArchive& operator << (FArchive& Ar, FFieldClass& InField)
425 {
426 check(false);
427 return Ar;
428 }
429 KARMA_API friend FArchive& operator << (FArchive& Ar, FFieldClass*& InOutFieldClass);*/
430
431 private:
432 bool IsChildOf_Walk(const FFieldClass* InBaseClass) const
433 {
434 for (const FFieldClass* TempField = this; TempField; TempField = TempField->GetSuperClass())
435 {
436 if (TempField == InBaseClass)
437 {
438 return true;
439 }
440 }
441 return false;
442 }
443 };
444
449 {
451 FFieldClass* ClassPrivate;
452
453 public:
455
456 typedef FField Super;
457 typedef FField ThisClass;
458 typedef FField BaseFieldClass;
459 typedef FFieldClass FieldTypeClass;
460
463
466
468 std::string m_NamePrivate;
469
472
473 public:
479 void Rename(const std::string& NewName);
480
481 //static FFieldClass* StaticClass();
482 };
483}
#define KARMA_API
Defining Karma's API macro for storage class information.
Definition Core.h:41
#define KR_NONCOPYABLE(TypeName)
Makes a type non-copyable and non-movable by deleting copy/move constructors and assignment/move oper...
Definition Core.h:140
#define FORCEINLINE
Typical inlining macro for clarity.
Definition Core.h:170
This file contains some commonly used game code macros.
EObjectFlags
Flags describing an object instance.
Definition UObjectGlobals.h:183
@ RF_NoFlags
No flags, used to avoid a cast.
Definition UObjectGlobals.h:186
EClassFlags
Flags describing a class.
Definition UObjectGlobals.h:57
Object representing a type of an FField struct. Mimics a subset of UObject reflection functions.
Definition Field.h:272
uint64_t GetId() const
Definition Field.h:348
const std::string & GetName() const
Definition Field.h:338
static std::unordered_map< std::string, FFieldClass * > & GetNameToFieldClassMap()
bool IsChildOf(const FFieldClass *InClass) const
See if the class is child of a class.
Definition Field.h:379
uint64_t GetCastFlags() const
Definition Field.h:358
static KarmaVector< FFieldClass * > & GetAllFieldClasses()
FFieldClass(const char *InCPPName, uint64_t InId, uint64_t InCastFlags, FFieldClass *InSuperClass, FField *(*ConstructFnPtr)(const FFieldVariant &, const std::string &, EObjectFlags))
Initialize various data members of the class.
Definition Field.cpp:6
FField * GetDefaultObject()
Definition Field.h:403
Base class of reflection data objects.
Definition Field.h:449
FField * m_Next
Definition Field.h:465
void Rename(const std::string &NewName)
Rename the FField object.
Definition Field.cpp:21
std::string m_NamePrivate
Definition Field.h:468
EObjectFlags m_FlagsPrivate
Definition Field.h:471
FFieldVariant m_Owner
Definition Field.h:462
Special container that can hold either UObject or FField.
Definition Field.h:31
FORCEINLINE UObject * ToUObjectUnsafe() const
Definition Field.h:219
UObject * ToUObject() const
Getter for UObject if possible.
Definition Field.h:173
bool IsA(const UClass *InClass) const
bool IsValidLowLevel() const
FORCEINLINE FField * ToFieldUnsafe() const
Definition Field.h:209
bool IsValid() const
Maybe sees if UObject is being held.
Definition Field.h:102
bool IsA(const FFieldClass *InClass) const
void * GetRawPointer() const
Definition Field.h:229
FField * ToField() const
Getter for FField if possible.
Definition Field.h:191
FFieldVariant(T &&InObject)
Overloaded template constructor (UObject).
Definition Field.h:79
bool IsUObject() const
Getter for bIsUobject.
Definition Field.h:91
FFieldVariant(const FField *InField)
Overloaded constructor (FField).
Definition Field.h:63
FFieldVariant()
Constructor.
Definition Field.h:51
An object class.
Definition Class.h:158
The base class of all the game code relevant objects.
Definition Object.h:106
Karma's std::vector wrapper with additional functionalities.
Definition KarmaTypes.h:243