-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplete-reflection.cc
More file actions
88 lines (78 loc) · 1.7 KB
/
implete-reflection.cc
File metadata and controls
88 lines (78 loc) · 1.7 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <string>
#include <map>
#include <iostream>
using namespace std;
typedef void* (*CreateFuntion)(void);
/*
以下为工厂类,用来创建类和注册类
*/
class ClassFactory
{
public:
static void* GetClassByName(std::string name)
{
std::map<std::string,CreateFuntion>::const_iterator find;
find=m_clsMap.find(name);
if(find==m_clsMap.end())
{
return NULL;
}
else
{
return find->second();
}
}
static void RegistClass(std::string name,CreateFuntion method)
{
m_clsMap.insert(std::make_pair(name,method));
}
private:
static std::map<std::string,CreateFuntion> m_clsMap;
};
std::map<std::string,CreateFuntion> ClassFactory::m_clsMap;
class RegistyClass
{
public:
RegistyClass(std::string name,CreateFuntion method)
{
ClassFactory::RegistClass(name,method);
}
};
template<class T,const char name[]>
class Register
{
public:
Register()
{
//这个一定要加,因为编译器不保证程序开始时就初始化变量rc
const RegistyClass tmp=rc;
}
static void* CreateInstance()
{
return new T;
}
public:
static const RegistyClass rc;
};
//初始化全局变量。
template<class T,const char name[]>
const RegistyClass Register<T,name>::rc(name,Register<T,name>::CreateInstance);
#define DEFINE_CLASS(class_name) char NameArray[]=#class_name;
/ class class_name:public Register<class_name,NameArray>
#define DEFINE_CLASS_EX(class_name,father_class) /
char NameArray[]=#class_name;/
class class_name:public Register<class_name,NameArray>,public father_class
DEFINE_CLASS(CG)
{
public:
void Display()
{
printf("fff");
}
};
int main(int argc, _TCHAR* argv[])
{
CG* tmp=(CG*)ClassFactory::GetClassByName("CG");
tmp->Display();
return 0;
}