C++反射实现(转)

参考了wxWidgets的相关代码,并进行简化,同时借鉴了网上的相关内容,改进之处在于,只需要继承基本类Object,在类的头文件中定义宏,DECLARE_CLASS(类名),在类的实现文件中定义宏,IMPLEMENT_CLASS(类名),即可,欢迎指正。 
代码以来的库都是C++标准库,所以不需要额外添加库。 
例如:
#include<iostream>
#include<cstring>
#include "DynBase.h"
using namespace std;
 
class A : public Object
{
 DECLARE_CLASS(A)
public :
A(){cout<<hex<<(long)this<<" A constructor!"<<endl;}
~A(){cout<<hex<<(long)this<<" A destructor!"<<endl;}
};
IMPLEMENT_CLASS(A)
 
class B : public Object
{
 DECLARE_CLASS(B)
public :
B(){cout<<hex<<(long)this<<" B constructor!"<<endl;}
~B(){cout<<hex<<(long)this<<" B destructor!"<<endl;}
};
IMPLEMENT_CLASS(B)
 
int main()
{
Object* p = Object::CreateObject("A");
delete p;
    system("pause");
    return 0;
}
 
 
标签: <无>
 

代码片段(3)[全屏查看所有代码]

1. [文件] DynBase.cpp ~ 781B     下载(284)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "StdAfx.h"
#include <map>
#include "DynBase.h"
  
static std::map< string,ClassInfo*> *classInfoMap = NULL;
using namespace std;
IMPLEMENT_CLASS(Object)
bool Object::Register(ClassInfo* ci)
{
    if(!classInfoMap)   {
        classInfoMap = new std::map< string,ClassInfo*>();
    }
    if(ci)  {
        if(classInfoMap->find(ci->m_className) == classInfoMap->end()){
            classInfoMap->insert(std::map< string,ClassInfo*>::value_type(ci->m_className,ci));
        }
    }
    return true;
}
Object* Object::CreateObject(std::string name)
{
    std::map< string,ClassInfo*>::const_iterator iter = classInfoMap->find(name);
    if(classInfoMap->end() != iter)  {
        return iter->second->CreateObject();
    }  
    return NULL;
}
 
bool Register(ClassInfo* ci)
{
    return Object::Register(ci);
}

2. [文件] DynBase.h ~ 1KB     下载(267)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#ifndef __DYNBASE_H__
#define __DYNBASE_H__
#include <string>
 
class Object;
class ClassInfo;
 
typedef Object* (*ObjectConstructorFn)(void);
bool Register(ClassInfo* ci);
using namespace std;
 
class ClassInfo
{
public:
    ClassInfo(const std::string className,ObjectConstructorFn ctor)
    :m_className(className) ,m_objectConstructor(ctor)
    {
        Register(this);
    }
    virtual ~ClassInfo(){}
    Object* CreateObject()const { return m_objectConstructor ? (*m_objectConstructor)() : 0;    }
    bool IsDynamic()const { return NULL != m_objectConstructor;}
    const std::string GetClassName()const { return m_className;}
    ObjectConstructorFn GetConstructor()const{ return m_objectConstructor;}
public:
    string m_className;
    ObjectConstructorFn m_objectConstructor;
};
 
#define DECLARE_CLASS(name) \
    protected: \
        static ClassInfo ms_classinfo; \
    public:  \
        virtual ClassInfo* GetClassInfo() const; \
        static Object* CreateObject();
 
#define IMPLEMENT_CLASS_COMMON(name,func) \
    ClassInfo name::ms_classinfo((#name), \
             (ObjectConstructorFn) func); \
                          \
    ClassInfo *name::GetClassInfo() const \
        {return &name::ms_classinfo;}
 
#define IMPLEMENT_CLASS(name)            \
    IMPLEMENT_CLASS_COMMON(name,name::CreateObject) \
    Object* name::CreateObject()                   \
        { return new name;}
 
 
class Object
{
    DECLARE_CLASS(Object)
public:
    Object(){}
    virtual ~Object(){}
    static bool Register(ClassInfo* ci);
    static Object* CreateObject(string name);
};
 
 
#endif

3. [文件] test.cpp ~ 579B     下载(236)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream>
#include<cstring>
#include "DynBase.h"
using namespace std;
 
class A : public Object
{
    DECLARE_CLASS(A)
public :
    A(){cout<<hex<<(long)this<<" A constructor!"<<endl;}
    ~A(){cout<<hex<<(long)this<<" A destructor!"<<endl;}
};
IMPLEMENT_CLASS(A)
 
class B : public Object
{
    DECLARE_CLASS(B)
public :
    B(){cout<<hex<<(long)this<<" B constructor!"<<endl;}
    ~B(){cout<<hex<<(long)this<<" B destructor!"<<endl;}
};
IMPLEMENT_CLASS(B)
 
int main()
{
    Object* p = Object::CreateObject("A");
    delete p;
    system("pause");
    return 0;
}
上一篇:Linux下Java开发环境搭建—CentOS下Mysql安装教程


下一篇:[工作代码]dom4j解析实例