Optimized type casting and refactor
This commit is contained in:
+55
-19
@@ -1,29 +1,60 @@
|
||||
#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++;
|
||||
Classes* NewTree = new Classes[size];
|
||||
|
||||
for (size_t i = 0; i < size - 1; ++i)
|
||||
NewTree[i] = Tree[i];
|
||||
|
||||
delete[] Tree;
|
||||
Tree = NewTree;
|
||||
long long indexObject = _size - 1;
|
||||
for (long long i = 0, j = 0; i < _size - 1; ++i, ++j)
|
||||
{
|
||||
if (newType < array[i] && indexObject == _size - 1)
|
||||
{
|
||||
indexObject = j;
|
||||
++j;
|
||||
}
|
||||
|
||||
IRTTI::TreeArray::TreeArray()
|
||||
{
|
||||
Tree = new Classes;
|
||||
size = 1;
|
||||
NewArray[j] = array[i];
|
||||
}
|
||||
NewArray[indexObject] = newType;
|
||||
|
||||
delete[] array;
|
||||
array = NewArray;
|
||||
}
|
||||
}
|
||||
|
||||
void IRTTI::SetType(const Classes type)
|
||||
bool IRTTI::FastContainer::find(const Classes& Type) const
|
||||
{
|
||||
this->type = type;
|
||||
Tree.push_back(type);
|
||||
long long low = 0;
|
||||
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()
|
||||
@@ -31,7 +62,12 @@ IRTTI::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
@@ -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)
|
||||
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);
|
||||
};
|
||||
+2
-2
@@ -1,13 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
// This macro adds meta information to a type.
|
||||
// This macro adds meta information to a type
|
||||
#define GENERATE_META(Class) \
|
||||
class Class; \
|
||||
template<> struct Meta <Class> { \
|
||||
static constexpr Classes Type = Classes::##Class; \
|
||||
};
|
||||
|
||||
// Predeclarate struct for meta information
|
||||
// Predeclared struct for meta information
|
||||
template<class T> struct Meta;
|
||||
|
||||
// Enum type classes
|
||||
|
||||
Reference in New Issue
Block a user