由於這段時間較忙,自從上次我發布了一個供初學者學習的“MFC”程序後,就沒怎麼寫文章了。這段時間在忙新的項目,項目邏輯還算復雜,用了好多有閃光點的技術,下面我要討論的是閃光點之一:在C++中實現C#的delegate機制。
用過C#的朋友都覺得C#的確是個好語言,但有時由於特殊需要,不得不用C++,在C++中要想實現C#中的Thread等也是可行的,不過代碼要稍復雜。
以例子來說明:
class Sdk
{
public:
void DoSomething();
};
class client
{
public:
Sdk m_sdk;
void DoSomething{}
{
m_sdk.DoSomething();
}
void OnMessage()
{
//
}
}
這個例子比較簡單,Sdk用來實現某個功能,client是客戶程序,要想在m_sdk做處理的時候能發送通知到client,有幾個方法可以實現, 其一是發線程消息,其一是回調,但傳統的回調對面向對象的支持並不好,這就是實現delegate的出發點。 下面是一個實現:
class Delegate
{
public:
virtual ~Delegate(void){ }
virtual void operator()(void) = 0;
};
template <typename C>
class DelegateImpl : public Delegate
{
private:
typedef void (C::*F)(void);
C* m_class;
F m_fun;
public:
DelegateImpl(C* c, F f){ m_class = c; m_fun = f; }
virtual void operator()(void)
{
if(m_class)
return (m_class-> *m_fun)();
}
};
有了上面代碼,再稍加修改sdk和client代碼就能實現事件通知了:
class Sdk
{
public:
Delegate* pEvent;
sdk(): pEvent(NULL) { }
void DoSomething()
{
cout < <"opened" < <endl;
if(pEvent != NULL)
(*pEvent)( );
}
};
class client
{
private:
Sdk m_sdk;
public:
client()
{
m_sdk.pEvent = new Delegate <client> (this, OnOpen);
}
void DoSomething()
{
m_sdk.DoSomething();
}
void OnDoSomething()
{
cout < <"event fired" < <endl;
}
}
上面的實現有不少局限性,事件處理方法的返回值和參數設置不是十分靈活,因此可以用宏定義優代以上代碼,下面給出我的全部代碼,請朋友指正。
//////////////////////////////////////////////////////////////////////////
//delegate 0
#define DEFINE_DELEGATE(NAME, R)\
class NAME##Delegate\
{\
public:\
virtual ~NAME##Delegate(void){ }\
virtual R operator()(void) = 0;\
};\
template<typename C>\
class NAME##DelegateImpl : public NAME##Delegate\
{\
private:\
typedef R (C::*F)(void);\
C* m_class;\
F m_fun;\
public:\
NAME##DelegateImpl(C* c, F f){ m_class = c; m_fun = f; }\
virtual R operator()(void)\
{\
if(m_class)\
return (m_class->*m_fun)();\
}\
};\
template<typename C, typename F>\
NAME##Delegate* Make##NAME##Delegate(C* c, F f)\
{\
return new NAME##DelegateImpl<C>(c, f);\
}
//////////////////////////////////////////////////////////////////////////
//delegate 1
#define DEFINE_DELEGATE(NAME, R, P1)\
class NAME##Delegate\
{\
public:\
virtual ~NAME##Delegate(void){ }\
virtual R operator()(P1 p1) = 0;\
};\
template<typename C>\
class NAME##DelegateImpl : public NAME##Delegate\
{\
private:\
typedef R (C::*F)(P1);\
C* m_class;\
F m_fun;\
public:\
NAME##DelegateImpl(C* c, F f){ m_class = c; m_fun = f; }\
virtual R operator()(P1 p1)\
{\
if(m_class)\
return (m_class->*m_fun)(p1);\
}\
};\
template<typename C, typename F>\
NAME##Delegate* Make##NAME##Delegate(C* c, F f)\
{\
return new NAME##DelegateImpl<C>(c, f);\
}
//////////////////////////////////////////////////////////////////////////
//delegate 2
#define DEFINE_DELEGATE(NAME, R, P1, P2)\
class NAME##Delegate\
{\
public:\
virtual ~NAME##Delegate(void){ }\
virtual R operator()(P1 p1, P2 p2) = 0;\
};\
template<typename C>\
class NAME##DelegateImpl : public NAME##Delegate\
{\
private:\
typedef R (C::*F)(P1, P2);\
C* m_class;\
F m_fun;\
public:\
NAME##DelegateImpl(C* c, F f){ m_class = c; m_fun = f; }\
virtual R operator()(P1 p1, P2 p2)\
{\
if(m_class)\
return (m_class->*m_fun)(p1, p2);\
}\
};\
template<typename C, typename F>\
NAME##Delegate* Make##NAME##Delegate(C* c, F f)\
{\
return new NAME##DelegateImpl<C>(c, f);\
}
/*
//////////////////////////////////////////////////////////////////////////
//sample
DEFINE_DELEGATE(Open, void, int, string)
class sdk
{
public:
OpenDelegate* pEvent;
sdk(): pEvent(NULL) { }
void Open()
{
cout<<"opened"<<endl;
if(pEvent != NULL)
(*pEvent)(100, "你好");
}
};
class client
{
private:
sdk m_sdk;
public:
client()
{
m_sdk.pEvent = MakeOpenDelegate(this, OnOpen2);
}
void Open()
{
m_sdk.Open();
}
void OnOpen()
{
cout<<"Open event fired"<<endl;
}
void OnOpen2(int t, string str)
{
cout<<"Open event fired, Param = "<<t<<"param2 = "<<str.c_str()<<endl;
}
};
*/
本文配套源碼