00001 #ifndef BASETYPEINFO_H
00002 #define BASETYPEINFO_H
00003
00004
00005
00006 namespace Spr{;
00007
00008
00009 class UTTypeInfo {
00010 public:
00011 const char* className;
00012 UTTypeInfo** base;
00013
00014 UTTypeInfo(const char* cn, UTTypeInfo** b): className(cn), base(b){}
00015 virtual bool Inherit(const UTTypeInfo* key) const ;
00016 virtual bool Inherit(const char* str) const ;
00017 virtual const char* ClassName() const = 0;
00018 virtual void* CreateInstance() const = 0;
00019 };
00020
00021
00022 template <class T>
00023 class UTTypeInfoImp: public UTTypeInfo{
00024 public:
00025 UTTypeInfoImp(const char* cn, UTTypeInfo** b): UTTypeInfo(cn, b){}
00026 virtual void* CreateInstance() const { return new T; }
00027 virtual const char* ClassName() const { return className; }
00028 };
00029
00030
00031 template <class T>
00032 class UTTypeInfoImpAbst: public UTTypeInfo{
00033 public:
00034 UTTypeInfoImpAbst(const char* cn, UTTypeInfo** b): UTTypeInfo(cn, b){}
00035 virtual void* CreateInstance() const { return 0; }
00036 virtual const char* ClassName() const { return className; }
00037 };
00038
00039
00040 class UTTypeInfoBase{
00041 public:
00042 virtual const UTTypeInfo* GetTypeInfo() const =0;
00043 };
00044
00045
00046
00047
00048 #define DEF_UTTYPEINFODEF(cls) \
00049 public: \
00050 static UTTypeInfoImp<cls> typeInfo; \
00051 virtual const UTTypeInfo* GetTypeInfo() const { \
00052 return &typeInfo; \
00053 } \
00054 static const UTTypeInfo* GetTypeInfoStatic(){ \
00055 return &typeInfo; \
00056 } \
00057
00058
00059 #define DEF_UTTYPEINFOABSTDEF(cls) \
00060 public: \
00061 static UTTypeInfoImpAbst<cls> typeInfo; \
00062 virtual const UTTypeInfo* GetTypeInfo() const { \
00063 return &typeInfo; \
00064 } \
00065 static const UTTypeInfo* GetTypeInfoStatic(){ \
00066 return &typeInfo; \
00067 } \
00068
00069
00070
00071 #define DEF_UTTYPEINFO(cls) \
00072 UTTypeInfo* cls##_BASE[] = {NULL}; \
00073 UTTypeInfoImp<cls> cls::typeInfo = UTTypeInfoImp<cls>(#cls, cls##_BASE);
00074
00075
00076 #define DEF_UTTYPEINFO1(cls, base1) \
00077 UTTypeInfo* cls##_BASE[] = {&base1::typeInfo, NULL}; \
00078 UTTypeInfoImp<cls> cls::typeInfo = UTTypeInfoImp<cls>(#cls, cls##_BASE);
00079
00080
00081 #define DEF_UTTYPEINFO2(cls, base1, base2) \
00082 UTTypeInfo* cls##_BASE[] = {&base1::typeInfo, &base2::typeInfo, NULL}; \
00083 UTTypeInfoImp<cls> cls::typeInfo = UTTypeInfoImp<cls>(#cls, cls##_BASE);
00084
00085
00086 #define DEF_UTTYPEINFO3(cls, base1, base2, base3) \
00087 UTTypeInfo* cls##_BASE[] = {&base1::typeInfo, &base2::typeInfo, &base3::typeInfo, NULL}; \
00088 UTTypeInfoImp<cls> cls::typeInfo = UTTypeInfoImp<cls>(#cls, cls##_BASE);
00089
00090
00091 #define DEF_UTTYPEINFOABST(cls) \
00092 UTTypeInfo* cls##_BASE[] = {NULL}; \
00093 UTTypeInfoImpAbst<cls> cls::typeInfo = UTTypeInfoImpAbst<cls>(#cls, cls##_BASE);
00094
00095
00096 #define DEF_UTTYPEINFOABST1(cls, base) \
00097 UTTypeInfo* cls##_BASE[] = {&base::typeInfo,NULL}; \
00098 UTTypeInfoImpAbst<cls> cls::typeInfo = UTTypeInfoImpAbst<cls>(#cls, cls##_BASE);
00099
00100
00101 #define DEF_UTTYPEINFOABST2(cls, base1, base2) \
00102 UTTypeInfo* cls##_BASE[] = {&base1::typeInfo, &base2::typeInfo, NULL}; \
00103 UTTypeInfoImpAbst<cls> cls::typeInfo = UTTypeInfoImpAbst<cls>(#cls, cls##_BASE);
00104
00105
00106 #define DEF_UTTYPEINFOABST3(cls, base) \
00107 UTTypeInfo* cls##_BASE[] = {&base1::typeInfo, &base2::typeInfo, &base3::typeInfo, NULL}; \
00108 UTTypeInfoImpAbst<cls> cls::typeInfo = UTTypeInfoImpAbst<cls>(#cls, cls##_BASE);
00109
00110 #define GETCLASSNAME(p) (p->GetTypeInfo()->className)
00111 #define GETCLASSNAMES(T) (T::GetTypeInfoStatic()->className)
00112
00113
00114 #define DCAST(T, p) UTDcastImp<T>(p)
00115 template <class T, class P> T* UTDcastImp(P p){
00116 if (p && p->GetTypeInfo()->Inherit(T::GetTypeInfoStatic())) return (T*)&*(p);
00117 return NULL;
00118 }
00119
00120
00121 #define ACAST(T, p) UTAcastImp<T>(p)
00122 template <class T, class P> T* UTAcastImp(P p){
00123 if (p && p->GetTypeInfo()->Inherit(T::GetTypeInfoStatic())) return (T*)&*(p);
00124 UTAcastError((p)->GetTypeInfo()->className);
00125 return NULL;
00126 }
00127
00128 #define DCASTP(TP, p) UTDcastpImp<TP>(p)
00129 template <class TP, class P> TP UTDcastpImp(P p){
00130 TP tp=NULL;
00131 if (p && p->GetTypeInfo()->Inherit(tp->GetTypeInfoStatic())) return (TP)&*(p);
00132 return NULL;
00133 }
00134
00135 #define ACASTP(TP, p) UTAcastpImp<TP>(p)
00136 template <class TP, class P> TP UTAcastpImp(P p){
00137 TP tp=NULL;
00138 if (p && p->GetTypeInfo()->Inherit(tp->GetTypeInfoStatic())) return (TP)&*(p);
00139 UTAcastError((p)->GetTypeInfo()->className);
00140 return NULL;
00141 }
00142
00143 char* UTAcastError(const char* );
00144
00145 }
00146 #endif