MyGUI 3.0.1
MyGUI_RTTI.h
Go to the documentation of this file.
00001 
00008 /*
00009     This file is part of MyGUI.
00010 
00011     MyGUI is free software: you can redistribute it and/or modify
00012     it under the terms of the GNU Lesser General Public License as published by
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your option) any later version.
00015 
00016     MyGUI is distributed in the hope that it will be useful,
00017     but WITHOUT ANY WARRANTY; without even the implied warranty of
00018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019     GNU Lesser General Public License for more details.
00020 
00021     You should have received a copy of the GNU Lesser General Public License
00022     along with MyGUI.  If not, see <http://www.gnu.org/licenses/>.
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     //VC++ 7.1
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     //OBSOLETE
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     //OBSOLETE
00100     #define MYGUI_RTTI_BASE_HEADER( BaseType ) MYGUI_RTTI_BASE( BaseType )
00101 
00102 } // namespace MyGUI
00103 
00104 #endif // __MYGUI_RTTI_H__