LkEngine 0.1.2
 
Loading...
Searching...
No Matches
Object.h
Go to the documentation of this file.
1
5#pragma once
6
14#include <set>
15#include <typeindex>
16#include <typeinfo>
17#include <type_traits>
18
20#include "LkEngine/Core/LObject/Class.h"
22#include "LkEngine/Core/MetadataRegistry.h"
23
24namespace LkEngine {
25
31 using FObjectHandle = LUUID;
32
33 class LMetadataRegistry;
34
35 template<typename T>
36 class TObjectPtr;
37
45 class LObject : public LObjectBase
46 {
47 public:
48 LObject();
49
50 LObject(const LObject& Other)
51 : ObjectHandle(Other.ObjectHandle)
52 , bObjectInitialized(Other.bObjectInitialized)
53 , ObjectFlags(Other.ObjectFlags)
55 {
56 }
57
58 virtual ~LObject() = default;
59
63 virtual void Initialize();
64
68 virtual void Destroy() {};
69
73 FORCEINLINE virtual bool IsInitialized() const { return bObjectInitialized; }
74
78 FORCEINLINE FObjectHandle GetObjectHandle() const { return ObjectHandle; }
79
83 FORCEINLINE virtual bool IsObjectValid() const
84 {
85 if (HasAnyFlags(EObjectFlag::Garbage | EObjectFlag::BeginDestroy | EObjectFlag::FinishDestroy))
86 {
87 return false;
88 }
89
90 return true;
91 }
92
98 FORCEINLINE static const LClass* StaticClass()
99 {
100 static bool bClassRegistered = false;
101 if (!bClassRegistered)
102 {
103 LClass* ObjectClass = LClass::Register<LObject>("LObject");
104 bClassRegistered = true;
105 }
106
108 }
109
115 virtual const LClass* ObjectRegistration() = 0;
116
122 FORCEINLINE static std::string StaticClassName() { return "LObject"; }
123
133 FORCEINLINE virtual const LClass* GetClass() const
134 {
135 return LObjectBase::GetClass();
136 }
137
143 virtual std::string ClassName() const = 0;
144
145 LObject& operator=(const LObject& Other)
146 {
147 if (this == &Other)
148 {
149 return *this;
150 }
151
152 ObjectHandle = Other.ObjectHandle;
153 bObjectInitialized = Other.bObjectInitialized;
154 ObjectFlags = Other.ObjectFlags;
156
157 return *this;
158 }
159
164 {
165 ObjectFlags |= EObjectFlag::Garbage;
166 }
167
171 template<typename T>
172 T& As()
173 {
174 static_assert(sizeof(T) > 0, "As<T> failed, incomplete type");
175 return static_cast<T&>(*this);
176 }
177
181 template<typename T>
182 const T& As() const
183 {
184 static_assert(sizeof(T) > 0, "As<T> failed, incomplete type");
185 return static_cast<const T&>(*this);
186 }
187
191 template<typename T>
192 bool IsA() const
193 {
194 static_assert(sizeof(T) > 0, "IsA<T> failed, incomplete type");
195 return (GetClass()->GetName() == T::StaticClassName());
196 }
197
201 FORCEINLINE virtual bool IsAsset() const { return false; }
202
206 FORCEINLINE uint32_t GetReferenceCount() const { return Ptr_ReferenceCount.load(); }
207
211 template <typename T>
213 {
214 static_assert(HasLClassMacro<T>, "Class must include the LCLASS macro");
215 }
216
217 private:
221 FORCEINLINE bool HasFlag(const EObjectFlag InFlag) const
222 {
223 return ((ObjectFlags & InFlag) == static_cast<std::underlying_type_t<EObjectFlag>>(InFlag));
224 }
225
229 FORCEINLINE bool HasAnyFlags(const EObjectFlag InFlags) const
230 {
231 return static_cast<bool>(ObjectFlags & InFlags);
232 }
233
234 private:
240 FORCEINLINE void IncrementReferenceCount() const
241 {
243 }
244
250 FORCEINLINE void DecrementReferenceCount() const
251 {
252 LK_CORE_ASSERT(Ptr_ReferenceCount > 0, "LObject::DecrementReferenceCount failed");
254 }
255
256 protected:
257 FObjectHandle ObjectHandle = 0;
258
259 bool bObjectInitialized = false;
260 EObjectFlag ObjectFlags = EObjectFlag::None;
261
263 mutable std::atomic<uint32_t> Ptr_ReferenceCount = 0;
264
265 template<typename T>
266 friend class TObjectPtr;
267
268 friend struct FInternalLObjectValidator;
269 };
270
277 {
278 FORCEINLINE static bool CheckObjectValidBasedOnFlags(const LObject* Object)
279 {
280 return !(Object->HasAnyFlags(EObjectFlag::Garbage));
281 }
282 };
283
284 FORCEINLINE bool IsValid(const LObject* Object)
285 {
286 return (Object && FInternalLObjectValidator::CheckObjectValidBasedOnFlags(Object));
287 }
288
289 FORCEINLINE bool IsValid(const LObject& Object)
290 {
291 return IsValid(&Object);
292 }
293
294 template<typename TObject>
295 inline constexpr bool IsBaseOfObject = std::is_base_of<LObject, std::decay_t<TObject>>::value;
296
298}
299
305namespace std
306{
307 template<>
308 struct hash<LkEngine::LObject>
309 {
310 std::size_t operator()(const LkEngine::LObject& Object) const noexcept
311 {
312 return std::hash<uint64_t>()(Object.GetObjectHandle());
313 }
314 };
315}
Core macros used by the entire engine.
LObject base implementation.
Definition Class.h:17
static LClass * Register(std::string_view ClassName)
Register metadata for a LClass.
Definition Class.h:67
static const LClass * Get(const std::type_index &ClassType)
Retrieve metadata for a LClass.
Definition Class.h:53
Definition ObjectBase.h:57
FORCEINLINE const LClass * GetClass() const
Get the private class member.
Definition ObjectBase.h:99
Definition Object.h:46
T & As()
Cast object to type T.
Definition Object.h:172
virtual FORCEINLINE bool IsAsset() const
Check if object is an asset.
Definition Object.h:201
const T & As() const
Cast object to type T.
Definition Object.h:182
virtual void Initialize()
Initialize object.
Definition Object.cpp:15
virtual FORCEINLINE const LClass * GetClass() const
Get the class for this LObject.
Definition Object.h:133
virtual void Destroy()
Destroy object, releasing all resources.
Definition Object.h:68
virtual FORCEINLINE bool IsObjectValid() const
Check if object is valid for use.
Definition Object.h:83
static void ValidateLObjectImplementation()
Validate the implementation of a LObject.
Definition Object.h:212
bool IsA() const
Check if the object is or is a derivation of type T.
Definition Object.h:192
virtual const LClass * ObjectRegistration()=0
Register object class.
static FORCEINLINE std::string StaticClassName()
Static class name.
Definition Object.h:122
virtual std::string ClassName() const =0
Get name of the LObject class.
virtual FORCEINLINE bool IsInitialized() const
Check if object is initialized.
Definition Object.h:73
FORCEINLINE uint32_t GetReferenceCount() const
Return current reference count from all object pointers.
Definition Object.h:206
static FORCEINLINE const LClass * StaticClass()
Get static class object.
Definition Object.h:98
std::atomic< uint32_t > Ptr_ReferenceCount
Definition Object.h:263
FORCEINLINE FObjectHandle GetObjectHandle() const
Get the object handle.
Definition Object.h:78
void MarkAsGarbage()
Mark object as garbage.
Definition Object.h:163
Definition ObjectPtr.h:102
Definition ObjectBase.h:134
Definition Asset.h:11
Definition UUID.h:33
Definition TypeTrait.h:29
Definition UUID.h:13
Definition UUID.h:35