Karma Engine
Loading...
Searching...
No Matches
SubClassOf.h
1#pragma once
2
3#include "krpch.h"
4
5#include "Class.h"
6#include "Field.h"
7
8namespace Karma
9{
11 template<bool Predicate, typename TrueClass, typename FalseClass>
13
14 template<typename TrueClass, typename FalseClass>
15 class TChooseClass<true, TrueClass, FalseClass>
16 {
17 public:
18 typedef TrueClass Result;
19 };
20
21 template<typename TrueClass, typename FalseClass>
22 class TChooseClass<false, TrueClass, FalseClass>
23 {
24 public:
25 typedef FalseClass Result;
26 };
27
28 template <typename T>
30 {
31 enum { Value = false };
32 };
33
35 template<typename DerivedType, typename BaseType>
37 {
38 // Different size types so we can compare their sizes later.
39 typedef char No[1];
40 typedef char Yes[2];
41
42 // Overloading Test() s.t. only calling it with something that is
43 // a BaseType (or inherited from the BaseType) will return a Yes.
44 static Yes& Test(BaseType*);
45 static Yes& Test(const BaseType*);
46 static No& Test(...);
47
48 // Makes a DerivedType ptr.
49 static DerivedType* DerivedTypePtr() { return nullptr; }
50
51 public:
52 // Test the derived type pointer. If it inherits from BaseType, the Test( BaseType* )
53 // will be chosen. If it does not, Test( ... ) will be chosen.
54 static constexpr bool Value = sizeof(Test(DerivedTypePtr())) == sizeof(Yes);
55
56 static constexpr bool IsDerived = Value;
57 };
58
62 template<class TClass>
64 {
65 public:
67 typedef typename TChooseClass<TIsDerivedFrom<TClass, FField>::IsDerived, FField, UObject>::Result TBaseType;
68
69 private:
70 template <class TClassA>
71 friend class TSubclassOf;
72
73 public:
75 inline TSubclassOf() :
76 Class(nullptr)
77 {
78 }
79
81 inline TSubclassOf(TClassType* From) :
82 Class(From)
83 {
84 }
85
87 template <
88 typename U,
89 std::enable_if_t<
91 decltype(ImplicitConv<TClassType*>(std::declval<U>()))
92 >* = nullptr
93 >
94 inline TSubclassOf(U&& From)
95 : Class(From)
96 {
97 }
98
100 template <class TClassA, class = decltype(ImplicitConv<TClass*>((TClassA*)nullptr))>
101 inline TSubclassOf(const TSubclassOf<TClassA>& From) :
102 Class(*From)
103 {
104 }
105
107 template <class TClassA, class = decltype(ImplicitConv<TClass*>((TClassA*)nullptr))>
108 inline TSubclassOf& operator=(const TSubclassOf<TClassA>& From)
109 {
110 Class = *From;
111 return *this;
112 }
113
115 inline TSubclassOf& operator=(TClassType* From)
116 {
117 Class = From;
118 return *this;
119 }
120
122 template <
123 typename U,
124 std::enable_if_t<
126 decltype(ImplicitConv<TClassType*>(std::declval<U>()))
127 >* = nullptr
128 >
129 inline TSubclassOf& operator=(U&& From)
130 {
131 Class = From;
132 return *this;
133 }
134
136 inline TClassType* operator*() const
137 {
138 if (!Class || !Class->IsChildOf(TClass::StaticClass()))
139 {
140 return nullptr;
141 }
142 return Class;
143 }
144
146 inline TClassType* Get() const
147 {
148 return **this;
149 }
150
152 inline TClassType* operator->() const
153 {
154 return **this;
155 }
156
158 inline operator TClassType* () const
159 {
160 return **this;
161 }
162
168 inline TClass* GetDefaultObject() const
169 {
170 TBaseType* Result = nullptr;
171 if (Class)
172 {
173 Result = Class->GetDefaultObject();
174 check(Result && Result->IsA(TClass::StaticClass()));
175 }
176 return (TClass*)Result;
177 }
178 /*
179 friend FArchive& operator<<(FArchive& Ar, TSubclassOf& SubclassOf)
180 {
181 Ar << SubclassOf.Class;
182 return Ar;
183 }
184
185 friend uint32 GetTypeHash(const TSubclassOf& SubclassOf)
186 {
187 return GetTypeHash(SubclassOf.Class);
188 }*/
189
190 private:
191 TClassType* Class;
192 };
193
194 template <typename T>
196 {
197 enum { Value = true };
198 };
199}
Definition Field.h:172
Definition Field.h:297
Definition SubClassOf.h:12
Definition SubClassOf.h:64
TClassType * operator*() const
Definition SubClassOf.h:136
TClassType * operator->() const
Definition SubClassOf.h:152
TSubclassOf()
Definition SubClassOf.h:75
TSubclassOf(TClassType *From)
Definition SubClassOf.h:81
TClass * GetDefaultObject() const
Definition SubClassOf.h:168
TSubclassOf(const TSubclassOf< TClassA > &From)
Definition SubClassOf.h:101
TSubclassOf & operator=(TClassType *From)
Definition SubClassOf.h:115
TSubclassOf(U &&From)
Definition SubClassOf.h:94
TClassType * Get() const
Definition SubClassOf.h:146
Definition Class.h:99
The base class of all the game code relevant objects.
Definition Object.h:106
Definition SubClassOf.h:37
Definition SubClassOf.h:30