00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00042 
00043 
00044 
00045 
00046 
00047 
00048 
00049 
00050 #ifndef STYPEINFO_H
00051 #define STYPEINFO_H
00052 
00053 
00054 #include <iostream>
00055 #include <cmath>
00056 
00057 #include <LEDA/list.h>
00058 #include <LEDA/string.h>
00059 
00060 
00061 #include "SUtil.h"
00062 
00076 
00077 class STypeInfo {
00078 public:
00079   
00085   virtual leda_string getClassifier() const =0;
00086   
00093   virtual bool isInstanceOf(const leda_string &classifier) const =0;
00094   
00099   virtual void assureInstanceOf(const leda_string &classifier) const =0;
00100 };
00101 
00102 
00103 
00104 #define TYPEINFOSTART       \
00105 private:          \
00106   static bool _validAncesAndSelf;   \
00107   static leda_list<leda_string> _ancesAndSelf;  \
00108 public:           \
00109   static const leda_list<leda_string> &getTypeInfo() {  \
00110     if( _validAncesAndSelf )      \
00111       return _ancesAndSelf;     \
00112                 \
00113     leda_list<leda_string> l;
00114 
00115 
00116 #undef dbgOn
00117 #define dbgOn false
00118 #define TYPEINFOEND(theName, theClassifier)           \
00119     _ancesAndSelf.sort();             \
00120     _ancesAndSelf.unique();             \
00121     _ancesAndSelf.append(theClassifier);          \
00122     dbgVar("ancesAndSelf", _ancesAndSelf);                                      \
00123     _validAncesAndSelf = true;                                                  \
00124     return _ancesAndSelf;             \
00125   };                    \
00126   virtual leda_string getClassifier() const { return theClassifier; };          \
00127   virtual bool isInstanceOf(const leda_string &classifier) const {              \
00128     return ( NULL != theName::getTypeInfo().search(classifier) );   \
00129   };                    \
00130   virtual void assureInstanceOf(const leda_string &classifier) const {    \
00131     if( !isInstanceOf(classifier) ) {                                           \
00132       std::cerr << "You tried to use an object of the class \""           \
00133            << theClassifier << "\" as an instance of the \"" << classifier  \
00134      << "\" class" << std::endl;                                          \
00135       std::cerr << "A cast isn't possible here!" << std::endl                   \
00136      << "This object is an instance of:" << std::endl                     \
00137      << theName::getTypeInfo() << std::endl;              \
00138       exit(1);                  \
00139     }                   \
00140   }
00141 
00149 #define TYPEINFO0(theName, theClassifier) \
00150   TYPEINFOSTART         \
00151   TYPEINFOEND(theName, theClassifier)
00152 
00162 #define TYPEINFO1(theName, theClassifier, theSuper1)  \
00163   TYPEINFOSTART           \
00164     l = theSuper1::getTypeInfo();     \
00165     _ancesAndSelf.conc(l);        \
00166   TYPEINFOEND(theName, theClassifier)
00167 
00178 #define TYPEINFO2(theName, theClassifier, theSuper1, theSuper2) \
00179   TYPEINFOSTART             \
00180     l = theSuper1::getTypeInfo();       \
00181     _ancesAndSelf.conc(l);          \
00182     l = theSuper2::getTypeInfo();       \
00183     _ancesAndSelf.conc(l);          \
00184   TYPEINFOEND(theName, theClassifier)
00185 
00197 #define TYPEINFO3(theName, theClassifier, theSuper1, theSuper2, theSuper3)  \
00198   TYPEINFOSTART                 \
00199     l = theSuper1::getTypeInfo();           \
00200     _ancesAndSelf.conc(l);              \
00201     l = theSuper2::getTypeInfo();           \
00202     _ancesAndSelf.conc(l);              \
00203     l = theSuper3::getTypeInfo();           \
00204     _ancesAndSelf.conc(l);              \
00205   TYPEINFOEND(theName, theClassifier)
00206   
00207 
00214 #define TYPEINFOVAR(theName)      \
00215   bool theName::_validAncesAndSelf = false; \
00216   leda_list<leda_string> theName::_ancesAndSelf;
00217   
00218 
00219 
00220 #endif //STYPEINFO_H
00221 
00222 
00223 
00224 
00225 
00226