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 "krpch.h"
14#include "UObjectGlobals.h"
15
16namespace Karma
17{
18 class UObject;
19 class UClass;
20 class FField;
21 class FFieldClass;
22
32 {
37 union FFieldObjectUnion
38 {
39 FField* Field;
40 UObject* Object;
41 } Container;
42
43 bool bIsUObject;
44
45 public:
46
53 : bIsUObject(false)
54 {
55 Container.Field = nullptr;
56 }
57
64 FFieldVariant(const FField* InField)
65 : bIsUObject(false)
66 {
67 Container.Field = const_cast<FField*>(InField);
68 }
69
76 template <
77 typename T,
78 decltype(ImplicitConv<const UObject*>(std::declval<T>()))* = nullptr
79 >
80 FFieldVariant(T&& InObject)
81 : bIsUObject(true)
82 {
83 Container.Object = const_cast<UObject*>(ImplicitConv<const UObject*>(InObject));
84 }
85
92 inline bool IsUObject() const
93 {
94 return bIsUObject;
95 }
96
103 inline bool IsValid() const
104 {
105 return !!Container.Object;
106 }
107
114 bool IsValidLowLevel() const;
115
122 inline operator bool() const
123 {
124 return IsValid();
125 }
126
130 bool IsA(const UClass* InClass) const;
131
135 bool IsA(const FFieldClass* InClass) const;
136
137 template <typename T>
138 bool IsA() const
139 {
140 static_assert(sizeof(T) > 0, "T must not be an incomplete type");
141 return IsA(T::StaticClass());
142 }
143
144 /*
145 template <typename T>
146 typename TEnableIf<TIsDerivedFrom<T, UObject>::IsDerived, T*>::Type Get() const
147 {
148 static_assert(sizeof(T) > 0, "T must not be an incomplete type");
149 if (IsA(T::StaticClass()))
150 {
151 return static_cast<T*>(Container.Object);
152 }
153 return nullptr;
154 }
155
156 template <typename T>
157 typename TEnableIf<!TIsDerivedFrom<T, UObject>::IsDerived, T*>::Type Get() const
158 {
159 static_assert(sizeof(T) > 0, "T must not be an incomplete type");
160 if (IsA(T::StaticClass()))
161 {
162 return static_cast<T*>(Container.Field);
163 }
164 return nullptr;
165 }
166 */
167
175 {
176 if (bIsUObject)
177 {
178 return Container.Object;
179 }
180 else
181 {
182 return nullptr;
183 }
184 }
185
193 {
194 if (!bIsUObject)
195 {
196 return Container.Field;
197 }
198 else
199 {
200 return nullptr;
201 }
202 }
203
211 {
212 return Container.Field;
213 }
214
221 {
222 return Container.Object;
223 }
224
230 void* GetRawPointer() const
231 {
232 return Container.Field;
233 }
234
235 FFieldVariant GetOwnerVariant() const;
236 UClass* GetOwnerClass() const;
237 const std::string& GetFullName() const;
238 const std::string& GetPathName() const;
239 const std::string& GetName() const;
240 const std::string& GetClassName() const;
241 const std::string& GetFName() const;
242 bool IsNative() const;
243 //UPackage* GetOutermost() const;
244
245 bool operator==(const FFieldVariant& Other) const
246 {
247 return Container.Field == Other.Container.Field;
248 }
249
250 bool operator!=(const FFieldVariant& Other) const
251 {
252 return Container.Field != Other.Container.Field;
253 }
254/*
255#if WITH_EDITORONLY_DATA
256 bool HasMetaData(const FName& Key) const;
257#endif*/
258
260 /*friend uint32_t GetTypeHash(const FFieldVariant& InFieldVariant)
261 {
262 return GetTypeHash(InFieldVariant.GetRawPointer());
263 }
264
265 KARMA_API friend FArchive& operator << (FArchive& Ar, FFieldVariant& InOutField);*/
266 };
267
273 {
275 std::string m_Name;
276
278 uint64_t m_Id;
279
281 uint64_t m_CastFlags;
282
284 EClassFlags ClassFlags;
285
287 FFieldClass* SuperClass;
288
290 FField* DefaultObject;
291
293 FField* (*ConstructFn)(const FFieldVariant&, const std::string&, EObjectFlags);
294
296 //FThreadSafeCounter UnqiueNameIndexCounter;
297
299 FField* ConstructDefaultObject();
300
301 public:
303
311
318 static std::unordered_map<std::string, FFieldClass*>& GetNameToFieldClassMap();
319
325 explicit FFieldClass(const char* InCPPName, uint64_t InId, uint64_t InCastFlags, FFieldClass* InSuperClass, FField* (*ConstructFnPtr)(const FFieldVariant&, const std::string&, EObjectFlags));
326
333
339 inline const std::string& GetName() const
340 {
341 return m_Name;
342 }
343
349 inline uint64_t GetId() const
350 {
351 return m_Id;
352 }
353
359 inline uint64_t GetCastFlags() const
360 {
361 return m_CastFlags;
362 }
363 inline bool HasAnyCastFlags(const uint64_t InCastFlags) const
364 {
365 return !!(m_CastFlags & InCastFlags);
366 }
367 inline bool HasAllCastFlags(const uint64_t InCastFlags) const
368 {
369 return (m_CastFlags & InCastFlags) == InCastFlags;
370 }
371
380 inline bool IsChildOf(const FFieldClass* InClass) const
381 {
382 const uint64_t OtherClassId = InClass->GetId();
383 return OtherClassId ? !!(m_CastFlags & OtherClassId) : IsChildOf_Walk(InClass);
384 }
385
386 const std::string& GetDescription() const;
387 const std::string& GetDisplayNameText() const;
388
389 FField* Construct(const FFieldVariant& InOwner, const std::string& InName, EObjectFlags InFlags = RF_NoFlags) const
390 {
391 return ConstructFn(InOwner, InName, InFlags);
392 }
393
394 FFieldClass* GetSuperClass() const
395 {
396 return SuperClass;
397 }
398
405 {
406 if (!DefaultObject)
407 {
408 DefaultObject = ConstructDefaultObject();
409 KR_CORE_ASSERT(DefaultObject, "Default object is null");
410 }
411 return DefaultObject;
412 }
413
414 /*
415 bool HasAnyClassFlags(EClassFlags FlagsToCheck) const
416 {
417 return EnumHasAnyFlags(ClassFlags, FlagsToCheck) != 0;
418 }
419
420 int32_t GetNextUniqueNameIndex()
421 {
422 return UnqiueNameIndexCounter.Increment();
423 }
424
425 friend FArchive& operator << (FArchive& Ar, FFieldClass& InField)
426 {
427 check(false);
428 return Ar;
429 }
430 KARMA_API friend FArchive& operator << (FArchive& Ar, FFieldClass*& InOutFieldClass);*/
431
432 private:
433 bool IsChildOf_Walk(const FFieldClass* InBaseClass) const
434 {
435 for (const FFieldClass* TempField = this; TempField; TempField = TempField->GetSuperClass())
436 {
437 if (TempField == InBaseClass)
438 {
439 return true;
440 }
441 }
442 return false;
443 }
444 };
445
450 {
452 FFieldClass* ClassPrivate;
453
454 public:
456
457 typedef FField Super;
458 typedef FField ThisClass;
459 typedef FField BaseFieldClass;
460 typedef FFieldClass FieldTypeClass;
461
464
467
469 std::string m_NamePrivate;
470
473
474 public:
480 void Rename(const std::string& NewName);
481
482 //static FFieldClass* StaticClass();
483 };
484}
#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:127
#define FORCEINLINE
Typical inlining macro for clarity.
Definition Core.h:157
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:273
uint64_t GetId() const
Definition Field.h:349
const std::string & GetName() const
Definition Field.h:339
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:380
uint64_t GetCastFlags() const
Definition Field.h:359
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:404
Base class of reflection data objects.
Definition Field.h:450
FField * m_Next
Definition Field.h:466
void Rename(const std::string &NewName)
Rename the FField object.
Definition Field.cpp:21
std::string m_NamePrivate
Definition Field.h:469
EObjectFlags m_FlagsPrivate
Definition Field.h:472
FFieldVariant m_Owner
Definition Field.h:463
Special container that can hold either UObject or FField.
Definition Field.h:32
FORCEINLINE UObject * ToUObjectUnsafe() const
Definition Field.h:220
UObject * ToUObject() const
Getter for UObject if possible.
Definition Field.h:174
bool IsA(const UClass *InClass) const
bool IsValidLowLevel() const
FORCEINLINE FField * ToFieldUnsafe() const
Definition Field.h:210
bool IsValid() const
Maybe sees if UObject is being held.
Definition Field.h:103
bool IsA(const FFieldClass *InClass) const
void * GetRawPointer() const
Definition Field.h:230
FField * ToField() const
Getter for FField if possible.
Definition Field.h:192
FFieldVariant(T &&InObject)
Overloaded template constructor (UObject)
Definition Field.h:80
bool IsUObject() const
Getter for bIsUobject.
Definition Field.h:92
FFieldVariant(const FField *InField)
Overloaded constructor (FField)
Definition Field.h:64
FFieldVariant()
Constructor.
Definition Field.h:52
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.
Definition KarmaTypes.h:152