sead
Loading...
Searching...
No Matches
seadRuntimeTypeInfo.h
Go to the documentation of this file.
1#ifndef SEAD_RUNTIMETYPEINFO_H_
2#define SEAD_RUNTIMETYPEINFO_H_
3
4#include <basis/seadTypes.h>
5
6namespace sead { namespace RuntimeTypeInfo {
7
9{
11
12 virtual bool isDerived(const Interface* other) const = 0;
13};
14#ifdef cafe
15static_assert(sizeof(Interface) == 4, "sead::RuntimeTypeInfo::Interface size mismatch");
16#endif // cafe
17
18struct Root : public Interface
19{
20 Root() { }
21
22 bool isDerived(const Interface* other) const override
23 {
24 return other == this;
25 }
26};
27
28template <typename Super>
29struct Derive : public Interface
30{
31 Derive() { }
32
33 bool isDerived(const Interface* other) const override
34 {
35 if (this == other)
36 return true;
37
38 const RuntimeTypeInfo::Interface* rootTypeInfo = Super::getRuntimeTypeInfoStatic();
39 return rootTypeInfo->isDerived(other);
40 }
41};
42
43} // namespace sead::RuntimeTypeInfo
44
45template <typename DerivedType, typename Type>
46inline bool
47IsDerivedTypes(const Type* ptr)
48{
49 const RuntimeTypeInfo::Interface* typeInfo = DerivedType::getRuntimeTypeInfoStatic();
50 return ptr != nullptr && ptr->checkDerivedRuntimeTypeInfo(typeInfo);
51}
52
53template<typename DerivedType, typename Type>
54inline DerivedType*
55DynamicCast(Type* ptr)
56{
57 if (IsDerivedTypes<DerivedType, Type>(ptr))
58 return static_cast<DerivedType*>(ptr);
59
60 return nullptr;
61}
62
63template<typename DerivedType, typename Type>
64inline const DerivedType*
65DynamicCast(const Type* ptr)
66{
67 if (IsDerivedTypes<DerivedType, Type>(ptr))
68 return static_cast<const DerivedType*>(ptr);
69
70 return nullptr;
71}
72
73} // namespace sead
74
75#define SEAD_RTTI_BASE(CLASS)
76 private:
77 static const sead::RuntimeTypeInfo::Root sTypeInfo;
78
79 public:
80 static const sead::RuntimeTypeInfo::Interface* getRuntimeTypeInfoStatic()
81 {
82 return &sTypeInfo;
83 }
84
85 virtual bool checkDerivedRuntimeTypeInfo(const sead::RuntimeTypeInfo::Interface* type) const
86 {
87 const sead::RuntimeTypeInfo::Interface* clsTypeInfo = CLASS::getRuntimeTypeInfoStatic();
88 return type == clsTypeInfo;
89 }
90
91 virtual const sead::RuntimeTypeInfo::Interface* getRuntimeTypeInfo() const
92 {
93 return getRuntimeTypeInfoStatic();
94 }
95
96#define SEAD_RTTI_OVERRIDE(CLASS, BASE)
97 private:
98 static const sead::RuntimeTypeInfo::Derive<BASE> sTypeInfo;
99
100 public:
101 static const sead::RuntimeTypeInfo::Interface* getRuntimeTypeInfoStatic()
102 {
103 return &sTypeInfo;
104 }
105
106 bool checkDerivedRuntimeTypeInfo(const sead::RuntimeTypeInfo::Interface* type) const override
107 {
108 const sead::RuntimeTypeInfo::Interface* clsTypeInfo = CLASS::getRuntimeTypeInfoStatic();
109 if (type == clsTypeInfo)
110 return true;
111
112 return BASE::checkDerivedRuntimeTypeInfo(type);
113 }
114
115 const sead::RuntimeTypeInfo::Interface* getRuntimeTypeInfo() const override
116 {
117 return getRuntimeTypeInfoStatic();
118 }
119
120// Used for templates when inline definition of the sTypeInfo is required.
121#define SEAD_RTTI_OVERRIDE_INLINE(CLASS, BASE)
122 private:
123 static inline const sead::RuntimeTypeInfo::Derive<BASE> sTypeInfo{};
124
125 public:
126 static const sead::RuntimeTypeInfo::Interface* getRuntimeTypeInfoStatic()
127 {
128 return &sTypeInfo;
129 }
130
131 bool checkDerivedRuntimeTypeInfo(const sead::RuntimeTypeInfo::Interface* type) const override
132 {
133 const sead::RuntimeTypeInfo::Interface* clsTypeInfo = CLASS::getRuntimeTypeInfoStatic();
134 if (type == clsTypeInfo)
135 return true;
136
137 return BASE::checkDerivedRuntimeTypeInfo(type);
138 }
139
140 const sead::RuntimeTypeInfo::Interface* getRuntimeTypeInfo() const override
141 {
142 return getRuntimeTypeInfoStatic();
143 }
144
145#define SEAD_RTTI_BASE_IMPL(CLASS)
146 const sead::RuntimeTypeInfo::Root CLASS::sTypeInfo;
147
148#define SEAD_RTTI_OVERRIDE_IMPL(CLASS, BASE)
149 const sead::RuntimeTypeInfo::Derive<BASE> CLASS::sTypeInfo;
150
151#endif // SEAD_RUNTIMETYPEINFO_H_
Definition seadRuntimeTypeInfo.h:6
Definition seadAssert.h:44
const DerivedType * DynamicCast(const Type *ptr)
Definition seadRuntimeTypeInfo.h:65
DerivedType * DynamicCast(Type *ptr)
Definition seadRuntimeTypeInfo.h:55
bool IsDerivedTypes(const Type *ptr)
Definition seadRuntimeTypeInfo.h:47
Definition seadRuntimeTypeInfo.h:30
bool isDerived(const Interface *other) const override
Definition seadRuntimeTypeInfo.h:33
Derive()
Definition seadRuntimeTypeInfo.h:31
Definition seadRuntimeTypeInfo.h:9
Interface()
Definition seadRuntimeTypeInfo.h:10
virtual bool isDerived(const Interface *other) const =0
Definition seadRuntimeTypeInfo.h:19
Root()
Definition seadRuntimeTypeInfo.h:20
bool isDerived(const Interface *other) const override
Definition seadRuntimeTypeInfo.h:22