委托函数模板
这些模板的实现,我是参考了boost,VCL,及其他人的实现。并按照我的风格和要求,重新做了实现。
示例代码:
1
#ifndef _X_DELEGATE_H__
2 #define _X_DELEGATE_H__
3
4 #include < xtype.h >
5 namespace zdh
6 {
7 //空事件类型
8 class XNULL_Event
9 {
10 private:
11 XNULL_Event(){}
12 };
13 //函数类型定义
14 template<class ClassType,
15 class P1 = XNULL_Event, class P2 = XNULL_Event, class P3 = XNULL_Event,
16 class P4 = XNULL_Event, class P5 = XNULL_Event, class P6 = XNULL_Event,
17 class P7 = XNULL_Event, class P8 = XNULL_Event, class P9 = XNULL_Event,
18 class P10 = XNULL_Event>
19 struct SFunctionDefine
20 {
21 typedef void (ClassType::*F0)();
22 typedef void (ClassType::*F1)(P1);
23 typedef void (ClassType::*F2)(P1,P2);
24 typedef void (ClassType::*F3)(P1,P2,P3);
25 typedef void (ClassType::*F4)(P1,P2,P3,P4);
26 typedef void (ClassType::*F5)(P1,P2,P3,P4,P5);
27 typedef void (ClassType::*F6)(P1,P2,P3,P4,P5,P6);
28 typedef void (ClassType::*F7)(P1,P2,P3,P4,P5,P6,P7);
29 typedef void (ClassType::*F8)(P1,P2,P3,P4,P5,P6,P7,P8);
30 typedef void (ClassType::*F9)(P1,P2,P3,P4,P5,P6,P7,P8,P9);
31 typedef void (ClassType::*F10)(P1,P2,P3,P4,P5,P6,P7,P8,P9,P10);
32 };
33 //委托的基类
34 class XDelegateBase
35 {
36 private:
37 //这个结构体的做用是在bind的函数中,将函数指针转换为void *
38 template<class FunctionPointer>
39 struct SFunctionPointer
40 {
41 FunctionPointer Pointer;
42 };
43 protected:
44 void * m_DelegateObject; /**////<委托的对象
45 void * m_DelegateFunction; ///<委托的函数
46 public:
47 ///构造函数
48 XDelegateBase()
49 :m_DelegateFunction(NULL),m_DelegateObject(NULL)
50 {}
51 /**////委托绑定
52 template<class DelegateObjectType,class DelegateFunctionPointer>
53 void Bind(DelegateObjectType * aDelegateObject, DelegateFunctionPointer aDelegateFunction)
54 {
55 SFunctionPointer<DelegateFunctionPointer> FunctionPtr = {aDelegateFunction };
56 m_DelegateObject = (void *)aDelegateObject;
57 m_DelegateFunction = *(void **)&FunctionPtr;
58 }
59 /**////解除绑定
60 void UnBind()
61 {
62 m_DelegateFunction = NULL;
63 m_DelegateObject = NULL;
64 }
65 /**////判断是否已经被绑定
66 bool isBinded() const
67 {
68 return m_DelegateFunction != NULL;
69 }
70 };
71 //无参数函数委托类
72 template<class T>
73 class XDelegate0 : public XDelegateBase
74 {
75 public:
76 typedef typename SFunctionDefine<XNULL_Event>::F0 Function;
77 void Invoke()
78 {
79 if( m_DelegateFunction != NULL )
80 {
81 Function Fun;
82 *(void **)&Fun = m_DelegateFunction;
83 (((XNULL_Event *)m_DelegateObject)->*Fun)();
84 }
85
86 }
87 };
88 //一个参数函数委托类
89 template<class P1>
90 class XDelegate1 : public XDelegateBase
91 {
92 public:
93 typedef typename SFunctionDefine<XNULL_Event,P1>::F1 Function;
94 void Invoke(P1 aP1)
95 {
96 if( m_DelegateFunction != NULL )
97 {
98 Function Fun;
99 *(void **)&Fun = m_DelegateFunction;
100 (((XNULL_Event *)m_DelegateObject)->*Fun)(aP1);
101 }
102 }
103 };
104
105 //两个参数函数委托类
106 template<class P1,class P2>
107 class XDelegate2 : public XDelegateBase
108 {
109 public:
110 typedef typename SFunctionDefine<XNULL_Event,P1,P2>::F2 Function;
111 void Invoke(P1 aP1,P2 aP2)
112 {
113 if( m_DelegateFunction != NULL )
114 {
115 Function Fun;
116 *(void **)&Fun = m_DelegateFunction;
117 (((XNULL_Event *)m_DelegateObject)->*Fun)(aP1, aP2);
118 }
119 }
120 };
121
122 //三个参数函数委托类
123 template<class P1,class P2,class P3>
124 class XDelegate3 : public XDelegateBase
125 {
126 public:
127 typedef typename SFunctionDefine<XNULL_Event,P1,P2,P3>::F3 Function;
128 void Invoke(P1 aP1,P2 aP2,P3 aP3)
129 {
130 if( m_DelegateFunction != NULL )
131 {
132 Function Fun;
133 *(void **)&Fun = m_DelegateFunction;
134 (((XNULL_Event *)m_DelegateObject)->*Fun)(aP1, aP2, aP3);
135 }
136 }
137 };
138
139 //四个参数函数委托类
140 template<class P1,class P2,class P3, class P4>
141 class XDelegate4 : public XDelegateBase
142 {
143 public:
144 typedef typename SFunctionDefine<XNULL_Event,P1,P2,P3,P4>::F4 Function;
145 void Invoke(P1 aP1,P2 aP2,P3 aP3,P4 aP4)
146 {
147 if( m_DelegateFunction != NULL )
148 {
149 Function Fun;
150 *(void **)&Fun = m_DelegateFunction;
151 (((XNULL_Event *)m_DelegateObject)->*Fun)(aP1, aP2, aP3,aP4);
152 }
153 }
154 };
155
156 //五个参数函数委托类
157 template<class P1,class P2,class P3, class P4, class P5>
158 class XDelegate5 : public XDelegateBase
159 {
160 public:
161 typedef typename SFunctionDefine<XNULL_Event,P1,P2,P3,P4,P5>::F5 Function;
162 void Invoke(P1 aP1,P2 aP2,P3 aP3,P4 aP4, P5 aP5)
163 {
164 if( m_DelegateFunction != NULL )
165 {
166 Function Fun;
167 *(void **)&Fun = m_DelegateFunction;
168 (((XNULL_Event *)m_DelegateObject)->*Fun)(aP1, aP2, aP3, aP4, aP5);
169 }
170 }
171 };
172 //六个参数函数委托类
173 template<class P1,class P2,class P3, class P4, class P5, class P6>
174 class XDelegate6 : public XDelegateBase
175 {
176 public:
177 typedef typename SFunctionDefine<XNULL_Event,P1,P2,P3,P4,P5,P6>::F6 Function;
178 void Invoke(P1 aP1,P2 aP2,P3 aP3,P4 aP4, P5 aP5, P6 aP6)
179 {
180 if( m_DelegateFunction != NULL )
181 {
182 Function Fun;
183 *(void **)&Fun = m_DelegateFunction;
184 (((XNULL_Event *)m_DelegateObject)->*Fun)(aP1, aP2, aP3, aP4, aP5, aP6);
185 }
186 }
187 };
188
189 //七个参数函数委托类
190 template<class P1,class P2,class P3, class P4, class P5, class P6, class P7>
191 class XDelegate7 : public XDelegateBase
192 {
193 public:
194 typedef typename SFunctionDefine<XNULL_Event,P1,P2,P3,P4,P5,P6,P7>::F7 Function;
195 void Invoke(P1 aP1,P2 aP2,P3 aP3,P4 aP4, P5 aP5, P6 aP6, P7 aP7)
196 {
197 if( m_DelegateFunction != NULL )
198 {
199 Function Fun;
200 *(void **)&Fun = m_DelegateFunction;
201 (((XNULL_Event *)m_DelegateObject)->*Fun)(aP1, aP2, aP3, aP4, aP5, aP6, aP7);
202 }
203 }
204 };
205 //八个参数函数委托类
206 template<class P1,class P2,class P3, class P4, class P5, class P6, class P7,class P8>
207 class XDelegate8 : public XDelegateBase
208 {
209 public:
210 typedef typename SFunctionDefine<XNULL_Event,P1,P2,P3,P4,P5,P6,P7,P8>::F8 Function;
211 void Invoke(P1 aP1,P2 aP2,P3 aP3,P4 aP4, P5 aP5, P6 aP6, P7 aP7,P8 aP8)
212 {
213 if( m_DelegateFunction != NULL )
214 {
215 Function Fun;
216 *(void **)&Fun = m_DelegateFunction;
217 (((XNULL_Event *)m_DelegateObject)->*Fun)(aP1, aP2, aP3, aP4, aP5, aP6, aP7, aP8);
218 }
219 }
220 };
221 //九个参数函数委托类
222 template<class P1,class P2,class P3, class P4, class P5, class P6, class P7,class P8, class P9>
223 class XDelegate9 : public XDelegateBase
224 {
225 public:
226 typedef typename SFunctionDefine<XNULL_Event,P1,P2,P3,P4,P5,P6,P7,P8,P9>::F9 Function;
227 void Invoke(P1 aP1,P2 aP2,P3 aP3,P4 aP4, P5 aP5, P6 aP6, P7 aP7,P8 aP8,P9 aP9)
228 {
229 if( m_DelegateFunction != NULL )
230 {
231 Function Fun;
232 *(void **)&Fun = m_DelegateFunction;
233 (((XNULL_Event *)m_DelegateObject)->*Fun)(aP1, aP2, aP3, aP4, aP5, aP6, aP7, aP8,aP9);
234 }
235 }
236 };
237 //十个参数函数委托类
238 template<class P1,class P2,class P3, class P4, class P5, class P6, class P7,class P8, class P9,class P10>
239 class XDelegate10 : public XDelegateBase
240 {
241 public:
242 typedef typename SFunctionDefine<XNULL_Event,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10>::F10 Function;
243 void Invoke(P1 aP1,P2 aP2,P3 aP3,P4 aP4, P5 aP5, P6 aP6, P7 aP7,P8 aP8,P9 aP9,P10 aP10)
244 {
245 if( m_DelegateFunction != NULL )
246 {
247 Function Fun;
248 *(void **)&Fun = m_DelegateFunction;
249 (((XNULL_Event *)m_DelegateObject)->*Fun)(aP1, aP2, aP3, aP4, aP5, aP6, aP7, aP8, aP9, aP10);
250 }
251 }
252 };
253}
254 #endif // _ZY_DELEGATE_H__
255
2 #define _X_DELEGATE_H__
3
4 #include < xtype.h >
5 namespace zdh
6 {
7 //空事件类型
8 class XNULL_Event
9 {
10 private:
11 XNULL_Event(){}
12 };
13 //函数类型定义
14 template<class ClassType,
15 class P1 = XNULL_Event, class P2 = XNULL_Event, class P3 = XNULL_Event,
16 class P4 = XNULL_Event, class P5 = XNULL_Event, class P6 = XNULL_Event,
17 class P7 = XNULL_Event, class P8 = XNULL_Event, class P9 = XNULL_Event,
18 class P10 = XNULL_Event>
19 struct SFunctionDefine
20 {
21 typedef void (ClassType::*F0)();
22 typedef void (ClassType::*F1)(P1);
23 typedef void (ClassType::*F2)(P1,P2);
24 typedef void (ClassType::*F3)(P1,P2,P3);
25 typedef void (ClassType::*F4)(P1,P2,P3,P4);
26 typedef void (ClassType::*F5)(P1,P2,P3,P4,P5);
27 typedef void (ClassType::*F6)(P1,P2,P3,P4,P5,P6);
28 typedef void (ClassType::*F7)(P1,P2,P3,P4,P5,P6,P7);
29 typedef void (ClassType::*F8)(P1,P2,P3,P4,P5,P6,P7,P8);
30 typedef void (ClassType::*F9)(P1,P2,P3,P4,P5,P6,P7,P8,P9);
31 typedef void (ClassType::*F10)(P1,P2,P3,P4,P5,P6,P7,P8,P9,P10);
32 };
33 //委托的基类
34 class XDelegateBase
35 {
36 private:
37 //这个结构体的做用是在bind的函数中,将函数指针转换为void *
38 template<class FunctionPointer>
39 struct SFunctionPointer
40 {
41 FunctionPointer Pointer;
42 };
43 protected:
44 void * m_DelegateObject; /**////<委托的对象
45 void * m_DelegateFunction; ///<委托的函数
46 public:
47 ///构造函数
48 XDelegateBase()
49 :m_DelegateFunction(NULL),m_DelegateObject(NULL)
50 {}
51 /**////委托绑定
52 template<class DelegateObjectType,class DelegateFunctionPointer>
53 void Bind(DelegateObjectType * aDelegateObject, DelegateFunctionPointer aDelegateFunction)
54 {
55 SFunctionPointer<DelegateFunctionPointer> FunctionPtr = {aDelegateFunction };
56 m_DelegateObject = (void *)aDelegateObject;
57 m_DelegateFunction = *(void **)&FunctionPtr;
58 }
59 /**////解除绑定
60 void UnBind()
61 {
62 m_DelegateFunction = NULL;
63 m_DelegateObject = NULL;
64 }
65 /**////判断是否已经被绑定
66 bool isBinded() const
67 {
68 return m_DelegateFunction != NULL;
69 }
70 };
71 //无参数函数委托类
72 template<class T>
73 class XDelegate0 : public XDelegateBase
74 {
75 public:
76 typedef typename SFunctionDefine<XNULL_Event>::F0 Function;
77 void Invoke()
78 {
79 if( m_DelegateFunction != NULL )
80 {
81 Function Fun;
82 *(void **)&Fun = m_DelegateFunction;
83 (((XNULL_Event *)m_DelegateObject)->*Fun)();
84 }
85
86 }
87 };
88 //一个参数函数委托类
89 template<class P1>
90 class XDelegate1 : public XDelegateBase
91 {
92 public:
93 typedef typename SFunctionDefine<XNULL_Event,P1>::F1 Function;
94 void Invoke(P1 aP1)
95 {
96 if( m_DelegateFunction != NULL )
97 {
98 Function Fun;
99 *(void **)&Fun = m_DelegateFunction;
100 (((XNULL_Event *)m_DelegateObject)->*Fun)(aP1);
101 }
102 }
103 };
104
105 //两个参数函数委托类
106 template<class P1,class P2>
107 class XDelegate2 : public XDelegateBase
108 {
109 public:
110 typedef typename SFunctionDefine<XNULL_Event,P1,P2>::F2 Function;
111 void Invoke(P1 aP1,P2 aP2)
112 {
113 if( m_DelegateFunction != NULL )
114 {
115 Function Fun;
116 *(void **)&Fun = m_DelegateFunction;
117 (((XNULL_Event *)m_DelegateObject)->*Fun)(aP1, aP2);
118 }
119 }
120 };
121
122 //三个参数函数委托类
123 template<class P1,class P2,class P3>
124 class XDelegate3 : public XDelegateBase
125 {
126 public:
127 typedef typename SFunctionDefine<XNULL_Event,P1,P2,P3>::F3 Function;
128 void Invoke(P1 aP1,P2 aP2,P3 aP3)
129 {
130 if( m_DelegateFunction != NULL )
131 {
132 Function Fun;
133 *(void **)&Fun = m_DelegateFunction;
134 (((XNULL_Event *)m_DelegateObject)->*Fun)(aP1, aP2, aP3);
135 }
136 }
137 };
138
139 //四个参数函数委托类
140 template<class P1,class P2,class P3, class P4>
141 class XDelegate4 : public XDelegateBase
142 {
143 public:
144 typedef typename SFunctionDefine<XNULL_Event,P1,P2,P3,P4>::F4 Function;
145 void Invoke(P1 aP1,P2 aP2,P3 aP3,P4 aP4)
146 {
147 if( m_DelegateFunction != NULL )
148 {
149 Function Fun;
150 *(void **)&Fun = m_DelegateFunction;
151 (((XNULL_Event *)m_DelegateObject)->*Fun)(aP1, aP2, aP3,aP4);
152 }
153 }
154 };
155
156 //五个参数函数委托类
157 template<class P1,class P2,class P3, class P4, class P5>
158 class XDelegate5 : public XDelegateBase
159 {
160 public:
161 typedef typename SFunctionDefine<XNULL_Event,P1,P2,P3,P4,P5>::F5 Function;
162 void Invoke(P1 aP1,P2 aP2,P3 aP3,P4 aP4, P5 aP5)
163 {
164 if( m_DelegateFunction != NULL )
165 {
166 Function Fun;
167 *(void **)&Fun = m_DelegateFunction;
168 (((XNULL_Event *)m_DelegateObject)->*Fun)(aP1, aP2, aP3, aP4, aP5);
169 }
170 }
171 };
172 //六个参数函数委托类
173 template<class P1,class P2,class P3, class P4, class P5, class P6>
174 class XDelegate6 : public XDelegateBase
175 {
176 public:
177 typedef typename SFunctionDefine<XNULL_Event,P1,P2,P3,P4,P5,P6>::F6 Function;
178 void Invoke(P1 aP1,P2 aP2,P3 aP3,P4 aP4, P5 aP5, P6 aP6)
179 {
180 if( m_DelegateFunction != NULL )
181 {
182 Function Fun;
183 *(void **)&Fun = m_DelegateFunction;
184 (((XNULL_Event *)m_DelegateObject)->*Fun)(aP1, aP2, aP3, aP4, aP5, aP6);
185 }
186 }
187 };
188
189 //七个参数函数委托类
190 template<class P1,class P2,class P3, class P4, class P5, class P6, class P7>
191 class XDelegate7 : public XDelegateBase
192 {
193 public:
194 typedef typename SFunctionDefine<XNULL_Event,P1,P2,P3,P4,P5,P6,P7>::F7 Function;
195 void Invoke(P1 aP1,P2 aP2,P3 aP3,P4 aP4, P5 aP5, P6 aP6, P7 aP7)
196 {
197 if( m_DelegateFunction != NULL )
198 {
199 Function Fun;
200 *(void **)&Fun = m_DelegateFunction;
201 (((XNULL_Event *)m_DelegateObject)->*Fun)(aP1, aP2, aP3, aP4, aP5, aP6, aP7);
202 }
203 }
204 };
205 //八个参数函数委托类
206 template<class P1,class P2,class P3, class P4, class P5, class P6, class P7,class P8>
207 class XDelegate8 : public XDelegateBase
208 {
209 public:
210 typedef typename SFunctionDefine<XNULL_Event,P1,P2,P3,P4,P5,P6,P7,P8>::F8 Function;
211 void Invoke(P1 aP1,P2 aP2,P3 aP3,P4 aP4, P5 aP5, P6 aP6, P7 aP7,P8 aP8)
212 {
213 if( m_DelegateFunction != NULL )
214 {
215 Function Fun;
216 *(void **)&Fun = m_DelegateFunction;
217 (((XNULL_Event *)m_DelegateObject)->*Fun)(aP1, aP2, aP3, aP4, aP5, aP6, aP7, aP8);
218 }
219 }
220 };
221 //九个参数函数委托类
222 template<class P1,class P2,class P3, class P4, class P5, class P6, class P7,class P8, class P9>
223 class XDelegate9 : public XDelegateBase
224 {
225 public:
226 typedef typename SFunctionDefine<XNULL_Event,P1,P2,P3,P4,P5,P6,P7,P8,P9>::F9 Function;
227 void Invoke(P1 aP1,P2 aP2,P3 aP3,P4 aP4, P5 aP5, P6 aP6, P7 aP7,P8 aP8,P9 aP9)
228 {
229 if( m_DelegateFunction != NULL )
230 {
231 Function Fun;
232 *(void **)&Fun = m_DelegateFunction;
233 (((XNULL_Event *)m_DelegateObject)->*Fun)(aP1, aP2, aP3, aP4, aP5, aP6, aP7, aP8,aP9);
234 }
235 }
236 };
237 //十个参数函数委托类
238 template<class P1,class P2,class P3, class P4, class P5, class P6, class P7,class P8, class P9,class P10>
239 class XDelegate10 : public XDelegateBase
240 {
241 public:
242 typedef typename SFunctionDefine<XNULL_Event,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10>::F10 Function;
243 void Invoke(P1 aP1,P2 aP2,P3 aP3,P4 aP4, P5 aP5, P6 aP6, P7 aP7,P8 aP8,P9 aP9,P10 aP10)
244 {
245 if( m_DelegateFunction != NULL )
246 {
247 Function Fun;
248 *(void **)&Fun = m_DelegateFunction;
249 (((XNULL_Event *)m_DelegateObject)->*Fun)(aP1, aP2, aP3, aP4, aP5, aP6, aP7, aP8, aP9, aP10);
250 }
251 }
252 };
253}
254 #endif // _ZY_DELEGATE_H__
255
示例代码:
1
//
未测试的示例代码
2 class XObject
3 {
4public:
5 ZY_Delegate0<NULL> EventClick;
6} ;
7 class XButton
8 {
9public:
10 XButton()
11 {
12 EventClick.Bind(this,&XButton::OnClick);
13 }
14
15private:
16 void OnClick()
17 {
18 printf("hello On Click");
19 }
20} ;
21 int main()
22 {
23 XAutoPtr<XObject> p = new XButton();
24 p->EventClick->Invoke();
25}
2 class XObject
3 {
4public:
5 ZY_Delegate0<NULL> EventClick;
6} ;
7 class XButton
8 {
9public:
10 XButton()
11 {
12 EventClick.Bind(this,&XButton::OnClick);
13 }
14
15private:
16 void OnClick()
17 {
18 printf("hello On Click");
19 }
20} ;
21 int main()
22 {
23 XAutoPtr<XObject> p = new XButton();
24 p->EventClick->Invoke();
25}