MyGUI  3.4.0
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 
19 #define MYGUI_DECLARE_TYPE_NAME(Type, Override) \
20  public: \
21  static const std::string& getClassTypeName() { static std::string type = #Type; return type; } \
22  \
23  virtual const std::string& getTypeName() const Override { return getClassTypeName(); }
24 
25 #define MYGUI_RTTI_BASE(BaseType) \
26  public: \
27  typedef BaseType RTTIBase; \
28  MYGUI_DECLARE_TYPE_NAME(BaseType,) \
29  \
30  virtual bool isType(const std::type_info& _type) const { return typeid(BaseType) == _type; } \
31  \
32  template<typename Type> bool isType() const { return isType(typeid(Type)); } \
33  \
36  template<typename Type> Type* castType(bool _throw = true) \
37  { \
38  if (this->isType<Type>()) return static_cast<Type*>(this); \
39  MYGUI_ASSERT(!_throw, "Error cast type '" << this->getTypeName() << "' to type '" << Type::getClassTypeName() << "' .") \
40  return nullptr; \
41  } \
42  \
45  template<typename Type> const Type* castType(bool _throw = true) const \
46  { \
47  if (this->isType<Type>()) return static_cast<Type*>(this); \
48  MYGUI_ASSERT(!_throw, "Error cast type '" << this->getTypeName() << "' to type '" << Type::getClassTypeName() << "' .") \
49  return nullptr; \
50  }
51 
52 #define MYGUI_RTTI_DERIVED(DerivedType) \
53  public: \
54  MYGUI_DECLARE_TYPE_NAME(DerivedType, override) \
55  typedef RTTIBase Base; \
56  typedef DerivedType RTTIBase; \
57  \
58  virtual bool isType(const std::type_info& _type) const override { return typeid(DerivedType) == _type || Base::isType(_type); } \
59  \
60  template<typename Type> bool isType() const { return isType(typeid(Type)); }
61 
62 } // namespace MyGUI
63 
64 #endif // MYGUI_RTTI_H_
MyGUI_Diagnostic.h
MyGUI_Prerequest.h
MyGUI
Definition: MyGUI_ActionController.h:15