MyGUI 3.5.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 \
49 template<typename Type> \
50 Type* castType(bool _throw = true) \
51 { \
52 if (this->isType<Type>()) \
53 return static_cast<Type*>(this); \
54 if (_throw) \
55 MyGUI::detail::throwBadCast(this->getTypeName(), Type::getClassTypeName(), __FILE__, __LINE__); \
56 return nullptr; \
57 } \
58 \
61 template<typename Type> \
62 const Type* castType(bool _throw = true) const \
63 { \
64 if (this->isType<Type>()) \
65 return static_cast<const Type*>(this); \
66 if (_throw) \
67 MyGUI::detail::throwBadCast(this->getTypeName(), Type::getClassTypeName(), __FILE__, __LINE__); \
68 return nullptr; \
69 }
70
71#define MYGUI_RTTI_DERIVED(DerivedType) \
72public: \
73 MYGUI_DECLARE_TYPE_NAME(DerivedType, override) \
74 typedef RTTIBase Base; \
75 typedef DerivedType RTTIBase; \
76 \
77 bool isType(const std::type_info& _type) const override \
78 { \
79 return typeid(DerivedType) == _type || Base::isType(_type); \
80 } \
81 \
82 template<typename Type> \
83 bool isType() const \
84 { \
85 return isType(typeid(Type)); \
86 }
87
88} // namespace MyGUI
89
90#endif // MYGUI_RTTI_H_