用过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);\}/*////////////////////////////////////////////////////////////////////////////sampleDEFINE_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;}};*/