MyGUI: MyGUI_RTTI.h Source File

MyGUI 3.2.2

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
16 namespace MyGUI
17 {
18 #define MYGUI_RTTI_TYPE const std::type_info&
19 #define MYGUI_RTTI_GET_TYPE(type) typeid(type)
20
21 //VC++ 7.1
22 #if MYGUI_COMPILER == MYGUI_COMPILER_MSVC && MYGUI_COMP_VER <= 1310
23 # define MYGUI_DECLARE_TYPE_NAME(Type) \
24 private: \
25 struct TypeNameHolder { const std::string& getClassTypeName() { static std::string type = #Type; return type; } }; \
26 public: \
27 static const std::string& getClassTypeName() { TypeNameHolder type; return type.getClassTypeName(); } \
28 \
29 virtual const std::string& getTypeName() const { return getClassTypeName(); }
30 #else
31 # define MYGUI_DECLARE_TYPE_NAME(Type) \
32 public: \
33 static const std::string& getClassTypeName() { static std::string type = #Type; return type; } \
34 \
35 virtual const std::string& getTypeName() const { return getClassTypeName(); }
36 #endif
37
38 #define MYGUI_RTTI_BASE(BaseType) \
39 public: \
40 typedef BaseType RTTIBase; \
41 MYGUI_DECLARE_TYPE_NAME(BaseType) \
42 \
43 virtual bool isType(MYGUI_RTTI_TYPE _type) const { return MYGUI_RTTI_GET_TYPE(BaseType) == _type; } \
44 \
45 template<typename Type> bool isType() const { return isType(MYGUI_RTTI_GET_TYPE(Type)); } \
46 \
49 template<typename Type> Type* castType(bool _throw = true) \
50 { \
51 if (this->isType<Type>()) return static_cast<Type*>(this); \
52 MYGUI_ASSERT(!_throw, "Error cast type '" << this->getTypeName() << "' to type '" << Type::getClassTypeName() << "' .") \
53 return nullptr; \
54 } \
55 \
58 template<typename Type> const Type* castType(bool _throw = true) const \
59 { \
60 if (this->isType<Type>()) return static_cast<Type*>(this); \
61 MYGUI_ASSERT(!_throw, "Error cast type '" << this->getTypeName() << "' to type '" << Type::getClassTypeName() << "' .") \
62 return nullptr; \
63 }
64
65 #define MYGUI_RTTI_DERIVED(DerivedType) \
66 public: \
67 MYGUI_DECLARE_TYPE_NAME(DerivedType) \
68 typedef RTTIBase Base; \
69 typedef DerivedType RTTIBase; \
70 \
71 virtual bool isType(MYGUI_RTTI_TYPE _type) const { return MYGUI_RTTI_GET_TYPE(DerivedType) == _type || Base::isType(_type); } \
72 \
73 template<typename Type> bool isType() const { return isType(MYGUI_RTTI_GET_TYPE(Type)); }
74
75 } // namespace MyGUI
76
77 #endif // MYGUI_RTTI_H_
MyGUI_Diagnostic.h
MyGUI_Prerequest.h
MyGUI
Definition: MyGUI_ActionController.h:14