C++委托模式

 希望想理解C++委托的同学,能够从代码中悟出其中的原理。有什么不太清楚的地方,欢迎留言交流。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 #define debug(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl
 5 
 6 class AF
 7 {
 8 public:
 9     AF() {}
10     void Func(int x){
11         cout << "AF::Func " << x << endl;
12     }
13 };
14 
15 void (AF::*pFunc)(int) = &AF::Func;
16 
17 int main(int argc, char *argv[])
18 {
19     AF af;
20     (af.*pFunc)(10);
21     AF *paf = &af;
22     (paf->*pFunc)(11);
23 
24     return 0;
25 }
成员函数指针的操作

 

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 
  4 #define debug(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl
  5 
  6 class AF
  7 {
  8 public:
  9     AF() {}
 10     void Func(int x){
 11         cout << "AF::Func " << x << endl;
 12     }
 13 };
 14 
 15 void (AF::*pFunc)(int) = &AF::Func;
 16 
 17 class BF
 18 {
 19 public:
 20     BF() {}
 21     void method(int x){
 22         cout << "BF::method " << x << endl;
 23     }
 24 };
 25 
 26 
 27 /* gcc-4.4.7 不支持特化与偏特化模板
 28 template <typename N>
 29 class Test<char, N>
 30 {
 31 public:
 32     Test( char i, N j ):a( i ), b( j )
 33     {
 34         cout<<"模版类偏特化"<< a<< ' ' << b << endl;
 35     }
 36 private:
 37     char a;
 38     N b;
 39 };
 40 */
 41 
 42 /*
 43 template<typename T>
 44 class DelegateNoneMemFunc
 45 {
 46     void (*m_pFunc)(int);
 47 public:
 48     DelegateNoneMemFunc(void (*pFunc)(int) ):m_pFunc(pFunc) {}
 49     void invoke(int value){
 50         (*m_pFunc)(value);
 51     }
 52 };
 53 */
 54 
 55 
 56 void NonmemberFunc(int value){
 57     printf("NonmemberFunc: %d\n", value);
 58 }
 59 
 60 
 61 class IDelegateHandler
 62 {
 63 public:
 64     virtual ~IDelegateHandler() {}
 65     virtual void invoke(int) = 0;
 66 };
 67 
 68 /*
 69 template <typename T>
 70 class DelegateHandlerChild : public IDelegateHandler
 71 {
 72 public:
 73     DelegateHandlerChild() {}
 74     void invoke(int value){
 75         printf("DelegateHandlerChild invoke: %d", value);
 76     }
 77 };
 78 */
 79 
 80 //*
 81 template <typename T>
 82 class DelegateHandler
 83 {
 84     T *m_pT;
 85     void (T::*m_pFunc)(int);
 86 public:
 87     DelegateHandler(T *pT, void (T::*pFunc)(int)):m_pT(pT), m_pFunc(pFunc) {}
 88 
 89     void invoke(int value){
 90         (m_pT->*m_pFunc)(value);
 91     }
 92 };
 93 
 94 template <typename T>
 95 class DelegateNmemHandler
 96 {
 97     void (*m_pFunc)(int);
 98 public:
 99     DelegateNmemHandler(void (*pFunc)(int)):m_pFunc(pFunc) {}
100 
101     void invoke(int value){
102         (*m_pFunc)(value);
103     }
104 };
105 
106 
107 int main(int argc, char *argv[])
108 {
109     AF af;
110     BF bf;
111 
112     (af.*pFunc)(10);
113     AF *paf = &af;
114     (paf->*pFunc)(11);
115 
116     // template
117     DelegateHandler<AF> afT(&af, &AF::Func);
118     afT.invoke(3);
119 
120     DelegateHandler<BF> bfT(&bf, &BF::method);
121     bfT.invoke(4);
122 
123     // 非成员函数
124     DelegateNmemHandler<void> nMemFunc(NonmemberFunc);
125     nMemFunc.invoke(5);
126 
127     return 0;
128 }
129 
130 class LuggageCompartment  {
131 private:
132     int m_iLuggage;    //私有变量,保存现在的行李总数
133 public:LuggageCompartment() {
134         m_iLuggage = 0;
135     }            //构造函数
136     int TakeoutLuggage()    //取出一件行李
137     {
138         if (m_iLuggage != 0)
139             m_iLuggage--;
140         return m_iLuggage;
141     }
142     int InsertLuggage()    //放入一件行李
143     {
144         return (++m_iLuggage);
145     }
146     int checkLuggage()    //检查行李总数
147     {
148         return m_iLuggage;
149     }
150 };
151 
152 class FlightSegment  {
153 private:LuggageCompartment * m_pLC;    //成员指针
154 public:void SetLuggageCompartment(LuggageCompartment * pLC)  {
155         m_pLC = pLC;
156     }            //设置成员指针
157     FlightSegment()    //构造函数将成员指针初始化为null
158     {
159         m_pLC = NULL;
160     }
161     int checkLuggage()  {
162         if (m_pLC == NULL)
163             return -1;
164         return m_pLC->checkLuggage();    //将函数调用委托给成员指针
165     }
166 };
使用类模板

 

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 
  4 #define debug(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl
  5 
  6 class AF
  7 {
  8 public:
  9     AF() {}
 10     void Func(int x){
 11         cout << "AF::Func " << x << endl;
 12     }
 13 };
 14 
 15 void (AF::*pFunc)(int) = &AF::Func;
 16 
 17 class BF
 18 {
 19 public:
 20     BF() {}
 21     void method(int x){
 22         cout << "BF::method " << x << endl;
 23     }
 24 };
 25 
 26 
 27 /* gcc-4.4.7 不支持特化与偏特化模板
 28 template <typename N>
 29 class Test<char, N>
 30 {
 31 public:
 32     Test( char i, N j ):a( i ), b( j )
 33     {
 34         cout<<"模版类偏特化"<< a<< ' ' << b << endl;
 35     }
 36 private:
 37     char a;
 38     N b;
 39 };
 40 */
 41 
 42 void NonmemberFunc(int value){
 43     printf("NonmemberFunc: %d\n", value);
 44 }
 45 
 46 
 47 template <typename T>
 48 class DelegateHandler
 49 {
 50     T *m_pT;
 51     void (T::*m_pFunc)(int);
 52 public:
 53     DelegateHandler(T *pT, void (T::*pFunc)(int)):m_pT(pT), m_pFunc(pFunc) {}
 54 
 55     void invoke(int value){
 56         (m_pT->*m_pFunc)(value);
 57     }
 58 };
 59 
 60 // 非成员函数
 61 template <typename T>
 62 class DelegateNmemHandler
 63 {
 64     void (*m_pFunc)(int);
 65 public:
 66     DelegateNmemHandler(void (*pFunc)(int)):m_pFunc(pFunc) {}
 67 
 68     void invoke(int value){
 69         (*m_pFunc)(value);
 70     }
 71 };
 72 
 73 
 74 class IDelegateHandler
 75 {
 76 public:
 77     virtual ~IDelegateHandler() {}
 78     virtual void invoke(int) = 0;
 79 };
 80 
 81 
 82 template <typename T>
 83 class DelegateHandlerChild : public IDelegateHandler
 84 {
 85     T *m_pT;
 86     void (T::*m_pFunc)(int);
 87 public:
 88     DelegateHandlerChild(T *pT, void (T::*pFunc)(int)):m_pT(pT), m_pFunc(pFunc) {}
 89 
 90     void invoke(int value){
 91         (m_pT->*m_pFunc)(value);
 92     }
 93 };
 94 
 95 
 96 template <typename T>
 97 class DelegateNmemHandlerChild : public IDelegateHandler
 98 {
 99     void (*m_pFunc)(int);
100 public:
101     DelegateNmemHandlerChild(void (*pFunc)(int)):m_pFunc(pFunc) {}
102 
103     void invoke(int value){
104         (*m_pFunc)(value);
105     }
106 };
107 
108 int main(int argc, char *argv[])
109 {
110     AF af;
111     BF bf;
112     /*
113     (af.*pFunc)(10);
114     AF *paf = &af;
115     (paf->*pFunc)(11);
116 
117     // template
118     DelegateHandler<AF> afT(&af, &AF::Func);
119     afT.invoke(3);
120 
121     DelegateHandler<BF> bfT(&bf, &BF::method);
122     bfT.invoke(4);
123 
124 
125     DelegateNmemHandler<void> nMemFunc(NonmemberFunc);
126     nMemFunc.invoke(5);
127 */
128     DelegateHandlerChild<AF> ac(&af, &AF::Func);
129     DelegateHandlerChild<BF> bc(&bf, &BF::method);
130     DelegateNmemHandlerChild<void> vc(NonmemberFunc);
131 
132     vector<IDelegateHandler *> handler;
133     handler.push_back(&ac);
134     handler.push_back(&bc);
135     handler.push_back(&vc);
136 
137     for(auto iter = handler.begin(); iter != handler.end(); iter++)
138         (*iter)->invoke(8);
139     return 0;
140 }
使用多态

 

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 
  4 #define debug(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl
  5 
  6 class AF
  7 {
  8 public:
  9     AF() {}
 10     void Func(int x){
 11         cout << "AF::Func " << x << endl;
 12     }
 13 };
 14 
 15 void (AF::*pFunc)(int) = &AF::Func;
 16 
 17 class BF
 18 {
 19 public:
 20     BF() {}
 21     void method(int x){
 22         cout << "BF::method " << x << endl;
 23     }
 24 };
 25 
 26 
 27 /* gcc-4.4.7 不支持特化与偏特化模板
 28 template <typename N>
 29 class Test<char, N>
 30 {
 31 public:
 32     Test( char i, N j ):a( i ), b( j )
 33     {
 34         cout<<"模版类偏特化"<< a<< ' ' << b << endl;
 35     }
 36 private:
 37     char a;
 38     N b;
 39 };
 40 */
 41 
 42 void NonmemberFunc(int value){
 43     printf("NonmemberFunc: %d\n", value);
 44 }
 45 
 46 /*
 47 template <typename T>
 48 class DelegateHandler
 49 {
 50     T *m_pT;
 51     void (T::*m_pFunc)(int);
 52 public:
 53     DelegateHandler(T *pT, void (T::*pFunc)(int)):m_pT(pT), m_pFunc(pFunc) {}
 54 
 55     void invoke(int value){
 56         (m_pT->*m_pFunc)(value);
 57     }
 58 };
 59 
 60 // 非成员函数
 61 template <typename T>
 62 class DelegateNmemHandler
 63 {
 64     void (*m_pFunc)(int);
 65 public:
 66     DelegateNmemHandler(void (*pFunc)(int)):m_pFunc(pFunc) {}
 67 
 68     void invoke(int value){
 69         (*m_pFunc)(value);
 70     }
 71 };
 72 
 73 // 多态
 74 class IDelegateHandler
 75 {
 76 public:
 77     virtual ~IDelegateHandler() {}
 78     virtual void invoke(int) = 0;
 79 };
 80 
 81 
 82 template <typename T>
 83 class DelegateHandlerChild : public IDelegateHandler
 84 {
 85     T *m_pT;
 86     void (T::*m_pFunc)(int);
 87 public:
 88     DelegateHandlerChild(T *pT, void (T::*pFunc)(int)):m_pT(pT), m_pFunc(pFunc) {}
 89 
 90     void invoke(int value){
 91         (m_pT->*m_pFunc)(value);
 92     }
 93 };
 94 
 95 
 96 template <typename T>
 97 class DelegateNmemHandlerChild : public IDelegateHandler
 98 {
 99     void (*m_pFunc)(int);
100 public:
101     DelegateNmemHandlerChild(void (*pFunc)(int)):m_pFunc(pFunc) {}
102 
103     void invoke(int value){
104         (*m_pFunc)(value);
105     }
106 };
107 
108 */
109 // 使用宏
110 #define DECLARE_PARAMS(...) __VA_ARGS__
111 #define DECLARE_ARGS(...) __VA_ARGS__
112 
113 //0个参数的委托
114 #define DELEGATE0(retType, name) \
115     DECLARE_DELEGATE(retType, name, DECLARE_PARAMS(void), )
116 
117 //1个参数的委托
118 #define DELEGATE1(retType, name, p1) \
119     DECLARE_DELEGATE( \
120         retType, \
121         name, \
122         DECLARE_PARAMS(p1 a), \
123         DECLARE_ARGS(a))
124 
125 //2个参数的委托
126 #define DELEGATE2(retType, name, p1, p2) \
127     DECLARE_DELEGATE( \
128         retType, \
129         name, \
130         DECLARE_PARAMS(p1 a, p2 b), \
131         DECLARE_ARGS(a, b))
132 
133 //3个参数的委托
134 #define DELEGATE3(retType, name, p1, p2, p3) \
135     DECLARE_DELEGATE( \
136         retType, \
137         name, \
138         DECLARE_PARAMS(p1 a, p2 b, p3 c), \
139         DECLARE_ARGS(a, b, c))
140 
141 //4个参数的委托
142 #define DELEGATE4(retType, name, p1, p2, p3, p4) \
143     DECLARE_DELEGATE( \
144         retType, \
145         name, \
146         DECLARE_PARAMS(p1 a, p2 b, p3 c, p4 d), \
147         DECLARE_ARGS(a, b, c, d))
148 
149 //5个参数的委托
150 #define DELEGATE5(retType, name, p1, p2, p3, p4, p5) \
151     DECLARE_DELEGATE( \
152         retType, \
153         name, \
154         DECLARE_PARAMS(p1 a, p2 b, p3 c, p4 d, p5 e), \
155         DECLARE_ARGS(a, b, c, d, e))
156 
157 //6个参数的委托
158 #define DELEGATE6(retType, name, p1, p2, p3, p4, p5, p6) \
159     DECLARE_DELEGATE( \
160         retType, \
161         name, \
162         DECLARE_PARAMS(p1 a, p2 b, p3 c, p4 d, p5 e, p6 f), \
163         DECLARE_ARGS(a, b, c, d, e, f))
164 
165 //7个参数的委托
166 #define DELEGATE7(retType, name, p1, p2, p3, p4, p5, p6, p7) \
167     DECLARE_DELEGATE( \
168         retType, \
169         name, \
170         DECLARE_PARAMS(p1 a, p2 b, p3 c, p4 d, p5 e, p6 f, p7 g), \
171         DECLARE_ARGS(a, b, c, d, e, f, g))
172 
173 //8个参数的委托
174 #define DELEGATE8(retType, name, p1, p2, p3, p4, p5, p6, p7, p8) \
175     DECLARE_DELEGATE( \
176         retType, \
177         name, \
178         DECLARE_PARAMS(p1 a, p2 b, p3 c, p4 d, p5 e, p6 f, p7 g, p8 h), \
179         DECLARE_ARGS(a, b, c, d, e, f, g, h))
180 
181 
182 
183 
184 #define DECLARE_DELEGATE(retType, name, params, args)                         \
185 class I##name {                                                               \
186 public:                                                                       \
187     virtual ~I##name() { }                                                    \
188     virtual retType Invoke(params) = 0;                                       \
189 };                                                                            \
190 template<typename T>                                                          \
191 class name : public I##name {                                                 \
192 public:                                                                       \
193     name(T* pType, retType (T::*pFunc)(params))                               \
194         : m_pType(pType), m_pFunc(pFunc) { }                                  \
195     retType Invoke(params) {                                                  \
196         return (m_pType->*m_pFunc)(args);                                     \
197     }                                                                         \
198 private:                                                                      \
199     T* m_pType; retType (T::*m_pFunc)(params);                                \
200 };                                                                            \
201 template<>                                                                    \
202 class name<void> : public I##name {                                           \
203 public:                                                                       \
204     name(retType (*pFunc)(params))                                            \
205         : m_pFunc(pFunc) { }                                                  \
206     retType Invoke(params) {                                                  \
207         return (*m_pFunc)(args);                                              \
208     }                                                                         \
209 private:                                                                      \
210     retType (*m_pFunc)(params);                                               \
211 }
212 
213 
214 DELEGATE0(void, Test0);
215 DELEGATE1(void, Test1, int);
216 DELEGATE2(void, Test2, int, int);
217 
218 void print(){
219     printf("Just 0 arg test\n");
220 }
221 
222 int main(int argc, char *argv[])
223 {
224     AF af;
225     BF bf;
226     /*
227     (af.*pFunc)(10);
228     AF *paf = &af;
229     (paf->*pFunc)(11);
230 
231     // template
232     DelegateHandler<AF> afT(&af, &AF::Func);
233     afT.invoke(3);
234 
235     DelegateHandler<BF> bfT(&bf, &BF::method);
236     bfT.invoke(4);
237 
238 
239     DelegateNmemHandler<void> nMemFunc(NonmemberFunc);
240     nMemFunc.invoke(5);
241 */
242     /*
243     DelegateHandlerChild<AF> ac(&af, &AF::Func);
244     DelegateHandlerChild<BF> bc(&bf, &BF::method);
245     DelegateNmemHandlerChild<void> vc(NonmemberFunc);
246 
247     vector<IDelegateHandler *> handler;
248     handler.push_back(&ac);
249     handler.push_back(&bc);
250     handler.push_back(&vc);
251 
252     for(auto iter = handler.begin(); iter != handler.end(); iter++)
253         (*iter)->invoke(8);
254     */
255 
256     Test0<void> t0(print);
257     t0.Invoke();
258 
259     Test1<AF> test(&af, &AF::Func);
260     test.Invoke(5);
261 
262     Test1<void> tet(NonmemberFunc);
263     tet.Invoke(10);
264 
265     return 0;
266 }
使用宏定义

 

posted @ 2017-11-06 15:53  PKICA  阅读(1068)  评论(1编辑  收藏  举报