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