p_dynamic_cast.h

Go to the documentation of this file.
00001 /*
00002  *  p_dynamic_cast.cpp - Pentagram Dynamic Cast Emulation Header
00003  *
00004  *  Copyright (C) 2003-2006  The Pentagram Team
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00019  */
00020 
00021 #ifndef P_DYNAMIC_CAST_H
00022 #define P_DYNAMIC_CAST_H
00023 
00024 // The Pentagram dynamic cast
00025 template<class A, class B> inline A p_dynamic_cast (B *object)
00026 {
00027         if (object && object->IsOfType(static_cast<A>(0)->ClassType)) return static_cast<A>(object);
00028         return 0;
00029 }
00030 
00031 // This is just a 'type' used to differentiate each class.
00032 struct RunTimeClassType
00033 {
00034         const char *    class_name;
00035         inline bool operator == (const RunTimeClassType &other) const { return this == &other; }
00036 };
00037 
00038 //
00039 // Include this in every class. It sets up the class to be able to use 
00040 // p_dynamic_cast. Make sure this is public!
00041 //
00042 #define ENABLE_RUNTIME_CLASSTYPE()                                                                                              \
00043         static const RunTimeClassType   ClassType;                                                                      \
00044         virtual bool IsOfType(const RunTimeClassType & type);                                           \
00045         virtual bool IsOfType(const char * type);                                                                       \
00046         template<class Type> inline bool IsOfType() { return IsOfType(Type::ClassType); }       \
00047         virtual const RunTimeClassType & GetClassType() { return ClassType; }
00048         
00049 
00050 
00051 //
00052 // Define this in the source files of base classes
00053 //
00054 #define DEFINE_RUNTIME_CLASSTYPE_CODE_BASE_CLASS(Classname)                     \
00055 const RunTimeClassType Classname::ClassType = {                                         \
00056         #Classname                                                                                                              \
00057 };                                                                                                                                      \
00058                                                                                                                                         \
00059 bool Classname::IsOfType(const RunTimeClassType & type)                         \
00060 {                                                                                                                                       \
00061         if (type == ClassType) return true;                                                             \
00062         return false;                                                                                                   \
00063 }                                                                                                                                       \
00064                                                                                                                                         \
00065 bool Classname::IsOfType(const char * type)                                                     \
00066 {                                                                                                                                       \
00067         if (!std::strcmp(type,ClassType.class_name)) return true;               \
00068         return false;                                                                                                   \
00069 }
00070 
00071 
00072 //
00073 // Define this in the source files of child classes, with 1 parent
00074 //
00075 #define DEFINE_RUNTIME_CLASSTYPE_CODE(Classname,ParentClassname)        \
00076 const RunTimeClassType Classname::ClassType = {                                         \
00077         #Classname                                                                                                              \
00078 };                                                                                                                                      \
00079                                                                                                                                         \
00080 bool Classname::IsOfType(const RunTimeClassType & type)                         \
00081 {                                                                                                                                       \
00082         if (type == ClassType) return true;                                                             \
00083         return ParentClassname::IsOfType(type);                                                 \
00084 }                                                                                                                                       \
00085                                                                                                                                         \
00086 bool Classname::IsOfType(const char * type)                                                     \
00087 {                                                                                                                                       \
00088         if (!std::strcmp(type,ClassType.class_name)) return true;               \
00089         return ParentClassname::IsOfType(type);                                                 \
00090 }
00091 
00092 
00093 
00094 //
00095 // Define this in the source files of child classes, with 2 parents
00096 //
00097 #define DEFINE_RUNTIME_CLASSTYPE_CODE_MULTI2(Classname,Parent1,Parent2) \
00098 const RunTimeClassType Classname::ClassType = {                                                 \
00099         #Classname                                                                                                                      \
00100 };                                                                                                                                              \
00101                                                                                                                                                 \
00102 bool Classname::IsOfType(const RunTimeClassType &type)                                  \
00103 {                                                                                                                                               \
00104         typedef Parent1 P1;                                                                                                     \
00105         typedef Parent2 P2;                                                                                                     \
00106         if (type == ClassType) return true;                                                                     \
00107         bool ret = P1::IsOfType(type);                                                                          \
00108         if (ret) return true;                                                                                           \
00109         return P2::IsOfType(type);                                                                                      \
00110 }                                                                                                                                               \
00111                                                                                                                                                 \
00112 bool Classname::IsOfType(const char * type)                                                             \
00113 {                                                                                                                                               \
00114         typedef Parent1 P1;                                                                                                     \
00115         typedef Parent2 P2;                                                                                                     \
00116         if (!std::strcmp(type,ClassType.class_name)) return true;                       \
00117         bool ret = P1::IsOfType(type);                                                                          \
00118         if (ret) return true;                                                                                           \
00119         return P2::IsOfType(type);                                                                                      \
00120 }
00121 
00122 #endif //P_RUNTIME_CLASSTYPE

Generated on Fri Jul 27 22:27:28 2007 for pentagram by  doxygen 1.4.7