Gizmo3D

gzType.h

Go to the documentation of this file.
00001 //*****************************************************************************
00002 // File         : gzType.h
00003 // Module       : gzBase
00004 // Description  : Class definition of type info utilities
00005 // Author       : Anders Modén      
00006 // Product      : GizmoBase 2.1.1
00007 //      
00008 // Copyright © 2003- Saab Training Systems AB, Sweden   
00009 //          
00010 // NOTE:    GizmoBase is a platform abstraction utility layer for C++. It contains 
00011 //          design patterns and C++ solutions for the advanced programmer.
00012 //
00013 //
00014 // Revision History...                          
00015 //                                  
00016 // Who  Date    Description                     
00017 //                                  
00018 // AMO  980915  Created file    
00019 //
00020 //******************************************************************************
00021 #ifndef __GZ_TYPE_H__
00022 #define __GZ_TYPE_H__
00023 
00059 #include "gzBasicTypes.h"
00060 
00061 const gzULong GZ_TYPE_MAX_NAME_LEN=100;
00062 
00063 #define GZ_DECLARE_TYPE_INTERFACE_EXPORT(exporter)  exporter static gzType s_type;\
00064                                                     exporter static gzType *getClassType() { return &s_type; }\
00065                                                     exporter virtual gzType *getType() const { return getClassType(); }
00066 
00067 #define GZ_DECLARE_TYPE_INTERFACE   static gzType s_type;\
00068                                     static gzType *getClassType() { return &s_type; }\
00069                                     virtual gzType *getType() const { return getClassType(); }
00070 
00071 #define GZ_DECLARE_TYPE_BASE(base,name) gzType base::s_type(0,name)
00072 
00073 #define GZ_DECLARE_TYPE_CHILD(parent,child,name) gzType child::s_type(&parent::s_type,name)
00074         
00075 //******************************************************************************
00076 // Class    : gzType
00077 //                                  
00078 // Purpose  : Run Time Type Information 
00079 //                                  
00080 // Notes    : RTTI can be supported on various compilers but this provides a generic
00081 //            interface.    
00082 //                                  
00083 // Revision History...                          
00084 //                                  
00085 // Who  Date    Description                     
00086 //                                  
00087 // AMO  980819  Created 
00088 //                                  
00089 //******************************************************************************
00090 class  GZ_BASE_EXPORT gzType
00091 {
00092 public:
00093     gzType(gzType *parent, const char *name );
00094     gzType(const gzType &right );
00095     gzType &operator=(const gzType &right );
00096     virtual ~gzType();
00097 
00098     const char *getName() const;
00099 
00100     gzType *getParent() const;
00101 
00102     gzBool isDerivedFrom(gzType *ancestor) const;
00103 
00104     gzULong hash() const;
00105 
00106     inline gzBool operator==(const gzType &type)
00107     {
00108         return (m_name==type.m_name);
00109     }
00110 
00111 
00112 private:
00113 
00114     gzType  *m_parent;
00115     char    *m_name; 
00116     gzBool  m_created;
00117 
00118 };
00119 
00121 typedef gzType * gzTypePtr;
00122 
00123 
00124 //******************************************************************************
00125 // Class    : gzTypeInterface
00126 //                                  
00127 // Purpose  : Interface functions for RTTI 
00128 //                                  
00129 // Notes    : - 
00130 //                                  
00131 // Revision History...                          
00132 //                                  
00133 // Who  Date    Description                     
00134 //                                  
00135 // AMO  980819  Created 
00136 //                                  
00137 //******************************************************************************
00138 class GZ_BASE_EXPORT gzTypeInterface 
00139 {
00140 public:
00141 
00142     gzTypeInterface(){};
00143 
00144     virtual ~gzTypeInterface(){};
00145 
00146     inline gzBool   isExactType( gzType *type ) const 
00147     {
00148         if(getType()==type)
00149             return TRUE;
00150         else
00151             return FALSE;
00152     }
00153 
00154     gzBool          isOfType(gzType *type ) const;
00155 
00156     virtual gzType  *getType() const = 0;
00157 
00158     const char      *getTypeName() const;
00159 };
00160 
00161 //******************************************************************************
00162 // Class    : gzDynamic_Cast<CastToClass>
00163 //                                  
00164 // Purpose  : Utility to do a checked dynamic cast 
00165 //                                  
00166 // Notes    : - 
00167 //                                  
00168 // Revision History...                          
00169 //                                  
00170 // Who  Date    Description                     
00171 //                                  
00172 // AMO  011105  Created 
00173 //                                  
00174 //******************************************************************************
00175 template<class T> T * gzDynamic_Cast(gzTypeInterface* pObject)
00176 {
00177     if (pObject == NULL)
00178         return NULL;
00179 
00180     if ( pObject->isOfType( T::getClassType() ) )
00181         return (T *)pObject;
00182 
00183     return NULL;
00184 }
00185 
00186 //******************************************************************************
00187 // Class    : gzDynamic_Instance_Cast<CastToClass>
00188 //                                  
00189 // Purpose  : Utility to do a checked dynamic cast of instance to pointer
00190 //                                  
00191 // Notes    : - 
00192 //                                  
00193 // Revision History...                          
00194 //                                  
00195 // Who  Date    Description                     
00196 //                                  
00197 // AMO  011105  Created 
00198 //                                  
00199 //******************************************************************************
00200 template<class T1 , class T2> T2 * gzDynamic_Instance_Cast(gzTypeInterface* pObject)
00201 {
00202     if (pObject == NULL)
00203         return NULL;
00204 
00205     if ( pObject->isOfType( T1::getClassType() ) )
00206         return (T2 *)(*(T1 *)pObject);
00207 
00208     return NULL;
00209 }
00210 
00211 
00212 #endif

Documentation for Gizmo3D generated at Wed Feb 20 11:54:12 2008 by   Saab Training Systems AB, ¸ (c) 2003-and beyond