Go to the documentation of this file.00001
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef __MYGUI_RTTI_H__
00025 #define __MYGUI_RTTI_H__
00026
00027 #include "MyGUI_Prerequest.h"
00028 #include "MyGUI_Common.h"
00029 #include <typeinfo>
00030
00031 namespace MyGUI
00032 {
00033
00034
00035 #if MYGUI_COMPILER == MYGUI_COMPILER_MSVC && MYGUI_COMP_VER == 1310
00036 #define MYGUI_DECLARE_TYPE_NAME( Type ) \
00037 private: \
00038 struct TypeNameHolder { const std::string& getClassTypeName() { static std::string type = #Type; return type; } }; \
00039 public: \
00040 static const std::string& getClassTypeName() { TypeNameHolder type; return type.getClassTypeName(); } \
00041 \
00042 virtual const std::string& getTypeName() const { return Type::getClassTypeName(); }
00043 #else
00044 #define MYGUI_DECLARE_TYPE_NAME( Type ) \
00045 public: \
00046 static const std::string& getClassTypeName() { static std::string type = #Type; return type; } \
00047 \
00048 virtual const std::string& getTypeName() const { return Type::getClassTypeName(); }
00049 #endif
00050
00051 #define MYGUI_RTTI_BASE( BaseType ) \
00052 public: \
00053 typedef BaseType RTTIBase; \
00054 MYGUI_DECLARE_TYPE_NAME( BaseType ) \
00055 \
00056 virtual bool isType( const std::type_info& _type) const { return typeid( BaseType ) == _type; } \
00057 \
00058 template<typename Type> bool isType() const { return isType( typeid( Type )); } \
00059 \
00062 template<typename Type> Type* castType(bool _throw = true) \
00063 { \
00064 if (this->isType<Type>()) return static_cast<Type*>( this ); \
00065 MYGUI_ASSERT(!_throw, "Error cast type '" << this->getTypeName() << "' to type '" << Type::getClassTypeName() << "' .") \
00066 return nullptr; \
00067 } \
00068 \
00071 template<typename Type> const Type* castType(bool _throw = true) const \
00072 { \
00073 if (this->isType<Type>()) return static_cast<Type*>( this ); \
00074 MYGUI_ASSERT(!_throw, "Error cast type '" << this->getTypeName() << "' to type '" << Type::getClassTypeName() << "' .") \
00075 return nullptr; \
00076 }
00077
00078 #define MYGUI_RTTI_DERIVED( DerivedType ) \
00079 public: \
00080 MYGUI_DECLARE_TYPE_NAME( DerivedType ) \
00081 typedef RTTIBase Base; \
00082 typedef DerivedType RTTIBase; \
00083 \
00084 virtual bool isType( const std::type_info& _type ) const { return typeid( DerivedType ) == _type || Base::isType( _type ); } \
00085 \
00086 template<typename Type> bool isType() const { return isType( typeid( DerivedType )); }
00087
00088
00089 #define MYGUI_RTTI_CHILD_HEADER( DerivedType, BaseType ) \
00090 public: \
00091 MYGUI_DECLARE_TYPE_NAME( DerivedType ) \
00092 typedef BaseType Base; \
00093 typedef DerivedType RTTIBase; \
00094 \
00095 virtual bool isType( const std::type_info& _type ) const { return typeid( DerivedType ) == _type || BaseType::isType( _type ); }
00096
00097
00098
00099 #define MYGUI_RTTI_BASE_HEADER( BaseType ) MYGUI_RTTI_BASE( BaseType )
00100
00101 }
00102
00103 #endif // __MYGUI_RTTI_H__