-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrtti.h
More file actions
79 lines (59 loc) · 1.64 KB
/
Copy pathrtti.h
File metadata and controls
79 lines (59 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#ifndef RTTI_H_
#define RTTI_H_
#include <typeinfo>
#include <extension.h>
class IBaseType
{
public:
/* Offset from the original IType* */
virtual ptrdiff_t GetOffset() =0;
/* Gets the basic type info for this type */
virtual const std::type_info &GetTypeInfo() =0;
/* Returns the number of types this type inherits from */
virtual size_t GetNumBaseClasses() =0;
/* Gets the first base class */
virtual IBaseType *GetBaseClass(size_t num) =0;
};
class IType
{
public:
/* Gets the top level class this type represents */
virtual IBaseType *GetBaseType() =0;
/* Destroy all memory resources held by this object chain */
virtual void Destroy() =0;
};
/* Get type information for a class pointer */
IType *GetType(const void *ptr);
/* Returns the classname for a given type - Removes platform specific formatting */
const char *GetTypeName(const std::type_info &type);
inline void DumpType(IBaseType *pType, int level)
{
for (int i = 0; i < level; i++)
META_CONPRINT("-");
META_CONPRINTF("%s\n", GetTypeName(pType->GetTypeInfo()));
for (size_t i = 0; i < pType->GetNumBaseClasses(); i++)
{
DumpType(pType->GetBaseClass(i), level + 1);
}
}
inline bool IsClassDerivedFrom(IBaseType* pBaseType, const char* name)
{
for (size_t i = 0; i < pBaseType->GetNumBaseClasses(); i++)
{
if (strcmp(GetTypeName(pBaseType->GetTypeInfo()), name) == 0) {
return true;
}
if (IsClassDerivedFrom(pBaseType->GetBaseClass(i), name)) {
return true;
}
}
return false;
}
inline void PrintTypeTree(void *pClass)
{
IType *pType = GetType(pClass);
IBaseType *pBase = pType->GetBaseType();
DumpType(pBase, 0);
pType->Destroy();
}
#endif // RTTI_H_