Optimized type casting and refactor

This commit is contained in:
Данил
2025-02-23 15:49:53 +07:00
parent 870d93c19f
commit 4436e79e6a
3 changed files with 105 additions and 41 deletions
+55 -19
View File
@@ -1,29 +1,60 @@
#include "RTTI.h" #include "RTTI.h"
void IRTTI::TreeArray::push_back(const Classes type) void IRTTI::FastContainer::push_back(const Classes newType)
{ {
Tree[size - 1] = type; if (0 == _size)
{
++_size;
array = new Classes;
*array = newType;
}
else
{
++_size;
Classes* NewArray = new Classes[_size];
size++; long long indexObject = _size - 1;
Classes* NewTree = new Classes[size]; for (long long i = 0, j = 0; i < _size - 1; ++i, ++j)
{
for (size_t i = 0; i < size - 1; ++i) if (newType < array[i] && indexObject == _size - 1)
NewTree[i] = Tree[i]; {
indexObject = j;
delete[] Tree; ++j;
Tree = NewTree;
} }
IRTTI::TreeArray::TreeArray() NewArray[j] = array[i];
{ }
Tree = new Classes; NewArray[indexObject] = newType;
size = 1;
delete[] array;
array = NewArray;
}
} }
void IRTTI::SetType(const Classes type) bool IRTTI::FastContainer::find(const Classes& Type) const
{ {
this->type = type; long long low = 0;
Tree.push_back(type); long long high = _size - 1;
while (low <= high)
{
long long mid = (low + high) / 2;
if (static_cast<int>(array[mid]) == static_cast<int>(Type))
return true;
if (static_cast<int>(array[mid]) > static_cast<int>(Type))
high = mid - 1;
else
low = mid + 1;
}
return false;
}
void IRTTI::SetType(const Classes Type)
{
this->type = Type;
Tree.push_back(Type);
} }
IRTTI::IRTTI() IRTTI::IRTTI()
@@ -31,7 +62,12 @@ IRTTI::IRTTI()
SetType(Classes::IRTTI); SetType(Classes::IRTTI);
} }
Classes IRTTI::RTTI_Kind() const Classes RTTI::GetType(const IRTTI* object)
{ {
return type; return object->type;
}
const char* RTTI::GetTypeStr(const IRTTI* object)
{
return enum_to_string(object->type);
} }
+45 -17
View File
@@ -6,37 +6,62 @@
GENERATE_META(IRTTI) GENERATE_META(IRTTI)
class IRTTI class IRTTI
{ {
struct TreeArray // Fast array
class FastContainer final
{ {
size_t size; long long _size = 0;
Classes* Tree; Classes* array = nullptr;
void push_back(const Classes type); public:
TreeArray(); 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; Classes type;
protected: protected:
void SetType(Classes type); // That method set type value and add type to inheritance tree
void SetType(Classes Type);
IRTTI(); IRTTI();
~IRTTI() = default; ~IRTTI() = default;
public:
Classes RTTI_Kind() const;
friend class RTTI; friend class RTTI;
}; };
// RTTI metods // RTTI methods
class RTTI final class RTTI final
{ {
RTTI() = default; 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> template<class Kind>
static bool IsA(Classes type) static bool IsA(const Classes type)
{ {
return Meta<Kind>::Type == 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> template<class To, class From>
static To* dyn_cast(From* object) static To* dyn_cast(From* object)
{ {
@@ -44,13 +69,16 @@ public:
if (IsA<To>(InterfaceOfObject->type)) if (IsA<To>(InterfaceOfObject->type))
return reinterpret_cast<To*>(object); return reinterpret_cast<To*>(object);
const size_t coutType = InterfaceOfObject->Tree.size - 1; if (InterfaceOfObject->Tree.find(Meta<To>::Type))
for (size_t i = 0; i < coutType; ++i)
{
if (InterfaceOfObject->Tree.Tree[i] == Meta<To>::Type)
return reinterpret_cast<To*>(object); return reinterpret_cast<To*>(object);
}
return nullptr; 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);
}; };
+2 -2
View File
@@ -1,13 +1,13 @@
#pragma once #pragma once
// This macro adds meta information to a type. // This macro adds meta information to a type
#define GENERATE_META(Class) \ #define GENERATE_META(Class) \
class Class; \ class Class; \
template<> struct Meta <Class> { \ template<> struct Meta <Class> { \
static constexpr Classes Type = Classes::##Class; \ static constexpr Classes Type = Classes::##Class; \
}; };
// Predeclarate struct for meta information // Predeclared struct for meta information
template<class T> struct Meta; template<class T> struct Meta;
// Enum type classes // Enum type classes