LkEngine 0.1.2
 
Loading...
Searching...
No Matches
Class.h
1/******************************************************************
2 * LClass
3 *
4 *
5 *******************************************************************/
6#pragma once
7
10#include "LkEngine/Core/Template/TypeTrait.h"
12
13
14namespace LkEngine {
15
16 class LClass
17 {
21 using FObjectCastFunction = std::function<void*(LObjectBase*)>;
22
23 public:
24 explicit LClass(std::string_view InName, const std::type_index& InTypeID, FObjectCastFunction InCastFunction)
25 : Name(InName)
26 , TypeID(InTypeID)
27 , CastFunction(std::move(InCastFunction))
28 {
29 }
30
31 LClass() = default;
32 virtual ~LClass() = default;
33
37 FORCEINLINE const std::string& GetName() const
38 {
39 return Name;
40 }
41
45 [[nodiscard]] FORCEINLINE const std::type_index& GetTypeID() const
46 {
47 return TypeID;
48 }
49
53 static const LClass* Get(const std::type_index& ClassType)
54 {
55 if (auto Iter = ClassMetadataMap.find(ClassType); Iter != ClassMetadataMap.end())
56 {
57 return Iter->second.get();
58 }
59
60 return nullptr;
61 }
62
66 template<typename T>
67 static LClass* Register(std::string_view ClassName)
68 {
69 static_assert(sizeof(T) > 0, "Register failed, incomplete type T");
70 if (ClassMetadataMap.contains(typeid(T)))
71 {
72 return ClassMetadataMap[typeid(T)].get();
73 }
74
75 ClassMetadataMap[typeid(T)] = std::make_unique<LClass>(ClassName, typeid(T), [](LObjectBase* ClassObject) -> void*
76 {
77 return static_cast<T*>(ClassObject);
78 });
79
80 LClass* Class = ClassMetadataMap[typeid(T)].get();
81 Class->bRegistered = true;
82
83 return Class;
84 }
85
86 bool operator==(const LClass& Other) const
87 {
88 return (TypeID == Other.TypeID);
89 }
90
94 FORCEINLINE bool IsRegistered() const
95 {
96 return bRegistered;
97 }
98
102 bool IsChildOf(const LClass* OtherClass) const;
103
104 template<typename T>
105 bool IsChildOf() const
106 {
107 static_assert(sizeof(T) > 0, "IsChildOf failed, incomplete type T");
108 return IsChildOf(T::StaticClass());
109 }
110
111 private:
112 std::string Name{};
113 std::type_index TypeID;
114 FObjectCastFunction CastFunction;
115
116 bool bRegistered = false;
117
119 inline static std::unordered_map<std::type_index, std::unique_ptr<LClass>> ClassMetadataMap{};
120 };
121
122}
Core macros used by the entire engine.
Core types.
LObject base implementation.
Definition Class.h:17
static LClass * Register(std::string_view ClassName)
Register metadata for a LClass.
Definition Class.h:67
FORCEINLINE bool IsRegistered() const
Check if class is registered in the static storage.
Definition Class.h:94
FORCEINLINE const std::string & GetName() const
Get name of class.
Definition Class.h:37
static const LClass * Get(const std::type_index &ClassType)
Retrieve metadata for a LClass.
Definition Class.h:53
FORCEINLINE const std::type_index & GetTypeID() const
Get type ID.
Definition Class.h:45
Definition ObjectBase.h:57
Definition Asset.h:11