-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplete-reflection2.cc
More file actions
134 lines (106 loc) · 3.31 KB
/
implete-reflection2.cc
File metadata and controls
134 lines (106 loc) · 3.31 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <string>
#include <map>
#include <iostream>
using namespace std;
typedef void* (*createClass)(void) ;
class CKClassFactory
{
public:
CKClassFactory(){}
virtual ~CKClassFactory(){}
void* getClassByName(string className) ;
void registClass(string name, createClass method) ;
static CKClassFactory& sharedClassFactory() ;
private:
map<string, createClass> m_classMap ;
} ;
void* CKClassFactory::getClassByName(string className)
{
map<string, createClass>::const_iterator iter ;
iter = m_classMap.find(className) ;
if ( iter == m_classMap.end() )
return NULL ;
else
return iter->second() ;
}
void CKClassFactory::registClass(string name, createClass method)
{
m_classMap.insert(pair<string, createClass>(name, method)) ;
}
CKClassFactory& CKClassFactory::sharedClassFactory()
{
static CKClassFactory _sharedClassFactory ;
return _sharedClassFactory ;
}
class CKDynamicClass
{
public:
CKDynamicClass(string name, createClass method)
{
CKClassFactory::sharedClassFactory().registClass(name, method) ;
}
} ;
#define DECLARE_CLASS(className)\
string className##Name ; \
static CKDynamicClass* m_className##dc ;
#define IMPLEMENT_CLASS(className) \
CKDynamicClass* className::m_className##dc = \
new CKDynamicClass(#className, className::createInstance) ;
class CKBaseClass ;
typedef void (*setValue)(CKBaseClass *t, void* c) ;
class CKBaseClass
{
private:
DECLARE_CLASS(CKBaseClass)
public:
CKBaseClass() {}
virtual ~CKBaseClass() {}
static void* createInstance() {return new CKBaseClass();}
virtual void registProperty() {}
virtual void display() {}
map<string, setValue> m_propertyMap ;
} ;
// #define SYNTHESIZE(classType, varType, varName) \
// public: \
// inline static void set##varName(CKBaseClass*cp, void*value){ \
// classType* tp = (classType*)cp ; \
// tp->varName = (varType)value ; \
// } \
// inline varType get##varName(void) const { \
// return varName ; \
// }
IMPLEMENT_CLASS(CKBaseClass)
class CKHelloClass
{
private:
DECLARE_CLASS(CKBaseClass)
public:
// SYNTHESIZE(CKHelloClass, int*, m_pValue)
CKHelloClass() {}
virtual ~CKHelloClass(){}
static void* createInstance()
{
return new CKHelloClass() ;
}
// virtual void registProperty()
// {
// m_propertyMap.inset(pair<string, setValue>("setm_pValue", setm_pValue)) ;
// }
virtual void display()
{
// cout << *getm_pValue() << endl ;
cout << "hello" << endl ;
}
protected:
int *m_pValue ;
} ;
IMPLEMENT_CLASS(CKHelloClass)
int main()
{
CKBaseClass *pVar = (CKBaseClass*)CKClassFactory::sharedClassFactory().getClassByName("CKHelloClass") ;
// pVar->registProperty() ;
// int pValue = 123456 ;
// pVar->m_propertyMap["setm_pValue"](pVar, &pValue) ;
pVar->display() ;
return 0;
}