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->GetRuntimeTypeInfo()->IsDerivedFrom(pDerivedTypeInfo)) {
52 return static_cast<TDerived>(pPtr);
53 }
54
55 return NULL;
56}
57
58} // namespace ut
59} // namespace nw4r
60
61/******************************************************************************
62 *
63 * Macros
64 *
65 ******************************************************************************/
66/**
67 * Declare type RTTI and accessor function.
68 */
69#define NW4R_UT_RTTI_DECL(T) \
70 virtual const nw4r::ut::detail::RuntimeTypeInfo* GetRuntimeTypeInfo() \
71 const { \
72 return &typeInfo; \
73 } \
74 \
75 static nw4r::ut::detail::RuntimeTypeInfo typeInfo;
76
77/**
78 * Define type RTTI (base type).
79 */
80#define NW4R_UT_RTTI_DEF_BASE(T) \
81 nw4r::ut::detail::RuntimeTypeInfo T::typeInfo(NULL)
82
83/**
84 * Define type RTTI (derived type).
85 */
86#define NW4R_UT_RTTI_DEF_DERIVED(T, BASE) \
87 nw4r::ut::detail::RuntimeTypeInfo T::typeInfo(&BASE::typeInfo)
88
89#endif
Debugging library which includes various utilities used by the rest of nw4r.
Definition ut_list.cpp:4