KarmaEngine
Game Engine for practical learning and research purposes
Loading...
Searching...
No Matches
SubClassOf.h
Go to the documentation of this file.
1
10
11#pragma once
12
13#include "krpch.h"
14
15#include "Class.h"
16#include "Field.h"
17
18namespace Karma
19{
23 template<bool Predicate, typename TrueClass, typename FalseClass>
25
26 template<typename TrueClass, typename FalseClass>
27 class TChooseClass<true, TrueClass, FalseClass>
28 {
29 public:
30 typedef TrueClass Result;
31 };
32
33 template<typename TrueClass, typename FalseClass>
34 class TChooseClass<false, TrueClass, FalseClass>
35 {
36 public:
37 typedef FalseClass Result;
38 };
39
40 template <typename T>
42 {
43 enum { Value = false };
44 };
45
53 template<typename DerivedType, typename BaseType>
55 {
56 // Different size types so we can compare their sizes later.
58 typedef char No[1];
59
61 typedef char Yes[2];
62
63 // Overloading Test() s.t. only calling it with something that is
64 // a BaseType (or inherited from the BaseType) will return a Yes.
65
74 static Yes& Test(BaseType*);
75
84 static Yes& Test(const BaseType*);
85
94 static No& Test(...);
95
102 static DerivedType* DerivedTypePtr() { return nullptr; }
103
104 public:
109 static constexpr bool Value = sizeof(Test(DerivedTypePtr())) == sizeof(Yes);
110
111 static constexpr bool IsDerived = Value;
112 };
113
117 template<class TClass>
118 class TSubclassOf
119 {
120 public:
127
134
135 private:
136 template <class TClassA>
137 friend class TSubclassOf;
138
139 public:
145 inline TSubclassOf() :
146 Class(nullptr)
147 {
148 }
149
156 inline TSubclassOf(TClassType* From) :
157 Class(From)
158 {
159 }
160
207 template <
208 typename U,
209 std::enable_if_t<!TIsTSubclassOf<std::decay_t<U>>::Value, decltype(ImplicitConv<TClassType*>(std::declval<U>()))
210 >* = nullptr
211 >
212 inline TSubclassOf(U&& From)
213 : Class(From)
214 {
215 }
216
223 template <class TClassA, class = decltype(ImplicitConv<TClass*>((TClassA*)nullptr))>
224 inline TSubclassOf(const TSubclassOf<TClassA>& From) :
225 Class(*From)
226 {
227 }
228
235 template <class TClassA, class = decltype(ImplicitConv<TClass*>((TClassA*)nullptr))>
236 inline TSubclassOf& operator=(const TSubclassOf<TClassA>& From)
237 {
238 Class = *From;
239 return *this;
240 }
241
248 inline TSubclassOf& operator=(TClassType* From)
249 {
250 Class = From;
251 return *this;
252 }
253
260 template <
261 typename U,
262 std::enable_if_t<
264 decltype(ImplicitConv<TClassType*>(std::declval<U>()))
265 >* = nullptr
266 >
267 inline TSubclassOf& operator=(U&& From)
268 {
269 Class = From;
270 return *this;
271 }
272
279 inline TClassType* operator*() const
280 {
281 if (!Class || !Class->IsChildOf(TClass::StaticClass()))
282 {
283 return nullptr;
284 }
285 return Class;
286 }
287
293 inline TClassType* Get() const
294 {
295 return **this;
296 }
297
303 inline TClassType* operator->() const
304 {
305 return **this;
306 }
307
313 inline operator TClassType* () const
314 {
315 return **this;
316 }
317
324 inline TClass* GetDefaultObject() const
325 {
326 TBaseType* Result = nullptr;
327 if (Class)
328 {
329 Result = Class->GetDefaultObject();
330 KR_CORE_ASSERT(Result && Result->IsA(TClass::StaticClass()), "");
331 }
332 return (TClass*)Result;
333 }
334 /*
335 friend FArchive& operator<<(FArchive& Ar, TSubclassOf& SubclassOf)
336 {
337 Ar << SubclassOf.Class;
338 return Ar;
339 }
340
341 friend uint32 GetTypeHash(const TSubclassOf& SubclassOf)
342 {
343 return GetTypeHash(SubclassOf.Class);
344 }*/
345
346 private:
347 TClassType* Class;
348 };
349
350 template <typename T>
352 {
353 enum { Value = true };
354 };
355}
This file contains the class UField, UStruct, and UClass.
This file contains the class FField and relevant helpers.
Object representing a type of an FField struct. Mimics a subset of UObject reflection functions.
Definition Field.h:273
Base class of reflection data objects.
Definition Field.h:450
Chooses between two different classes based on a boolean.
Definition SubClassOf.h:24
TClassType * operator*() const
Definition SubClassOf.h:279
TClassType * operator->() const
Definition SubClassOf.h:303
TSubclassOf()
Definition SubClassOf.h:145
TSubclassOf(TClassType *From)
Definition SubClassOf.h:156
TClass * GetDefaultObject() const
Definition SubClassOf.h:324
TSubclassOf(const TSubclassOf< TClassA > &From)
Definition SubClassOf.h:224
TSubclassOf & operator=(TClassType *From)
Definition SubClassOf.h:248
TSubclassOf(U &&From)
Constructor that takes a UClass and does a runtime check to make sure this is a compatible class.
Definition SubClassOf.h:212
TChooseClass< TIsDerivedFrom< TClass, FField >::IsDerived, FField, UObject >::Result TBaseType
Again, on parsing, I understand the meaning to be, if TClass is derived from FField then TBaseType is...
Definition SubClassOf.h:133
TClassType * Get() const
Definition SubClassOf.h:293
TChooseClass< TIsDerivedFrom< TClass, FField >::IsDerived, FFieldClass, UClass >::Result TClassType
Since I don't think I understand the purpose of this typedef, I may attempt to parse the code....
Definition SubClassOf.h:126
An object class.
Definition Class.h:158
The base class of all the game code relevant objects.
Definition Object.h:106
Is type DerivedType inherited from BaseType?
Definition SubClassOf.h:55
static Yes & Test(const BaseType *)
Handy variant function called when type-cast succeeds.
static No & Test(...)
Handy function called when type-cast does not succeed.
static constexpr bool Value
Definition SubClassOf.h:109
char Yes[2]
Definition SubClassOf.h:61
static Yes & Test(BaseType *)
Handy function called when type-cast succeeds.
static DerivedType * DerivedTypePtr()
Definition SubClassOf.h:102
char No[1]
Definition SubClassOf.h:58
Definition SubClassOf.h:42