MyGUI  3.2.0
MyGUI_RTTI.h
Go to the documentation of this file.
00001 
00007 /*
00008     This file is part of MyGUI.
00009 
00010     MyGUI is free software: you can redistribute it and/or modify
00011     it under the terms of the GNU Lesser General Public License as published by
00012     the Free Software Foundation, either version 3 of the License, or
00013     (at your option) any later version.
00014 
00015     MyGUI is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018     GNU Lesser General Public License for more details.
00019 
00020     You should have received a copy of the GNU Lesser General Public License
00021     along with MyGUI.  If not, see <http://www.gnu.org/licenses/>.
00022 */
00023 #ifndef __MYGUI_RTTI_H__
00024 #define __MYGUI_RTTI_H__
00025 
00026 #include "MyGUI_Prerequest.h"
00027 #include "MyGUI_Diagnostic.h"
00028 #include <string>
00029 
00030 #ifndef MYGUI_RTTI_DISABLE_TYPE_INFO
00031 #include <typeinfo>
00032 #endif
00033 
00034 namespace MyGUI
00035 {
00036 #ifndef MYGUI_RTTI_DISABLE_TYPE_INFO
00037     #define MYGUI_RTTI_TYPE const std::type_info&
00038     #define MYGUI_RTTI_GET_TYPE(type) typeid(type)
00039 #else
00040     #define MYGUI_RTTI_TYPE const std::string&
00041     #define MYGUI_RTTI_GET_TYPE(type) type::getClassTypeName()
00042 #endif
00043 
00044     //VC++ 7.1
00045     #if MYGUI_COMPILER == MYGUI_COMPILER_MSVC && MYGUI_COMP_VER <= 1310
00046         #define MYGUI_DECLARE_TYPE_NAME(Type) \
00047         private: \
00048             struct TypeNameHolder { const std::string& getClassTypeName() { static std::string type = #Type; return type; } }; \
00049         public: \
00050             static const std::string& getClassTypeName() { TypeNameHolder type; return type.getClassTypeName(); } \
00051  \
00052             virtual const std::string& getTypeName() const { return getClassTypeName(); }
00053     #else
00054         #define MYGUI_DECLARE_TYPE_NAME(Type) \
00055         public: \
00056             static const std::string& getClassTypeName() { static std::string type = #Type; return type; } \
00057  \
00058             virtual const std::string& getTypeName() const { return getClassTypeName(); }
00059     #endif
00060 
00061     #define MYGUI_RTTI_BASE(BaseType) \
00062         public: \
00063             typedef BaseType RTTIBase; \
00064             MYGUI_DECLARE_TYPE_NAME(BaseType) \
00065  \
00066             virtual bool isType(MYGUI_RTTI_TYPE _type) const { return MYGUI_RTTI_GET_TYPE(BaseType) == _type; } \
00067  \
00068             template<typename Type> bool isType() const { return isType(MYGUI_RTTI_GET_TYPE(Type)); } \
00069  \
00072             template<typename Type> Type* castType(bool _throw = true) \
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  \
00081             template<typename Type> const Type* castType(bool _throw = true) const \
00082             { \
00083                 if (this->isType<Type>()) return static_cast<Type*>(this); \
00084                 MYGUI_ASSERT(!_throw, "Error cast type '" << this->getTypeName() << "' to type '" << Type::getClassTypeName() << "' .") \
00085                 return nullptr; \
00086             }
00087 
00088     #define MYGUI_RTTI_DERIVED(DerivedType) \
00089         public: \
00090             MYGUI_DECLARE_TYPE_NAME(DerivedType) \
00091             typedef RTTIBase Base; \
00092             typedef DerivedType RTTIBase; \
00093  \
00094             virtual bool isType(MYGUI_RTTI_TYPE _type) const { return MYGUI_RTTI_GET_TYPE(DerivedType) == _type || Base::isType(_type); } \
00095  \
00096             template<typename Type> bool isType() const { return isType(MYGUI_RTTI_GET_TYPE(Type)); }
00097 
00098 } // namespace MyGUI
00099 
00100 #endif // __MYGUI_RTTI_H__