c++回調函數 callback

                          C++中實現回調機制的幾種方式

(1)Callback方式
Callback的本質是設置一個函數指針進去,而後在須要須要觸發某個事件時調用該方法, 好比Windows的窗口消息處理函數就是這種類型。好比下面的示例代碼,咱們在Download完成時須要觸發一個通知外面的事件: 程序員

[cpp]  view plain copy
  1. typedef void (__stdcall *DownloadCallback)(const char* pURL, bool bOK);  
  2. void DownloadFile(const char* pURL, DownloadCallback callback)  
  3. {  
  4.     cout << "downloading: " << pURL << "" << endl;  
  5.     callback(pURL, true);  
  6. }  
  7. void __stdcall OnDownloadFinished(const char* pURL, bool bOK)  
  8. {  
  9.     cout << "OnDownloadFinished, URL:" << pURL << "    status:" << bOK << endl;  
  10. }  

(2)Sink方式
Sink的本質是你按照對方要求實現一個C++接口,而後把你實現的接口設置給對方,對方須要觸發事件時調用該接口, COM中鏈接點就是居於這種方式。上面下載文件的需求,若是用Sink實現,代碼以下: 函數

[cpp]  view plain copy
  1. class IDownloadSink  
  2. {  
  3. public:  
  4.     virtual void OnDownloadFinished(const char* pURL, bool bOK) = 0;  
  5. };  
  6.   
  7.   
  8. class CMyDownloader  
  9. {  
  10. public:  
  11.     CMyDownloader(IDownloadSink* pSink)  
  12.         :m_pSink(pSink)  
  13.     {  
  14.     }  
  15.   
  16.     void DownloadFile(const char* pURL)  
  17.     {  
  18.         cout << "downloading: " << pURL << "" << endl;  
  19.         if(m_pSink != NULL)  
  20.         {  
  21.             m_pSink->OnDownloadFinished(pURL, true);  
  22.         }  
  23.     }  
  24.   
  25. private:  
  26.     IDownloadSink* m_pSink;  
  27. };  
  28.   
  29. class CMyFile: public IDownloadSink  
  30. {  
  31. public:  
  32.     void download()  
  33.     {  
  34.         CMyDownloader downloader(this);  
  35.         downloader.DownloadFile("www.baidu.com");  
  36.     }  
  37.   
  38.     virtual void OnDownloadFinished(const char* pURL, bool bOK)  
  39.     {  
  40.         cout << "OnDownloadFinished, URL:" << pURL << "    status:" << bOK << endl;  
  41.     }  
  42. };  

 

(3)Delegate方式
    Delegate的本質是設置成員函數指針給對方,而後讓對方在須要觸發事件時調用。C#中用Delegate的方式實現Event,讓C++程序員非常羨慕,C++中由於語言自己的關係,要實現Delegate仍是很麻煩的。上面的例子咱們用Delegate的方式實現以下: 測試

[cpp]  view plain copy
  1. class CDownloadDelegateBase  
  2. {  
  3. public:  
  4.     virtual void Fire(const char* pURL, bool bOK) = 0;  
  5. };  
  6.   
  7. template<typename O, typename T>  
  8. class CDownloadDelegate: public CDownloadDelegateBase  
  9. {  
  10.     typedef void (T::*Fun)(const char*, bool);  
  11. public:  
  12.     CDownloadDelegate(O* pObj = NULL, Fun pFun = NULL)  
  13.         :m_pFun(pFun), m_pObj(pObj)  
  14.     {  
  15.     }  
  16.      
  17.     virtual void Fire(const char* pURL, bool bOK)  
  18.     {  
  19.         if(m_pFun != NULL  
  20.             && m_pObj != NULL)  
  21.         {  
  22.             (m_pObj->*m_pFun)(pURL, bOK);  
  23.         }  
  24.     }  
  25.   
  26. private:  
  27.     Fun m_pFun;  
  28.     O* m_pObj;  
  29. };  
  30.   
  31. template<typename O, typename T>  
  32. CDownloadDelegate<O,T>* MakeDelegate(O* pObject, void (T::*pFun)(const char* pURL, bool))  
  33. {  
  34.     return new CDownloadDelegate<O, T>(pObject, pFun);  
  35. }  
  36.   
  37. class CDownloadEvent  
  38. {  
  39. public:  
  40.     ~CDownloadEvent()  
  41.     {  
  42.         vector<CDownloadDelegateBase*>::iterator itr = m_arDelegates.begin();  
  43.         while (itr != m_arDelegates.end())  
  44.         {  
  45.             delete *itr;  
  46.             ++itr;  
  47.         }  
  48.         m_arDelegates.clear();  
  49.     }  
  50.   
  51.     void operator += (CDownloadDelegateBase* p)  
  52.     {  
  53.         m_arDelegates.push_back(p);  
  54.     }  
  55.   
  56.     void operator -= (CDownloadDelegateBase* p)  
  57.     {  
  58.         ITR itr = remove(m_arDelegates.begin(), m_arDelegates.end(), p);  
  59.   
  60.         ITR itrTemp = itr;  
  61.         while (itrTemp != m_arDelegates.end())  
  62.         {  
  63.             delete *itr;  
  64.             ++itr;  
  65.         }  
  66.         m_arDelegates.erase(itr, m_arDelegates.end());  
  67.     }  
  68.   
  69.     void operator()(const char* pURL, bool bOK)  
  70.     {  
  71.         ITR itrTemp = m_arDelegates.begin();  
  72.         while (itrTemp != m_arDelegates.end())  
  73.         {  
  74.             (*itrTemp)->Fire(pURL, bOK);  
  75.             ++itrTemp;  
  76.         }  
  77.     }  
  78.   
  79. private:  
  80.     vector<CDownloadDelegateBase*> m_arDelegates;  
  81.     typedef vector<CDownloadDelegateBase*>::iterator ITR;  
  82. };  
  83.   
  84.   
  85. class CMyDownloaderEx  
  86. {  
  87. public:  
  88.     void DownloadFile(const char* pURL)  
  89.     {  
  90.         cout << "downloading: " << pURL << "" << endl;  
  91.         downloadEvent(pURL, true);  
  92.     }  
  93.   
  94.     CDownloadEvent downloadEvent;  
  95. };  
  96.   
  97. class CMyFileEx  
  98. {  
  99. public:  
  100.     void download()  
  101.     {  
  102.         CMyDownloaderEx downloader;  
  103.         downloader.downloadEvent += MakeDelegate(this, &CMyFileEx::OnDownloadFinished);  
  104.         downloader.DownloadFile("www.baidu.com");  
  105.     }  
  106.   
  107.     virtual void OnDownloadFinished(const char* pURL, bool bOK)  
  108.     {  
  109.         cout << "OnDownloadFinished, URL:" << pURL << "    status:" << bOK << endl;  
  110.     }  
  111. };  

 

    能夠看到Delegate的方式代碼量比上面其餘2種方式大多了,而且咱們上面是固定參數數量和類型的實現方式,若是要實現可變參數,要更加麻煩的多。可變參數的方式能夠參考這2種實現: this

Yet Another C#-style Delegate Class in Standard C++
Member Function Pointers and the Fastest Possible C++ Delegates spa

 

咱們能夠用下面的代碼測試咱們上面的實現: .net

[cpp]  view plain copy
  1. int _tmain(int argc, _TCHAR* argv[])  
  2. {  
  3.   
  4.     DownloadFile("www.baidu.com", OnDownloadFinished);  
  5.   
  6.     CMyFile f1;  
  7.     f1.download();  
  8.   
  9.     CMyFileEx ff;  
  10.     ff.download();  
  11.   
  12.     system("pause");  
  13.   
  14.     return 0;  
  15. }  


最後簡單比較下上面3種實現回調的方法: 指針


第一種Callback的方法是面向過程的,使用簡單並且靈活,正如C語言自己。
第二種Sink的方法是面向對象的,在C++裏使用較多, 能夠在一個Sink裏封裝一組回調接口,適用於一系列比較固定的回調事件。
第三種Delegate的方法也是面向對象的,和Sink封裝一組接口不一樣,Delegate的封裝是以函數爲單位,粒度比Sink更小更靈活。 對象

你更傾向於用哪一種方式來實現回調? blog

相關文章
相關標籤/搜索