MyGUI 3.4.3
MyGUI_RTTI.h
Go to the documentation of this file.
1/*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7#ifndef MYGUI_RTTI_H_
8#define MYGUI_RTTI_H_
9
10#include "MyGUI_Prerequest.h"
11#include "MyGUI_Diagnostic.h"
12#include <string>
13
14#include <typeinfo>
15
16namespace MyGUI
17{
18
19#define MYGUI_DECLARE_TYPE_NAME(Type, Override) \
20public: \
21 static std::string_view getClassTypeName() \
22 { \
23 return #Type; \
24 } \
25 \
26 virtual std::string_view getTypeName() const Override \
27 { \
28 return getClassTypeName(); \
29 }
30
31#define MYGUI_RTTI_BASE(BaseType) \
32public: \
33 typedef BaseType RTTIBase; \
34 MYGUI_DECLARE_TYPE_NAME(BaseType, ) \
35 \
36 virtual bool isType(const std::type_info& _type) const \
37 { \
38 return typeid(BaseType) == _type; \
39 } \
40 \
41 template<typename Type> \
42 bool isType() const \
43 { \
44 return isType(typeid(Type)); \
45 } \
46
48 \
49 template<typename Type> \
50 Type* castType(bool _throw = true) \
51 { \
52 if (this->isType<Type>()) \
53 return static_cast<Type*>(this); \
54 MYGUI_ASSERT( \
55 !_throw, \
56 "Error cast type '" << this->getTypeName() << "' to type '" << Type::getClassTypeName() << "' ."); \
57 return nullptr; \
58 } \
59
61 \
62 template<typename Type> \
63 const Type* castType(bool _throw = true) const \
64 { \
65 if (this->isType<Type>()) \
66 return static_cast<Type*>(this); \
67 MYGUI_ASSERT( \
68 !_throw, \
69 "Error cast type '" << this->getTypeName() << "' to type '" << Type::getClassTypeName() << "' ."); \
70 return nullptr; \
71 }
72
73#define MYGUI_RTTI_DERIVED(DerivedType) \
74public: \
75 MYGUI_DECLARE_TYPE_NAME(DerivedType, override) \
76 typedef RTTIBase Base; \
77 typedef DerivedType RTTIBase; \
78 \
79 virtual bool isType(const std::type_info& _type) const override \
80 { \
81 return typeid(DerivedType) == _type || Base::isType(_type); \
82 } \
83 \
84 template<typename Type> \
85 bool isType() const \
86 { \
87 return isType(typeid(Type)); \
88 }
89
90} // namespace MyGUI
91
92#endif // MYGUI_RTTI_H_