メインページ | ネームスペース一覧 | クラス階層 | 構成 | Directories | ファイル一覧 | ネームスペースメンバ | 構成メンバ | ファイルメンバ | 関連ページ

BaseTypeInfo.h

00001 #ifndef BASETYPEINFO_H
00002 #define BASETYPEINFO_H
00003 /** 実行時型情報テンプレートライブラリ
00004     コンパイラのrttiを使わずマクロとテンプレートで実装してある.
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 /// 実行時型情報を持つクラスが持つべきメンバの実装.1つのクラス継承をする場合
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 /// 実行時型情報を持つクラスが持つべきメンバの実装.2つのクラス継承をする場合
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 /// 実行時型情報を持つクラスが持つべきメンバの実装.3つのクラス継承をする場合
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 /// 実行時型情報を持つクラスが持つべきメンバの実装.抽象クラス版.1つのクラスを継承する場合
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 /// 実行時型情報を持つクラスが持つべきメンバの実装.抽象クラス版.2つのクラスを継承する場合
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 /// 実行時型情報を持つクラスが持つべきメンバの実装.抽象クラス版.2つのクラスを継承する場合
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 /// キャストに失敗するとassertするダイナミックキャスト
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 /// キャストに失敗するとassertするダイナミックキャスト.型名にポインタ型を使うバージョン
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 /// キャストに失敗した場合のassert関数
00143 char* UTAcastError(const char* );
00144 
00145 }
00146 #endif

Springheadに対してSun Apr 16 01:57:49 2006に生成されました。  doxygen 1.4.1