MFC RTTI 仿真

/*在定义CRuntimeClass时候我把暂时不需要的属性去掉了,留下了必须的属性。主要思想利用两个宏 DECLARE_DYNAMIC 完成将定义的CRuntimeClass 嵌入到相应的类中。 IMPLEMENT_DYNAMIC 宏负责初始化,上面宏的定义。应该放到相应的CPP文件中,实质是上面宏在类外的实现。
AFX_CLASS_INIT 这个结构体的构造函数负责,连接整个RTTI网络。
宏定义中 \ 意思是紧接着下一行,没有空格。
*/
//mfx.h 主要是RTTI相关宏的定义
#include<Windows.h>
struct CRuntimeClass
{

    LPCSTR m_pszClassName;
    DWORD  m_dwObjectSize;
    CRuntimeClass* m_pBaseClass;
    CRuntimeClass* m_pNextClass;
    static CRuntimeClass* m_pFirstClass;
};
struct AFX_CLASS_INIT
{
   AFX_CLASS_INIT(CRuntimeClass* pNewClass);
};
#define DECLEAR_DYNAMIC(class_name) \
public:\
    static CRuntimeClass class##class_name; \
    virtual CRuntimeClass* GetRuntimeClass() const; 
#define RUNTIME_CLASS(class_name) &(class_name::class##class_name)
#define IMPLEMENT_DYNAMIC(class_name,base_class_name)  _IMPLEMENT_RUNTIMECLASS(class_name,base_class_name)
#define _IMPLEMENT_RUNTIMECLASS(class_name,base_class_name)\
    CRuntimeClass class_name::class##class_name = {#class_name,sizeof(class_name),RUNTIME_CLASS(base_class_name),NULL};\
    CRuntimeClass* class_name::GetRuntimeClass() const {return &(class_name::class##class_name);}\
    struct AFX_CLASS_INIT  init_##class_name( &class_name::class##class_name);

//======================================================mfx.cpp=========================================//

#include"mfx.h" AFX_CLASS_INIT::AFX_CLASS_INIT(CRuntimeClass* pNewClass) {

    pNewClass ->m_pNextClass = CRuntimeClass::m_pFirstClass;     CRuntimeClass::m_pFirstClass = pNewClass;

} CRuntimeClass* CRuntimeClass::m_pFirstClass = NULL;

//=============================================mfc.h====================================//

//自己定义MFC类

#ifndef   _MY_FRAMEWORK_CWINAPP_H_ #define   _MY_FRAMEWORK_CWINAPP_H_

#include"mfx.h" #include<windows.h> class CObject {     //public operation public:     CObject(){};     ~CObject(){}; public:     static CRuntimeClass classCObject;     virtual CRuntimeClass* GetRuntimeClass() const;  

    };    

class CCmdTarget: public CObject { public:     CCmdTarget() {};     ~CCmdTarget() {};

    DECLEAR_DYNAMIC(CCmdTarget) public:     virtual BOOL IsKindOf(const CRuntimeClass* pBaseClass); }; //IMPLEMENT_DYNAMIC(CCmdTarget,CObject) class CWinThread: public CCmdTarget { public:     CWinThread() { };     ~CWinThread() { }; public://operation     virtual BOOL InitInstance();     virtual INT  Run(); DECLEAR_DYNAMIC(CWinThread) }; class CWnd: public CCmdTarget { public:     CWnd() {};     ~CWnd() {}; DECLEAR_DYNAMIC(CWnd) };  //IMPLEMENT_DYNAMIC(CWnd,CCmdTarget) class CWinApp: public CWinThread { public:     CWinApp() { m_pCurWinApp = this; };     ~CWinApp() { }; public: //operation     virtual BOOL InitInstance();     virtual INT  Run();     virtual BOOL InitApplication(); public://attributes     CWinApp* m_pCurWinApp;     CWnd* m_pMainWnd;     DECLEAR_DYNAMIC(CWinApp) }; //IMPLEMENT_DYNAMIC(CWinApp,CWinThread) class CFrameWnd: public CWnd { public:     CFrameWnd();     ~CFrameWnd(); public:     virtual BOOL PreCreateWindow();     BOOL Create();     DECLEAR_DYNAMIC(CFrameWnd) }; //============================globle function define===================================// CWinApp* mfxGetWinApp(); #endif

//=====================================================mfc.cpp==============================================//

#include"MyWinApp.h" extern CMyWinApp theApp; CWinApp *  mfxGetWinApp() {     return theApp.m_pCurWinApp; }

BOOL CWinThread::InitInstance() {        return TRUE; }

INT CWinThread::Run() {   return 1; }

BOOL CWinApp::InitInstance() {   return TRUE; }

INT CWinApp::Run() {   return 1; }

BOOL CWinApp::InitApplication() {   return TRUE; }

CFrameWnd::CFrameWnd() {

}

CFrameWnd::~CFrameWnd() {

}

BOOL CFrameWnd::PreCreateWindow() {   return TRUE; }

BOOL CFrameWnd::Create() {   return TRUE; }

/////////////////////////////////////// IMPLEMENT_DYNAMIC(CCmdTarget,CObject) IMPLEMENT_DYNAMIC(CWinThread,CCmdTarget) IMPLEMENT_DYNAMIC(CWnd,CCmdTarget) IMPLEMENT_DYNAMIC(CWinApp,CWinThread) IMPLEMENT_DYNAMIC(CFrameWnd,CWnd) CRuntimeClass CObject::classCObject = {"CObject",sizeof(CObject),NULL,NULL}; CRuntimeClass* CObject::GetRuntimeClass() const {    return &classCObject; }

BOOL CCmdTarget::IsKindOf( const CRuntimeClass* pBaseClass ) {    CRuntimeClass* pClass = GetRuntimeClass();    while(pClass !=NULL )    {        if(!strcmp(pClass->m_pszClassName,pBaseClass->m_pszClassName))            return TRUE;        else            pClass = pClass->m_pBaseClass;    }    return FALSE; }

//========================================MyWinApp.h======================================//

#pragma once #include "mfc.h"

class CMyWinApp :     public CWinApp { public:     CMyWinApp(void); public:     virtual ~CMyWinApp(void);     virtual BOOL InitInstance();     DECLEAR_DYNAMIC(CMyWinApp); };

//=====================================================MyWinApp.cpp===========================================//

#include "MyWinApp.h" #include "MyFrameWnd.h" CMyWinApp theApp;

CMyWinApp::CMyWinApp(void) { }

CMyWinApp::~CMyWinApp(void) { }

BOOL CMyWinApp::InitInstance() {    m_pMainWnd = new CMyFrameWnd();    return TRUE; } IMPLEMENT_DYNAMIC(CMyWinApp,CWinApp);

//=================================================MyFrameWnd.h======================================//

#pragma once #include "mfc.h"

class CMyFrameWnd :     public CFrameWnd { public:     CMyFrameWnd(void); public:     virtual ~CMyFrameWnd(void);     DECLEAR_DYNAMIC(CMyFrameWnd) };

//=================================================MyFrameWnd.cpp======================================//

#include "MyFrameWnd.h"

CMyFrameWnd::CMyFrameWnd(void) {     Create(); }

CMyFrameWnd::~CMyFrameWnd(void) { } IMPLEMENT_DYNAMIC(CMyFrameWnd,CFrameWnd)

 

//================================================WinMain.cpp========================================//

#include"mfc.h" #include"MyWinApp.h" #include<stdarg.h> #include<iostream> using namespace std; void MessageBoxPrintf(char* pszCaputre,char* Format,...)  {      va_list vaList;//equal to Format + sizeof(FOrmat)      char szBuff[100];      memset(szBuff,0,sizeof(char)*100);      va_start(vaList,Format);      //vsPrintf 三个参数 buff,format,参数数组的指针,va_list类型的。这个函数      // 多用于实现多个参数的自定义函数       _vsnprintf(szBuff,100,Format,vaList);       va_end(vaList);      MessageBoxA(NULL,szBuff,pszCaputre,MB_OK);  }      void Print() {     CRuntimeClass* pClassFirst = CRuntimeClass::m_pFirstClass;     while(pClassFirst != NULL)     {         cout<<pClassFirst->m_pszClassName<<endl;         cout<<pClassFirst->m_dwObjectSize<<endl;         cout<<pClassFirst->m_pBaseClass->m_pszClassName<<endl;         cout<<"====================================="<<endl;         pClassFirst = pClassFirst->m_pNextClass;     } } int main() {     Print();     CWinApp* pWinApp = mfxGetWinApp();     CWinApp* pApp = pWinApp;    cout<< pApp->IsKindOf(RUNTIME_CLASS(CMyWinApp))<<endl;     system("pause");     return 0; }

 


 

你可能感兴趣的:(MFC RTTI 仿真)