• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

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_Common.h"
00029 #include <typeinfo>
00030 
00031 namespace MyGUI
00032 {
00033 
00034     //VC++ 7.1
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     //OBSOLETE
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     //OBSOLETE
00099     #define MYGUI_RTTI_BASE_HEADER( BaseType ) MYGUI_RTTI_BASE( BaseType )
00100 
00101 } // namespace MyGUI
00102 
00103 #endif // __MYGUI_RTTI_H__

Generated on Sun Jan 30 2011 for MyGUI by  doxygen 1.7.1