Add cast by Tree and refactor
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
#include "RTTI.h"
|
||||
|
||||
void IRTTI::TreeArray::push_back(const Classes type)
|
||||
{
|
||||
size++;
|
||||
Classes* NewTree = new Classes[size];
|
||||
|
||||
for (size_t i = 0; i < size - 1; ++i)
|
||||
NewTree[i] = Tree[i];
|
||||
|
||||
NewTree[size - 1] = type;
|
||||
}
|
||||
|
||||
IRTTI::TreeArray::TreeArray()
|
||||
{
|
||||
Tree = new Classes;
|
||||
size = 1;
|
||||
}
|
||||
|
||||
void IRTTI::SetType(const Classes type)
|
||||
{
|
||||
this->type = type;
|
||||
//Tree.push_back(type);
|
||||
}
|
||||
|
||||
IRTTI::IRTTI()
|
||||
{
|
||||
SetType(Classes::IRTTI);
|
||||
}
|
||||
|
||||
Classes IRTTI::RTTI_Kind() const
|
||||
{
|
||||
return type;
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
#include "RTTI_Meta.h"
|
||||
|
||||
// Interface RTTI
|
||||
GENERATE_META(IRTTI)
|
||||
class IRTTI
|
||||
{
|
||||
struct TreeArray
|
||||
{
|
||||
size_t size;
|
||||
Classes* Tree;
|
||||
void push_back(const Classes type);
|
||||
TreeArray();
|
||||
};
|
||||
TreeArray Tree;
|
||||
Classes type;
|
||||
protected:
|
||||
void SetType(Classes type);
|
||||
|
||||
IRTTI();
|
||||
~IRTTI() = default;
|
||||
public:
|
||||
Classes RTTI_Kind() const;
|
||||
|
||||
friend class RTTI;
|
||||
};
|
||||
|
||||
// RTTI metods
|
||||
class RTTI final
|
||||
{
|
||||
RTTI() = default;
|
||||
public:
|
||||
template<class Kind>
|
||||
static bool IsA(Classes type)
|
||||
{
|
||||
return Meta<Kind>::Type == type;
|
||||
}
|
||||
|
||||
template<class To, class From>
|
||||
static To* dyn_cast(From* object)
|
||||
{
|
||||
IRTTI* InterfaceOfObject = static_cast<IRTTI*>(object);
|
||||
if (IsA<To>(InterfaceOfObject->type))
|
||||
return reinterpret_cast<To*>(object);
|
||||
|
||||
const size_t coutType = InterfaceOfObject->Tree.size;
|
||||
for (size_t i = 0; i < coutType; ++i)
|
||||
{
|
||||
if (InterfaceOfObject->Tree.Tree[i] == Meta<To>::Type)
|
||||
return reinterpret_cast<To*>(object);
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
// 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
|
||||
template<class T> struct Meta;
|
||||
|
||||
// Enum type classes
|
||||
enum class Classes
|
||||
{
|
||||
IRTTI
|
||||
};
|
||||
Reference in New Issue
Block a user