MyGUI  3.2.0
MyGUI_RTTI.h
Go to the documentation of this file.
1 
7 /*
8  This file is part of MyGUI.
9 
10  MyGUI is free software: you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  MyGUI is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
22 */
23 #ifndef __MYGUI_RTTI_H__
24 #define __MYGUI_RTTI_H__
25 
26 #include "MyGUI_Prerequest.h"
27 #include "MyGUI_Diagnostic.h"
28 #include <string>
29 
30 #ifndef MYGUI_RTTI_DISABLE_TYPE_INFO
31 #include <typeinfo>
32 #endif
33 
34 namespace MyGUI
35 {
36 #ifndef MYGUI_RTTI_DISABLE_TYPE_INFO
37  #define MYGUI_RTTI_TYPE const std::type_info&
38  #define MYGUI_RTTI_GET_TYPE(type) typeid(type)
39 #else
40  #define MYGUI_RTTI_TYPE const std::string&
41  #define MYGUI_RTTI_GET_TYPE(type) type::getClassTypeName()
42 #endif
43 
44  //VC++ 7.1
45  #if MYGUI_COMPILER == MYGUI_COMPILER_MSVC && MYGUI_COMP_VER <= 1310
46  #define MYGUI_DECLARE_TYPE_NAME(Type) \
47  private: \
48  struct TypeNameHolder { const std::string& getClassTypeName() { static std::string type = #Type; return type; } }; \
49  public: \
50  static const std::string& getClassTypeName() { TypeNameHolder type; return type.getClassTypeName(); } \
51  \
52  virtual const std::string& getTypeName() const { return getClassTypeName(); }
53  #else
54  #define MYGUI_DECLARE_TYPE_NAME(Type) \
55  public: \
56  static const std::string& getClassTypeName() { static std::string type = #Type; return type; } \
57  \
58  virtual const std::string& getTypeName() const { return getClassTypeName(); }
59  #endif
60 
61  #define MYGUI_RTTI_BASE(BaseType) \
62  public: \
63  typedef BaseType RTTIBase; \
64  MYGUI_DECLARE_TYPE_NAME(BaseType) \
65  \
66  virtual bool isType(MYGUI_RTTI_TYPE _type) const { return MYGUI_RTTI_GET_TYPE(BaseType) == _type; } \
67  \
68  template<typename Type> bool isType() const { return isType(MYGUI_RTTI_GET_TYPE(Type)); } \
69  \
72  template<typename Type> Type* castType(bool _throw = true) \
73  { \
74  if (this->isType<Type>()) return static_cast<Type*>(this); \
75  MYGUI_ASSERT(!_throw, "Error cast type '" << this->getTypeName() << "' to type '" << Type::getClassTypeName() << "' .") \
76  return nullptr; \
77  } \
78  \
81  template<typename Type> const Type* castType(bool _throw = true) const \
82  { \
83  if (this->isType<Type>()) return static_cast<Type*>(this); \
84  MYGUI_ASSERT(!_throw, "Error cast type '" << this->getTypeName() << "' to type '" << Type::getClassTypeName() << "' .") \
85  return nullptr; \
86  }
87 
88  #define MYGUI_RTTI_DERIVED(DerivedType) \
89  public: \
90  MYGUI_DECLARE_TYPE_NAME(DerivedType) \
91  typedef RTTIBase Base; \
92  typedef DerivedType RTTIBase; \
93  \
94  virtual bool isType(MYGUI_RTTI_TYPE _type) const { return MYGUI_RTTI_GET_TYPE(DerivedType) == _type || Base::isType(_type); } \
95  \
96  template<typename Type> bool isType() const { return isType(MYGUI_RTTI_GET_TYPE(Type)); }
97 
98 } // namespace MyGUI
99 
100 #endif // __MYGUI_RTTI_H__