Optimized type casting and refactor
This commit is contained in:
+48
-20
@@ -6,37 +6,62 @@
|
||||
GENERATE_META(IRTTI)
|
||||
class IRTTI
|
||||
{
|
||||
struct TreeArray
|
||||
// Fast array
|
||||
class FastContainer final
|
||||
{
|
||||
size_t size;
|
||||
Classes* Tree;
|
||||
void push_back(const Classes type);
|
||||
TreeArray();
|
||||
long long _size = 0;
|
||||
Classes* array = nullptr;
|
||||
public:
|
||||
long long constexpr size() const { return _size; }
|
||||
void push_back(const Classes object);
|
||||
bool find(const Classes& Type) const;
|
||||
};
|
||||
TreeArray Tree;
|
||||
FastContainer Tree;
|
||||
Classes type;
|
||||
protected:
|
||||
void SetType(Classes type);
|
||||
// That method set type value and add type to inheritance tree
|
||||
void SetType(Classes Type);
|
||||
|
||||
IRTTI();
|
||||
~IRTTI() = default;
|
||||
public:
|
||||
Classes RTTI_Kind() const;
|
||||
|
||||
|
||||
friend class RTTI;
|
||||
};
|
||||
|
||||
// RTTI metods
|
||||
// RTTI methods
|
||||
class RTTI final
|
||||
{
|
||||
RTTI() = default;
|
||||
public:
|
||||
|
||||
// Convert enum to string
|
||||
[[nodiscard]]
|
||||
static const char* enum_to_string(const Classes type)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case Classes::IRTTI: return "IRTTI";
|
||||
case Classes::TestA: return "TestA";
|
||||
case Classes::TestB: return "TestB";
|
||||
case Classes::TestC: return "TestC";
|
||||
case Classes::TestD: return "TestD";
|
||||
default: return "Undefined";
|
||||
}
|
||||
}
|
||||
|
||||
// That method allow compare given type with real type object
|
||||
template<class Kind>
|
||||
static bool IsA(Classes type)
|
||||
static bool IsA(const Classes type)
|
||||
{
|
||||
return Meta<Kind>::Type == type;
|
||||
}
|
||||
|
||||
public:
|
||||
template<class Kind>
|
||||
static bool IsA(const IRTTI* object)
|
||||
{
|
||||
return IsA<Kind>(object->type);
|
||||
}
|
||||
|
||||
// That method realized dynamic_cast
|
||||
template<class To, class From>
|
||||
static To* dyn_cast(From* object)
|
||||
{
|
||||
@@ -44,13 +69,16 @@ public:
|
||||
if (IsA<To>(InterfaceOfObject->type))
|
||||
return reinterpret_cast<To*>(object);
|
||||
|
||||
const size_t coutType = InterfaceOfObject->Tree.size - 1;
|
||||
for (size_t i = 0; i < coutType; ++i)
|
||||
{
|
||||
if (InterfaceOfObject->Tree.Tree[i] == Meta<To>::Type)
|
||||
return reinterpret_cast<To*>(object);
|
||||
}
|
||||
if (InterfaceOfObject->Tree.find(Meta<To>::Type))
|
||||
return reinterpret_cast<To*>(object);
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// That method return real type object in the form of enum value
|
||||
static Classes GetType(const IRTTI* object);
|
||||
|
||||
// That method return real type in the form of string
|
||||
[[nodiscard]]
|
||||
static const char* GetTypeStr(const IRTTI* object);
|
||||
};
|
||||
Reference in New Issue
Block a user