zoukankan      html  css  js  c++  java
  • [转]C/C++实现回调机制的几种方式(回调、槽、代理)

    转自:https://www.jianshu.com/p/4f907bba6d5f

    (1)Callback方式(回调)

    Callback的本质是设置一个函数指针进去,然后在需要需要触发某个事件时调用该方法, 比如Windows的窗口消息处理函数就是这种类型。比如下面的示例代码,我们在Download完成时需要触发一个通知外面的事件:

    #include <iostream>
    
    typedef void (__stdcall *DownloadCallback)(const char *pURL,bool OK);
    
    void DownLoadFile(const char *pURL,DownloadCallback callback)
    {
    
        std::cout<<"downloading..."<<pURL<<""<<std::endl;
        callback(pURL,true);
    }
    
    void __stdcall onDownloadFinished(const char* pURL,bool bOK)
    {
        std::cout<<"onDownloadFinished..."<<pURL<<"   status:"<<bOK<<std::endl;
    }
    
    void main()
    {
        DownLoadFile("http://wwww.baidu.com",onDownloadFinished);
    
        system("pause");
    }

    (2)Sink方式(槽)

    Sink的本质是你按照对方要求实现一个C++接口,然后把你实现的接口设置给对方,对方需要触发事件时调用该接口, COM中连接点就是居于这种方式。上面下载文件的需求,如果用Sink实现,代码如下:

    //2. sink方式
    
    class IDownloadSink
    {
    public:  
        virtual void OnDownloadFinished(const char *pURL,bool bOK) = 0;
    };
    
    class CMyDownloader
    {
    public:  
        CMyDownloader (IDownloadSink *pSink)
            :m_pSink(pSink)
        {
    
        }
    
        void DownloadFile(const char* pURL)
        {
            std::cout<<"downloading..."<<pURL<<""<<std::endl;
            if(m_pSink!=NULL)
            {
                m_pSink->OnDownloadFinished(pURL,true);
            }
        }
    
    private:
        IDownloadSink *m_pSink;
    };
    
    
    class CMyFile:public IDownloadSink
    {
    public:  
        void download()
        {
            CMyDownloader downloader(this);
            downloader.DownloadFile("www.baidu.com");
        }
    
        virtual void OnDownloadFinished(const char *pURL,bool bOK) 
        {
            std::cout<<"onDownloadFinished..."<<pURL<<"   status:"<<bOK<<std::endl;
        }
    };
    
    void main()
    {
        CMyFile *file = new CMyFile();
        file->download();
    
        system("pause");
    }

    小结:从上面的代码中可以看出,IDownloadSink 接口是一种约定,CMyDownloader 是接口的使用者,CMyFile是接口的实现者。他们两者之间是通过IDownloadSink 进行解耦的,使他们可以专注于自己的实现,接口的使用者CMyDownloader只管怎么使用接口的,接口怎么实现他不必关心;而接口的实现者IDownloadSink 他只管实现接口,接口怎么去用他不关心。维系两者关系的就是接口约定IDownloadSink 。

    (3)Delegate方式(代理)

    Delegate的本质是设置成员函数指针给对方,然后让对方在需要触发事件时调用。C#中用Delegate的方式实现Event,让C++程序员很是羡慕,C++中因为语言本身的关系,要实现Delegate还是很麻烦的。上面的例子我们用Delegate的方式实现如下:

    class CDownloadDelegateBase  
    {  
    public:  
        virtual void Fire(const char* pURL, bool bOK) = 0;  
    };  
    
    template<typename O, typename T>  
    class CDownloadDelegate: public CDownloadDelegateBase  
    {  
        typedef void (T::*Fun)(const char*, bool);  
    public:  
        CDownloadDelegate(O* pObj = NULL, Fun pFun = NULL)  
            :m_pFun(pFun), m_pObj(pObj)  
        {  
        }  
    
        virtual void Fire(const char* pURL, bool bOK)  
        {  
            if(m_pFun != NULL  
                && m_pObj != NULL)  
            {  
                (m_pObj->*m_pFun)(pURL, bOK);  
            }  
        }  
    
    private:  
        Fun m_pFun;  
        O* m_pObj;  
    };  
    
    template<typename O, typename T>  
    CDownloadDelegate<O,T>* MakeDelegate(O* pObject, void (T::*pFun)(const char* pURL, bool))  
    {  
        return new CDownloadDelegate<O, T>(pObject, pFun);  
    }  
    
    class CDownloadEvent  
    {  
    public:  
        ~CDownloadEvent()  
        {  
            vector<CDownloadDelegateBase*>::iterator itr = m_arDelegates.begin();  
            while (itr != m_arDelegates.end())  
            {  
                delete *itr;  
                ++itr;  
            }  
            m_arDelegates.clear();  
        }  
    
        void operator += (CDownloadDelegateBase* p)  
        {  
            m_arDelegates.push_back(p);  
        }  
    
        void operator -= (CDownloadDelegateBase* p)  
        {  
            ITR itr = remove(m_arDelegates.begin(), m_arDelegates.end(), p);  
    
            ITR itrTemp = itr;  
            while (itrTemp != m_arDelegates.end())  
            {  
                delete *itr;  
                ++itr;  
            }  
            m_arDelegates.erase(itr, m_arDelegates.end());  
        }  
    
        void operator()(const char* pURL, bool bOK)  
        {  
            ITR itrTemp = m_arDelegates.begin();  
            while (itrTemp != m_arDelegates.end())  
            {  
                (*itrTemp)->Fire(pURL, bOK);  
                ++itrTemp;  
            }  
        }  
    
    private:  
        vector<CDownloadDelegateBase*> m_arDelegates;  
        typedef vector<CDownloadDelegateBase*>::iterator ITR;  
    };  
    
    
    class CMyDownloaderEx  
    {  
    public:  
        void DownloadFile(const char* pURL)  
        {  
            cout << "downloading: " << pURL << "" << endl;  
            downloadEvent(pURL, true);  
        }  
    
        CDownloadEvent downloadEvent;  
    };  
    
    class CMyFileEx  
    {  
    public:  
        void download()  
        {  
            CMyDownloaderEx downloader;  
            downloader.downloadEvent += MakeDelegate(this, &CMyFileEx::OnDownloadFinished);  
            downloader.DownloadFile("www.baidu.com");  
        }  
    
        virtual void OnDownloadFinished(const char* pURL, bool bOK)  
        {  
            cout << "OnDownloadFinished, URL:" << pURL << "    status:" << bOK << endl;  
        }  
    }; 

    可以看到Delegate的方式代码量比上面其他2种方式大多了,并且我们上面是固定参数数量和类型的实现方式,如果要实现可变参数,要更加麻烦的多。可变参数的方式可以参考这2种实现:

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

    我们可以用下面的代码测试我们上面的实现:

    int _tmain(int argc, _TCHAR* argv[])  
    {  
    
        DownloadFile("www.baidu.com", OnDownloadFinished);  
    
        CMyFile f1;  
        f1.download();  
    
        CMyFileEx ff;  
        ff.download();  
    
        system("pause");  
    
        return 0;  
    } 

    最后简单比较下上面3种实现回调的方法:

    第一种Callback的方法是面向过程的,使用简单而且灵活,正如C语言本身。

    第二种Sink的方法是面向对象的,在C++里使用较多, 可以在一个Sink里封装一组回调接口,适用于一系列比较固定的回调事件。

    第三种Delegate的方法也是面向对象的,和Sink封装一组接口不同,Delegate的封装是以函数为单位,粒度比Sink更小更灵活。

  • 相关阅读:
    源码
    Leetcode 230. 二叉搜索树中第K小的元素 中序遍历
    Leetcode 160. 相交链表 哈希 链表
    Leetcode 142. 环形链表 II
    Leetcode 217. 存在重复元素 哈希 排序
    asp.mvc2.0资料
    关于CRM的介绍
    WPf控件模板缺省样式
    皮肤制作工具
    关于linq的用法
  • 原文地址:https://www.cnblogs.com/lyggqm/p/12010005.html
Copyright © 2011-2022 走看看