Karma Engine
Loading...
Searching...
No Matches
Field.h
1#pragma once
2
3#include "krpch.h"
4#include "UObjectGlobals.h"
5
6namespace Karma
7{
8 class UObject;
9 class UClass;
10 class FField;
11 class FFieldClass;
12
18 class KARMA_API FFieldVariant
19 {
20 union FFieldObjectUnion
21 {
22 FField* Field;
23 UObject* Object;
24 } Container;
25
26 bool bIsUObject;
27
28 public:
29
31 : bIsUObject(false)
32 {
33 Container.Field = nullptr;
34 }
35
36 FFieldVariant(const FField* InField)
37 : bIsUObject(false)
38 {
39 Container.Field = const_cast<FField*>(InField);
40 }
41
42 template <
43 typename T,
44 decltype(ImplicitConv<const UObject*>(std::declval<T>()))* = nullptr
45 >
46 FFieldVariant(T&& InObject)
47 : bIsUObject(true)
48 {
49 Container.Object = const_cast<UObject*>(ImplicitConv<const UObject*>(InObject));
50 }
51
52 inline bool IsUObject() const
53 {
54 return bIsUObject;
55 }
56 inline bool IsValid() const
57 {
58 return !!Container.Object;
59 }
60 bool IsValidLowLevel() const;
61 inline operator bool() const
62 {
63 return IsValid();
64 }
65 bool IsA(const UClass* InClass) const;
66 bool IsA(const FFieldClass* InClass) const;
67 template <typename T>
68 bool IsA() const
69 {
70 static_assert(sizeof(T) > 0, "T must not be an incomplete type");
71 return IsA(T::StaticClass());
72 }
73
74 /*
75 template <typename T>
76 typename TEnableIf<TIsDerivedFrom<T, UObject>::IsDerived, T*>::Type Get() const
77 {
78 static_assert(sizeof(T) > 0, "T must not be an incomplete type");
79 if (IsA(T::StaticClass()))
80 {
81 return static_cast<T*>(Container.Object);
82 }
83 return nullptr;
84 }
85
86 template <typename T>
87 typename TEnableIf<!TIsDerivedFrom<T, UObject>::IsDerived, T*>::Type Get() const
88 {
89 static_assert(sizeof(T) > 0, "T must not be an incomplete type");
90 if (IsA(T::StaticClass()))
91 {
92 return static_cast<T*>(Container.Field);
93 }
94 return nullptr;
95 }
96 */
97
98 UObject* ToUObject() const
99 {
100 if (bIsUObject)
101 {
102 return Container.Object;
103 }
104 else
105 {
106 return nullptr;
107 }
108 }
109 FField* ToField() const
110 {
111 if (!bIsUObject)
112 {
113 return Container.Field;
114 }
115 else
116 {
117 return nullptr;
118 }
119 }
121 FORCEINLINE FField* ToFieldUnsafe() const
122 {
123 return Container.Field;
124 }
126 FORCEINLINE UObject* ToUObjectUnsafe() const
127 {
128 return Container.Object;
129 }
130
131 void* GetRawPointer() const
132 {
133 return Container.Field;
134 }
135 FFieldVariant GetOwnerVariant() const;
136 UClass* GetOwnerClass() const;
137 const std::string& GetFullName() const;
138 const std::string& GetPathName() const;
139 const std::string& GetName() const;
140 const std::string& GetClassName() const;
141 const std::string& GetFName() const;
142 bool IsNative() const;
143 //UPackage* GetOutermost() const;
144
145 bool operator==(const FFieldVariant& Other) const
146 {
147 return Container.Field == Other.Container.Field;
148 }
149 bool operator!=(const FFieldVariant& Other) const
150 {
151 return Container.Field != Other.Container.Field;
152 }
153/*
154#if WITH_EDITORONLY_DATA
155 bool HasMetaData(const FName& Key) const;
156#endif*/
157
159 /*friend uint32_t GetTypeHash(const FFieldVariant& InFieldVariant)
160 {
161 return GetTypeHash(InFieldVariant.GetRawPointer());
162 }
163
164 KARMA_API friend FArchive& operator << (FArchive& Ar, FFieldVariant& InOutField);*/
165 };
166
171 class KARMA_API FFieldClass
172 {
174 std::string m_Name;
175
177 uint64_t m_Id;
178
180 uint64_t m_CastFlags;
181
183 EClassFlags ClassFlags;
184
186 FFieldClass* SuperClass;
187
189 FField* DefaultObject;
190
192 FField* (*ConstructFn)(const FFieldVariant&, const std::string&, EObjectFlags);
193
195 //FThreadSafeCounter UnqiueNameIndexCounter;
196
198 FField* ConstructDefaultObject();
199
200 public:
201 KR_NONCOPYABLE(FFieldClass);
202
205
207 static std::unordered_map<std::string, FFieldClass*>& GetNameToFieldClassMap();
208
209 explicit FFieldClass(const char* InCPPName, uint64_t InId, uint64_t InCastFlags, FFieldClass* InSuperClass, FField* (*ConstructFnPtr)(const FFieldVariant&, const std::string&, EObjectFlags));
210 ~FFieldClass();
211
212 inline const std::string& GetName() const
213 {
214 return m_Name;
215 }
216
217 inline uint64_t GetId() const
218 {
219 return m_Id;
220 }
221 inline uint64_t GetCastFlags() const
222 {
223 return m_CastFlags;
224 }
225 inline bool HasAnyCastFlags(const uint64_t InCastFlags) const
226 {
227 return !!(m_CastFlags & InCastFlags);
228 }
229 inline bool HasAllCastFlags(const uint64_t InCastFlags) const
230 {
231 return (m_CastFlags & InCastFlags) == InCastFlags;
232 }
233 inline bool IsChildOf(const FFieldClass* InClass) const
234 {
235 const uint64_t OtherClassId = InClass->GetId();
236 return OtherClassId ? !!(m_CastFlags & OtherClassId) : IsChildOf_Walk(InClass);
237 }
238 const std::string& GetDescription() const;
239 const std::string& GetDisplayNameText() const;
240
241 FField* Construct(const FFieldVariant& InOwner, const std::string& InName, EObjectFlags InFlags = RF_NoFlags) const
242 {
243 return ConstructFn(InOwner, InName, InFlags);
244 }
245
246 FFieldClass* GetSuperClass() const
247 {
248 return SuperClass;
249 }
250
251 FField* GetDefaultObject()
252 {
253 if (!DefaultObject)
254 {
255 DefaultObject = ConstructDefaultObject();
256 KR_CORE_ASSERT(DefaultObject, "Default object is null");
257 }
258 return DefaultObject;
259 }
260
261 /*
262 bool HasAnyClassFlags(EClassFlags FlagsToCheck) const
263 {
264 return EnumHasAnyFlags(ClassFlags, FlagsToCheck) != 0;
265 }
266
267 int32_t GetNextUniqueNameIndex()
268 {
269 return UnqiueNameIndexCounter.Increment();
270 }
271
272 friend FArchive& operator << (FArchive& Ar, FFieldClass& InField)
273 {
274 check(false);
275 return Ar;
276 }
277 KARMA_API friend FArchive& operator << (FArchive& Ar, FFieldClass*& InOutFieldClass);*/
278
279 private:
280 bool IsChildOf_Walk(const FFieldClass* InBaseClass) const
281 {
282 for (const FFieldClass* TempField = this; TempField; TempField = TempField->GetSuperClass())
283 {
284 if (TempField == InBaseClass)
285 {
286 return true;
287 }
288 }
289 return false;
290 }
291 };
292
296 class KARMA_API FField
297 {
299 FFieldClass* ClassPrivate;
300
301 public:
302 KR_NONCOPYABLE(FField)
303
304 typedef FField Super;
305 typedef FField ThisClass;
306 typedef FField BaseFieldClass;
308
311
314
316 std::string m_NamePrivate;
317
319 EObjectFlags m_FlagsPrivate;
320
321 public:
322 void Rename(const std::string& NewName);
323
324 //static FFieldClass* StaticClass();
325 };
326}
FORCEINLINE bool IsValid(const UObject *Test)
Test validity of object.
Definition Object.h:232
Karma's std::vector wrapper.
Definition KarmaTypes.h:128
Definition Field.h:172
static std::unordered_map< std::string, FFieldClass * > & GetNameToFieldClassMap()
static KarmaVector< FFieldClass * > & GetAllFieldClasses()
Definition Field.h:19
FORCEINLINE UObject * ToUObjectUnsafe() const
Definition Field.h:126
FORCEINLINE FField * ToFieldUnsafe() const
Definition Field.h:121
Definition Field.h:297
FField * m_Next
Definition Field.h:313
std::string m_NamePrivate
Definition Field.h:316
EObjectFlags m_FlagsPrivate
Definition Field.h:319
FFieldVariant m_Owner
Definition Field.h:310
Definition Class.h:99
The base class of all the game code relevant objects.
Definition Object.h:106