仿MFC实现c++的按名动态创建对象功能

 类似于CObject的基类BaseClass、类似于CRuntimeClass的RuntimeClass和相关宏的定义头文件如下:

//////////////////////////BaseClass.h

#ifndef BASECLASS_H
#define BASECLASS_H

#include <iostream>

class BaseClass;

struct RuntimeClass
{
    char *classname;
    BaseClass* (*pCreateFn)();

    void* CreateObject();
    static RuntimeClass* LoadByName(std::string classname);

    static RuntimeClass *pFirstClass;
    RuntimeClass *pBaseClass;
    RuntimeClass *pNextClass;
};

struct Class_List
{
    Class_List(RuntimeClass* pNewClass);
};

#define Runtime_Class(classname) /
    (&classname::class##classname)


#define Declare_RTTI(classname) /
    public: /
        static RuntimeClass class##classname; /
        virtual RuntimeClass* GetRuntimeClass() const; /


#define Implement_RTTI(classname,basename) /
    static char lpsz##classname[]=#classname; /
    RuntimeClass classname::class##classname= /
    { lpsz##classname,NULL,Runtime_Class(basename),NULL}; /
    static Class_List _init_##classname(&classname::class##classname); /
    RuntimeClass* classname::GetRuntimeClass() const /
    { return &classname::class##classname;} /


#define Declare_DynCreate(classname) /
    Declare_RTTI(classname) /
    static BaseClass* CreateObject(); /

#define Implement_DynCreate(classname,basename) /
    BaseClass* classname::CreateObject() /
    { return new classname; } /
    static char lpsz##classname[]=#classname; /
    RuntimeClass classname::class##classname= /
    { lpsz##classname,classname::CreateObject,Runtime_Class(basename),NULL}; /
    static Class_List _init_##classname(&classname::class##classname); /
    RuntimeClass* classname::GetRuntimeClass() const /
    { return &classname::class##classname;} /

 

class BaseClass
{
public:
    virtual RuntimeClass* GetRuntimeClass() const
    {
        return &BaseClass::classBaseClass;
    }
    virtual void printClassName() { std::cout<<"BaseClass!/n";}
public:
    static RuntimeClass classBaseClass;
};

#endif

 

 

//////////////////////////////////////////////////BaseClass.cpp

#include "BaseClass.h"

 

RuntimeClass* RuntimeClass::pFirstClass=NULL;

static char lpszBaseClass[]="BaseClass";

RuntimeClass BaseClass::classBaseClass ={lpszBaseClass,NULL,NULL,NULL};

static Class_List _init_BaseClass(&BaseClass::classBaseClass);

Class_List::Class_List(RuntimeClass* pNewClass)
{
    pNewClass->pNextClass=RuntimeClass::pFirstClass;
    RuntimeClass::pFirstClass=pNewClass;
}


void* RuntimeClass::CreateObject()
{
    if (pCreateFn == NULL)
    {
        return NULL;
    }

    void* pObject = NULL;
    pObject = (*pCreateFn)();

    return pObject;
}

RuntimeClass* RuntimeClass::LoadByName(std::string classname)
{
    RuntimeClass* pClass;
    for (pClass = pFirstClass; pClass != NULL; pClass = pClass->pNextClass)
    {
        if (strcmp(classname.c_str(), pClass->classname) == 0)
            return pClass;
    }
    return NULL;
}

 

 

///////////////////////////////////////////////////////三个测试类ClassA,ClassB,ClassAS

#pragma once
#include "baseclass.h"

class ClassA :
    public BaseClass
{
    Declare_DynCreate(ClassA)
public:
    ClassA(void);
public:
    ~ClassA(void);
public:
    void printClassName()
    {
        std::cout<<"ClassA!/n";
    }
public:
    void dosth()
    {
        std::cout<<"dosth/n";
    }
};

 

#include "ClassA.h"

Implement_DynCreate(ClassA,BaseClass)

ClassA::ClassA(void)
{
}

ClassA::~ClassA(void)
{
}

 

/////////////////////////

#pragma once
#include "baseclass.h"

class ClassB :
    public BaseClass
{
    Declare_DynCreate(ClassB)
public:
    ClassB(void);
public:
    ~ClassB(void);
public:
    void printClassName()
    {
        std::cout<<"ClassB/n";
    }
};

 

#include "ClassB.h"

Implement_DynCreate(ClassB,BaseClass)

ClassB::ClassB(void)
{
}

ClassB::~ClassB(void)
{
}

////////////////////////////////////

#pragma once
#include "classa.h"

class ClassAS :
    public ClassA
{
    Declare_DynCreate(ClassAS)
public:
    ClassAS(void);
public:
    ~ClassAS(void);
public:
    void printClassName()
    {
        std::cout<<"ClassAS/n";
    }
};

 

#include "ClassAS.h"

Implement_DynCreate(ClassAS,ClassA)

ClassAS::ClassAS(void)
{
}

ClassAS::~ClassAS(void)
{
}

 

 

////////////////////////////////////////////////////////////////////////测试

#include "BaseClass.h"
#include <iostream>
#include "ClassA.h"

using namespace std;

#define DOTEST(classname,varname) /
    classname *##varname=new classname(); /
    varname->dosth(); /

 


int _tmain(int argc, _TCHAR* argv[])
{
    RuntimeClass* prc;
    for(prc=RuntimeClass::pFirstClass;prc!=NULL;prc=prc->pNextClass)
    {
        cout<<prc->classname;
        if(prc->pBaseClass!=NULL)
            cout<<"--"<<prc->pBaseClass->classname<<endl;
        else
            cout<<"--NULL"<<endl;
    }
    getchar();

    prc=RuntimeClass::LoadByName("ClassAS");
    ClassA* pb=(ClassA*)prc->CreateObject();
    pb->printClassName();

    DOTEST(ClassA,ca)

    getchar();

 return 0;
}

 

 

你可能感兴趣的:(C++,String,list,null,Class,mfc)