NSMBW-Decomp
A decompilation of New Super Mario Bros. Wii
Loading...
Searching...
No Matches
ut_RuntimeTypeInfo.h
1#ifndef NW4R_UT_RUNTIME_TYPE_INFO_H
2#define NW4R_UT_RUNTIME_TYPE_INFO_H
3#include <nw4r/types_nw4r.h>
4
5namespace nw4r {
6namespace ut {
7namespace detail {
8
9/******************************************************************************
10 *
11 * RuntimeTypeInfo
12 *
13 ******************************************************************************/
14struct RuntimeTypeInfo {
15 explicit RuntimeTypeInfo(const RuntimeTypeInfo* pBase)
16 : mParentTypeInfo(pBase) {}
17
18 bool IsDerivedFrom(const RuntimeTypeInfo* pInfo) const {
19 for (const RuntimeTypeInfo* pIt = this; pIt != NULL;
20 pIt = pIt->mParentTypeInfo) {
21
22 if (pIt == pInfo) {
23 return true;
24 }
25 }
26
27 return false;
28 }
29
30 const RuntimeTypeInfo* mParentTypeInfo; // at 0x0
31};
32
33template <typename T>
34inline const RuntimeTypeInfo* GetTypeInfoFromPtr_(T* /* pPtr */) {
35 return &T::typeInfo;
36}
37
38} // namespace detail
39
40/******************************************************************************
41 *
42 * DynamicCast
43 *
44 ******************************************************************************/
45template <typename TDerived, typename TBase>
46inline TDerived DynamicCast(TBase* pPtr) {
47 const detail::RuntimeTypeInfo* pDerivedTypeInfo =
48 detail::GetTypeInfoFromPtr_(static_cast<TDerived>(NULL));
49
50 // Downcast only if possible
51 if (pPtr) {
52 if (pPtr->GetRuntimeTypeInfo()->IsDerivedFrom(pDerivedTypeInfo)) {
53 return static_cast<TDerived>(pPtr);
54 }
55 }
56
57 return NULL;
58}
59
60} // namespace ut
61} // namespace nw4r
62
63/******************************************************************************
64 *
65 * Macros
66 *
67 ******************************************************************************/
68/**
69 * Declare type RTTI and accessor function.
70 */
71#define NW4R_UT_RTTI_DECL(T) \
72 virtual const nw4r::ut::detail::RuntimeTypeInfo* GetRuntimeTypeInfo() \
73 const { \
74 return &typeInfo; \
75 } \
76 \
77 static nw4r::ut::detail::RuntimeTypeInfo typeInfo;
78
79/**
80 * Define type RTTI (base type).
81 */
82#define NW4R_UT_RTTI_DEF_BASE(T) \
83 nw4r::ut::detail::RuntimeTypeInfo T::typeInfo(NULL)
84
85/**
86 * Define type RTTI (derived type).
87 */
88#define NW4R_UT_RTTI_DEF_DERIVED(T, BASE) \
89 nw4r::ut::detail::RuntimeTypeInfo T::typeInfo(&BASE::typeInfo)
90
91#endif
Debugging library which includes various utilities used by the rest of nw4r.
Definition ut_list.cpp:4