C++委托模式

时间:2025-02-12 15:36:26

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

 #include <bits/stdc++.h>
using namespace std; #define debug(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl class AF
{
public:
AF() {}
void Func(int x){
cout << "AF::Func " << x << endl;
}
}; void (AF::*pFunc)(int) = &AF::Func; int main(int argc, char *argv[])
{
AF af;
(af.*pFunc)();
AF *paf = &af;
(paf->*pFunc)(); return ;
}

成员函数指针的操作

 #include <bits/stdc++.h>
using namespace std; #define debug(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl class AF
{
public:
AF() {}
void Func(int x){
cout << "AF::Func " << x << endl;
}
}; void (AF::*pFunc)(int) = &AF::Func; class BF
{
public:
BF() {}
void method(int x){
cout << "BF::method " << x << endl;
}
}; /* gcc-4.4.7 不支持特化与偏特化模板
template <typename N>
class Test<char, N>
{
public:
Test( char i, N j ):a( i ), b( j )
{
cout<<"模版类偏特化"<< a<< ' ' << b << endl;
}
private:
char a;
N b;
};
*/ /*
template<typename T>
class DelegateNoneMemFunc
{
void (*m_pFunc)(int);
public:
DelegateNoneMemFunc(void (*pFunc)(int) ):m_pFunc(pFunc) {}
void invoke(int value){
(*m_pFunc)(value);
}
};
*/ void NonmemberFunc(int value){
printf("NonmemberFunc: %d\n", value);
} class IDelegateHandler
{
public:
virtual ~IDelegateHandler() {}
virtual void invoke(int) = ;
}; /*
template <typename T>
class DelegateHandlerChild : public IDelegateHandler
{
public:
DelegateHandlerChild() {}
void invoke(int value){
printf("DelegateHandlerChild invoke: %d", value);
}
};
*/ //*
template <typename T>
class DelegateHandler
{
T *m_pT;
void (T::*m_pFunc)(int);
public:
DelegateHandler(T *pT, void (T::*pFunc)(int)):m_pT(pT), m_pFunc(pFunc) {} void invoke(int value){
(m_pT->*m_pFunc)(value);
}
}; template <typename T>
class DelegateNmemHandler
{
void (*m_pFunc)(int);
public:
DelegateNmemHandler(void (*pFunc)(int)):m_pFunc(pFunc) {} void invoke(int value){
(*m_pFunc)(value);
}
}; int main(int argc, char *argv[])
{
AF af;
BF bf; (af.*pFunc)();
AF *paf = &af;
(paf->*pFunc)(); // template
DelegateHandler<AF> afT(&af, &AF::Func);
afT.invoke(); DelegateHandler<BF> bfT(&bf, &BF::method);
bfT.invoke(); // 非成员函数
DelegateNmemHandler<void> nMemFunc(NonmemberFunc);
nMemFunc.invoke(); return ;
} class LuggageCompartment {
private:
int m_iLuggage; //私有变量,保存现在的行李总数
public:LuggageCompartment() {
m_iLuggage = ;
} //构造函数
int TakeoutLuggage() //取出一件行李
{
if (m_iLuggage != )
m_iLuggage--;
return m_iLuggage;
}
int InsertLuggage() //放入一件行李
{
return (++m_iLuggage);
}
int checkLuggage() //检查行李总数
{
return m_iLuggage;
}
}; class FlightSegment {
private:LuggageCompartment * m_pLC; //成员指针
public:void SetLuggageCompartment(LuggageCompartment * pLC) {
m_pLC = pLC;
} //设置成员指针
FlightSegment() //构造函数将成员指针初始化为null
{
m_pLC = NULL;
}
int checkLuggage() {
if (m_pLC == NULL)
return -;
return m_pLC->checkLuggage(); //将函数调用委托给成员指针
}
};

使用类模板

 #include <bits/stdc++.h>
using namespace std; #define debug(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl class AF
{
public:
AF() {}
void Func(int x){
cout << "AF::Func " << x << endl;
}
}; void (AF::*pFunc)(int) = &AF::Func; class BF
{
public:
BF() {}
void method(int x){
cout << "BF::method " << x << endl;
}
}; /* gcc-4.4.7 不支持特化与偏特化模板
template <typename N>
class Test<char, N>
{
public:
Test( char i, N j ):a( i ), b( j )
{
cout<<"模版类偏特化"<< a<< ' ' << b << endl;
}
private:
char a;
N b;
};
*/ void NonmemberFunc(int value){
printf("NonmemberFunc: %d\n", value);
} template <typename T>
class DelegateHandler
{
T *m_pT;
void (T::*m_pFunc)(int);
public:
DelegateHandler(T *pT, void (T::*pFunc)(int)):m_pT(pT), m_pFunc(pFunc) {} void invoke(int value){
(m_pT->*m_pFunc)(value);
}
}; // 非成员函数
template <typename T>
class DelegateNmemHandler
{
void (*m_pFunc)(int);
public:
DelegateNmemHandler(void (*pFunc)(int)):m_pFunc(pFunc) {} void invoke(int value){
(*m_pFunc)(value);
}
}; class IDelegateHandler
{
public:
virtual ~IDelegateHandler() {}
virtual void invoke(int) = ;
}; template <typename T>
class DelegateHandlerChild : public IDelegateHandler
{
T *m_pT;
void (T::*m_pFunc)(int);
public:
DelegateHandlerChild(T *pT, void (T::*pFunc)(int)):m_pT(pT), m_pFunc(pFunc) {} void invoke(int value){
(m_pT->*m_pFunc)(value);
}
}; template <typename T>
class DelegateNmemHandlerChild : public IDelegateHandler
{
void (*m_pFunc)(int);
public:
DelegateNmemHandlerChild(void (*pFunc)(int)):m_pFunc(pFunc) {} void invoke(int value){
(*m_pFunc)(value);
}
}; int main(int argc, char *argv[])
{
AF af;
BF bf;
/*
(af.*pFunc)(10);
AF *paf = &af;
(paf->*pFunc)(11); // template
DelegateHandler<AF> afT(&af, &AF::Func);
afT.invoke(3); DelegateHandler<BF> bfT(&bf, &BF::method);
bfT.invoke(4); DelegateNmemHandler<void> nMemFunc(NonmemberFunc);
nMemFunc.invoke(5);
*/
DelegateHandlerChild<AF> ac(&af, &AF::Func);
DelegateHandlerChild<BF> bc(&bf, &BF::method);
DelegateNmemHandlerChild<void> vc(NonmemberFunc); vector<IDelegateHandler *> handler;
handler.push_back(&ac);
handler.push_back(&bc);
handler.push_back(&vc); for(auto iter = handler.begin(); iter != handler.end(); iter++)
(*iter)->invoke();
return ;
}

使用多态

 #include <bits/stdc++.h>
using namespace std; #define debug(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl class AF
{
public:
AF() {}
void Func(int x){
cout << "AF::Func " << x << endl;
}
}; void (AF::*pFunc)(int) = &AF::Func; class BF
{
public:
BF() {}
void method(int x){
cout << "BF::method " << x << endl;
}
}; /* gcc-4.4.7 不支持特化与偏特化模板
template <typename N>
class Test<char, N>
{
public:
Test( char i, N j ):a( i ), b( j )
{
cout<<"模版类偏特化"<< a<< ' ' << b << endl;
}
private:
char a;
N b;
};
*/ void NonmemberFunc(int value){
printf("NonmemberFunc: %d\n", value);
} /*
template <typename T>
class DelegateHandler
{
T *m_pT;
void (T::*m_pFunc)(int);
public:
DelegateHandler(T *pT, void (T::*pFunc)(int)):m_pT(pT), m_pFunc(pFunc) {} void invoke(int value){
(m_pT->*m_pFunc)(value);
}
}; // 非成员函数
template <typename T>
class DelegateNmemHandler
{
void (*m_pFunc)(int);
public:
DelegateNmemHandler(void (*pFunc)(int)):m_pFunc(pFunc) {} void invoke(int value){
(*m_pFunc)(value);
}
}; // 多态
class IDelegateHandler
{
public:
virtual ~IDelegateHandler() {}
virtual void invoke(int) = 0;
}; template <typename T>
class DelegateHandlerChild : public IDelegateHandler
{
T *m_pT;
void (T::*m_pFunc)(int);
public:
DelegateHandlerChild(T *pT, void (T::*pFunc)(int)):m_pT(pT), m_pFunc(pFunc) {} void invoke(int value){
(m_pT->*m_pFunc)(value);
}
}; template <typename T>
class DelegateNmemHandlerChild : public IDelegateHandler
{
void (*m_pFunc)(int);
public:
DelegateNmemHandlerChild(void (*pFunc)(int)):m_pFunc(pFunc) {} void invoke(int value){
(*m_pFunc)(value);
}
}; */
// 使用宏
#define DECLARE_PARAMS(...) __VA_ARGS__
#define DECLARE_ARGS(...) __VA_ARGS__ //0个参数的委托
#define DELEGATE0(retType, name) \
DECLARE_DELEGATE(retType, name, DECLARE_PARAMS(void), ) //1个参数的委托
#define DELEGATE1(retType, name, p1) \
DECLARE_DELEGATE( \
retType, \
name, \
DECLARE_PARAMS(p1 a), \
DECLARE_ARGS(a)) //2个参数的委托
#define DELEGATE2(retType, name, p1, p2) \
DECLARE_DELEGATE( \
retType, \
name, \
DECLARE_PARAMS(p1 a, p2 b), \
DECLARE_ARGS(a, b)) //3个参数的委托
#define DELEGATE3(retType, name, p1, p2, p3) \
DECLARE_DELEGATE( \
retType, \
name, \
DECLARE_PARAMS(p1 a, p2 b, p3 c), \
DECLARE_ARGS(a, b, c)) //4个参数的委托
#define DELEGATE4(retType, name, p1, p2, p3, p4) \
DECLARE_DELEGATE( \
retType, \
name, \
DECLARE_PARAMS(p1 a, p2 b, p3 c, p4 d), \
DECLARE_ARGS(a, b, c, d)) //5个参数的委托
#define DELEGATE5(retType, name, p1, p2, p3, p4, p5) \
DECLARE_DELEGATE( \
retType, \
name, \
DECLARE_PARAMS(p1 a, p2 b, p3 c, p4 d, p5 e), \
DECLARE_ARGS(a, b, c, d, e)) //6个参数的委托
#define DELEGATE6(retType, name, p1, p2, p3, p4, p5, p6) \
DECLARE_DELEGATE( \
retType, \
name, \
DECLARE_PARAMS(p1 a, p2 b, p3 c, p4 d, p5 e, p6 f), \
DECLARE_ARGS(a, b, c, d, e, f)) //7个参数的委托
#define DELEGATE7(retType, name, p1, p2, p3, p4, p5, p6, p7) \
DECLARE_DELEGATE( \
retType, \
name, \
DECLARE_PARAMS(p1 a, p2 b, p3 c, p4 d, p5 e, p6 f, p7 g), \
DECLARE_ARGS(a, b, c, d, e, f, g)) //8个参数的委托
#define DELEGATE8(retType, name, p1, p2, p3, p4, p5, p6, p7, p8) \
DECLARE_DELEGATE( \
retType, \
name, \
DECLARE_PARAMS(p1 a, p2 b, p3 c, p4 d, p5 e, p6 f, p7 g, p8 h), \
DECLARE_ARGS(a, b, c, d, e, f, g, h)) #define DECLARE_DELEGATE(retType, name, params, args) \
class I##name { \
public: \
virtual ~I##name() { } \
virtual retType Invoke(params) = ; \
}; \
template<typename T> \
class name : public I##name { \
public: \
name(T* pType, retType (T::*pFunc)(params)) \
: m_pType(pType), m_pFunc(pFunc) { } \
retType Invoke(params) { \
return (m_pType->*m_pFunc)(args); \
} \
private: \
T* m_pType; retType (T::*m_pFunc)(params); \
}; \
template<> \
class name<void> : public I##name { \
public: \
name(retType (*pFunc)(params)) \
: m_pFunc(pFunc) { } \
retType Invoke(params) { \
return (*m_pFunc)(args); \
} \
private: \
retType (*m_pFunc)(params); \
} DELEGATE0(void, Test0);
DELEGATE1(void, Test1, int);
DELEGATE2(void, Test2, int, int); void print(){
printf("Just 0 arg test\n");
} int main(int argc, char *argv[])
{
AF af;
BF bf;
/*
(af.*pFunc)(10);
AF *paf = &af;
(paf->*pFunc)(11); // template
DelegateHandler<AF> afT(&af, &AF::Func);
afT.invoke(3); DelegateHandler<BF> bfT(&bf, &BF::method);
bfT.invoke(4); DelegateNmemHandler<void> nMemFunc(NonmemberFunc);
nMemFunc.invoke(5);
*/
/*
DelegateHandlerChild<AF> ac(&af, &AF::Func);
DelegateHandlerChild<BF> bc(&bf, &BF::method);
DelegateNmemHandlerChild<void> vc(NonmemberFunc); vector<IDelegateHandler *> handler;
handler.push_back(&ac);
handler.push_back(&bc);
handler.push_back(&vc); for(auto iter = handler.begin(); iter != handler.end(); iter++)
(*iter)->invoke(8);
*/ Test0<void> t0(print);
t0.Invoke(); Test1<AF> test(&af, &AF::Func);
test.Invoke(); Test1<void> tet(NonmemberFunc);
tet.Invoke(); return ;
}

使用宏定义