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_Diagnostic.h"
00029 #include <typeinfo>
00030 #include <string>
00031
00032 namespace MyGUI
00033 {
00034
00035
00036 #if MYGUI_COMPILER == MYGUI_COMPILER_MSVC && MYGUI_COMP_VER == 1310
00037 #define MYGUI_DECLARE_TYPE_NAME( Type ) \
00038 private: \
00039 struct TypeNameHolder { const std::string& getClassTypeName() { static std::string type = #Type; return type; } }; \
00040 public: \
00041 static const std::string& getClassTypeName() { TypeNameHolder type; return type.getClassTypeName(); } \
00042 \
00043 virtual const std::string& getTypeName() const { return Type::getClassTypeName(); }
00044 #else
00045 #define MYGUI_DECLARE_TYPE_NAME( Type ) \
00046 public: \
00047 static const std::string& getClassTypeName() { static std::string type = #Type; return type; } \
00048 \
00049 virtual const std::string& getTypeName() const { return Type::getClassTypeName(); }
00050 #endif
00051
00052 #define MYGUI_RTTI_BASE( BaseType ) \
00053 public: \
00054 typedef BaseType RTTIBase; \
00055 MYGUI_DECLARE_TYPE_NAME( BaseType ) \
00056 \
00057 virtual bool isType( const std::type_info& _type) const { return typeid( BaseType ) == _type; } \
00058 \
00059 template<typename Type> bool isType() const { return isType( typeid( Type )); } \
00060 \
00063 template<typename Type> Type* castType(bool _throw = true) \
00064 { \
00065 if (this->isType<Type>()) return static_cast<Type*>( this ); \
00066 MYGUI_ASSERT(!_throw, "Error cast type '" << this->getTypeName() << "' to type '" << Type::getClassTypeName() << "' .") \
00067 return nullptr; \
00068 } \
00069 \
00072 template<typename Type> const Type* castType(bool _throw = true) const \
00073 { \
00074 if (this->isType<Type>()) return static_cast<Type*>( this ); \
00075 MYGUI_ASSERT(!_throw, "Error cast type '" << this->getTypeName() << "' to type '" << Type::getClassTypeName() << "' .") \
00076 return nullptr; \
00077 }
00078
00079 #define MYGUI_RTTI_DERIVED( DerivedType ) \
00080 public: \
00081 MYGUI_DECLARE_TYPE_NAME( DerivedType ) \
00082 typedef RTTIBase Base; \
00083 typedef DerivedType RTTIBase; \
00084 \
00085 virtual bool isType( const std::type_info& _type ) const { return typeid( DerivedType ) == _type || Base::isType( _type ); } \
00086 \
00087 template<typename Type> bool isType() const { return isType( typeid( DerivedType )); }
00088
00089
00090 #define MYGUI_RTTI_CHILD_HEADER( DerivedType, BaseType ) \
00091 public: \
00092 MYGUI_DECLARE_TYPE_NAME( DerivedType ) \
00093 typedef BaseType Base; \
00094 typedef DerivedType RTTIBase; \
00095 \
00096 virtual bool isType( const std::type_info& _type ) const { return typeid( DerivedType ) == _type || BaseType::isType( _type ); }
00097
00098
00099
00100 #define MYGUI_RTTI_BASE_HEADER( BaseType ) MYGUI_RTTI_BASE( BaseType )
00101
00102 }
00103
00104 #endif // __MYGUI_RTTI_H__