GizmoBase

rtti.cpp

Example which shows how to use RTTI information. The source code can also be found in the examples directory of the GizmoBase distribution.

00001 // *****************************************************************************
00002 // File         : rtti.cpp
00003 // Module       : 
00004 // Description  : Example of specialised rtti in GizmoBase
00005 // Author       : Anders Modén      
00006 // Product      : GizmoBase 2.1.1
00007 //      
00008 // Copyright © 1998 Anders Modén , Sweden       
00009 //          
00010 // NOTE:    The GIZMO package defines a general purpose API for large model
00011 //          visualisation and advanced graphic behaviours. The package is similar
00012 //          to Cosmo3D, Inventor, Performer etc. but adds automated behaviour
00013 //          patterns to the graphics. 
00014 //
00015 //
00016 // Revision History...                          
00017 //                                  
00018 // Who  Date    Description                     
00019 //                                  
00020 // AMO  040209  Created file    
00021 //
00022 // ******************************************************************************
00023 
00024 #include "gzBaseLibrary.h"
00025 
00026 class MyBaseClass : public gzTypeInterface
00027 {
00028 public:
00029     GZ_DECLARE_TYPE_INTERFACE;      // You need this line to add type info to your class
00030 
00031     MyBaseClass():a(10){}           // Create the member with a default value of 10
00032 
00033     int a;
00034 
00035 };
00036 
00037 
00038 class MyDerivedClass : public MyBaseClass
00039 {
00040 public:
00041 
00042     GZ_DECLARE_TYPE_INTERFACE;      // You need this line to add type info to your class
00043 
00044 };
00045 
00046 class MySecondDerivedClass : public MyBaseClass
00047 {
00048 public:
00049 
00050     GZ_DECLARE_TYPE_INTERFACE;      // You need this line to add type info to your class
00051 
00052 };
00053 
00054 
00055 class MyVeryDerivedClass : public MyDerivedClass
00056 {
00057 public:
00058 
00059     GZ_DECLARE_TYPE_INTERFACE;      // You need this line to add type info to your class
00060 
00061 };
00062 
00063 
00064 
00065 
00066 // You need this in the implementation file
00067 
00068 
00069 GZ_DECLARE_TYPE_BASE(MyBaseClass,"MyBaseClass");
00070 GZ_DECLARE_TYPE_CHILD(MyBaseClass,MyDerivedClass,"MyDerivedClass");
00071 GZ_DECLARE_TYPE_CHILD(MyBaseClass,MySecondDerivedClass,"MySecondDerivedClass");
00072 GZ_DECLARE_TYPE_CHILD(MyDerivedClass,MyVeryDerivedClass,"MyVeryDerivedClass");
00073 
00074 gzVoid printInfo(gzTypeInterface *pClass)
00075 {
00076     // The standard way to do it...
00077 
00078     MyBaseClass *pMyBaseClass=NULL;
00079 
00080     if(pClass->isOfType(MyBaseClass::getClassType()))
00081     {
00082         pMyBaseClass=(MyBaseClass *)pClass;     // Now the pointer has a value
00083     }
00084 
00085     // The neat way to do it
00086 
00087     pMyBaseClass=gzDynamic_Cast<MyBaseClass>(pClass);
00088 
00089     if(pMyBaseClass)
00090     {
00091         GZMESSAGE(GZ_MESSAGE_NOTICE,"The instance is a '%s' type",pClass->getTypeName());
00092 
00093         if(pMyBaseClass->isExactType(MyVeryDerivedClass::getClassType()))
00094         {
00095             GZMESSAGE(GZ_MESSAGE_NOTICE,"The instance is really a very very '%s' type",pClass->getTypeName());
00096         }
00097     }
00098 
00099 }
00100 
00101 int main(int argc , char *argv[] )
00102 {
00103     try
00104     {
00105     
00106         MyDerivedClass *pDerivedClass=new MyDerivedClass;       // Lets make a derived class
00107 
00108         printInfo(pDerivedClass);
00109 
00110         delete pDerivedClass;
00111 
00112 
00113 
00114         MyVeryDerivedClass *pVeryDerivedClass=new MyVeryDerivedClass;       // Lets make a very derived class
00115 
00116         printInfo(pVeryDerivedClass);
00117 
00118         delete pVeryDerivedClass;
00119         
00120     }
00121     catch(gzBaseError &error)   // In case of exceptions thrown we want to print the message
00122     {
00123         error.reportError();
00124     }
00125 
00126     return 0;
00127 }
00128 

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