• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            在上文 在C++中實現事件(委托) 中我們實現的C#里委托方式的事件處理, 雖然使用很方便,但是感覺似乎少了一點C#的味道, 下面我們嘗試把它改成真正的C#版。

            其實要改成真正的C#版,我們主要要做2件事, 一是吧CEventHandler放到外面,可以讓外部直接構造, 二是實現operator +=和operator -=, 下面是我的實現代碼:
            #pragma once

            #include <functional>
            #include <algorithm>
            #include <vector>
            #include <assert.h>

            namespace Common
            {
                typedef void* cookie_type;

                template<typename TR, typename T1, typename T2>
                class CEventHandler 
                {
                public:
                    typedef TR return_type;
                    typedef T1 first_type;
                    typedef T2 second_type;

                    typedef std::function<return_type (first_type, second_type)> handler_type;

                    CEventHandler(const CEventHandler& h)
                    {
                        _handler = h._handler;
                        assert(_handler != nullptr);
                    }

                    CEventHandler(handler_type h)
                    {
                        _handler = h;
                        assert(_handler != nullptr);
                    }

                    template<typename class_type, typename class_fun>
                    CEventHandler(class_type* pThis, class_fun object_function)
                    {
                        using namespace std::placeholders;
                        _handler = std::bind(object_function, pThis, _1, _2);
                        assert(_handler != nullptr);
                    }

                    return_type operator()(first_type p1, second_type p2)
                    {
                        return_type ret = return_type();
                        assert(_handler != nullptr);
                        if(_handler != nullptr) ret = _handler(p1, p2);
                        return ret;
                    }

                    handler_type _handler;
                };

                template<typename EventHandler>
                class CEvent
                {
                public:
                    typedef EventHandler event_handler_type;
                    typedef typename event_handler_type::return_type return_type;
                    typedef typename event_handler_type::first_type first_type;
                    typedef typename event_handler_type::second_type second_type;
                    typedef typename event_handler_type::handler_type handler_type;

                    ~CEvent()
                    {
                        Clear();
                    }

                    return_type operator()(first_type p1, second_type p2)
                    {
                        return_type ret = return_type();
                        size_t size = _handlers.size();
                        for(auto p : _handlers)
                        {
                            ret = p->operator()(p1, p2);
                        }
                        return ret;
                    }

                    cookie_type AddHandler(const event_handler_type& h)
                    {
                        event_handler_type*p = new(nothrow)  event_handler_type(h);
                        if(p != nullptr) _handlers.push_back(p);
                        return (cookie_type)p;
                    }

                    void RemoveHandler(cookie_type cookie)
                    {
                        event_handler_type* p = (event_handler_type*)cookie;

                        auto itr = std::find(_handlers.begin(), _handlers.end(), p);
                        if(itr != _handlers.end())
                        {
                            _handlers.erase(itr);
                            delete p;
                        }
                        else
                        {
                            assert(false);
                        }
                    }

                    cookie_type operator += (const event_handler_type& pHandler)
                    {
                        return AddHandler(pHandler);
                    }

                    void operator -= (cookie_type cookie)
                    {
                        RemoveHandler(cookie);
                    }

                    void Clear()
                    {
                        if(!_handlers.empty())
                        {
                            int n = _handlers.size();
                            std::for_each(_handlers.begin(), _handlers.end(), [](event_handler_type* p)
                            { 
                                assert(p != nullptr);
                                delete p;
                            });
                            _handlers.clear();        
                        }
                    }

                private:
                    std::vector<event_handler_type*> _handlers;
                };

            //Common

            然后我們就可以這樣使用了:
            // EventTest.cpp : Defines the entry point for the console application.
            //

            #include "stdafx.h"

            #include <iostream>
            #include "event2.h"

            using namespace std;

            class CObjectX 
            {

            };

            class CClickEventArgs: public CObjectX
            {

            };


            class CButton: public CObjectX
            {
            public:
                void FireClick()
                {
                    CClickEventArgs args;
                    OnClicked(this, args);
                }

                typedef Common::CEventHandler<int, CObjectX*, CClickEventArgs&> ButtonClickEventHandler;
                Common::CEvent<ButtonClickEventHandler> OnClicked;
            };


            class CMyClass 
            {
            public:
                int OnBtuttonClicked(CObjectX* pButton, CClickEventArgs& args)
                {
                    cout << "CMyClass: Receive button clicked event" << endl;
                    return 1;
                }
            };

            int OnBtuttonClicked_C_fun(CObjectX* pButton, CClickEventArgs& args)
            {
                cout << "C Style Function: Receive button clicked event" << endl;
                return 1;
            }


            class CMyFunObj
            {
            public:
                int operator()(CObjectX* pButton, CClickEventArgs& args)
                {
                    cout << "Functor: Receive button clicked event" << endl;
                    return 1;    
                }
            };

            int _tmain(int argc, _TCHAR* argv[])
            {
                CButton btn;

                CMyClass obj;
                Common::cookie_type c1 = btn.OnClicked += CButton::ButtonClickEventHandler(&obj, &CMyClass::OnBtuttonClicked);

                Common::cookie_type c2 = btn.OnClicked += CButton::ButtonClickEventHandler(OnBtuttonClicked_C_fun);

                CMyFunObj functor;
                Common::cookie_type c3 = btn.OnClicked += CButton::ButtonClickEventHandler(functor);

                btn.FireClick();

                btn.OnClicked -= c2;

                std::cout << endl;

                btn.FireClick();

                system("pause");

                return 0;
            }

            怎么樣,是不是感覺和C#一樣了 !


            最后,我們比較一下2種實現方式:
            第一種方法把委托函數類型封裝起來了,對外部來說是透明的, 使用起來更簡單。
            第二種方式把委托函數的類型暴露了出來, 適用于事件處理函數類型各異,比較強調事件處理函數類型的場合。

            其實對于C++來說,個人覺得還是第一種方式更合理, 不知道大家怎么看?

             

             注: 如果你不想用C++11的新特性或是你手頭的編譯器不支持C++11, 下面是一種不借助function和bind的實現方式,

                直接用下面的CEventHandler替代上面的就可以了。

             一種不借助C++11 function和bind的實現方式

            template<typename TR, typename T1, typename T2>
                class CEventHandler
                {
                public:
                    typedef TR return_type;
                    typedef T1 first_type;
                    typedef T2 second_type;

                private:
                    class CFunctionBase 
                    {
                    public:
                        virtual return_type Invoke(first_type p1, second_type p2) = 0;
                        virtual CFunctionBase* Clone() = 0;
                        virtual ~CFunctionBase() { }
                    };

                    template<typename TFunType>
                    class CFunction: public CFunctionBase
                    {
                    public:
                        CFunction(const TFunType& f): _f(f) { }
                        virtual return_type Invoke(first_type p1, second_type p2)
                        {
                            return _f(p1, p2);
                        }
                        virtual CFunctionBase* Clone()
                        {
                            return new CFunction(this->_f);
                        }

                    private:
                        TFunType _f;
                    };

                    template<typename TClassPtr, typename TMemFunType>
                    class CClassMemFun: public CFunctionBase
                    {
                    public:
                        CClassMemFun(TClassPtr pObj, const TMemFunType& f): _pObj(pObj), _pMemFun(f) { }
                        virtual return_type Invoke(first_type p1, second_type p2)
                        {
                            return ((*_pObj).*_pMemFun)(p1, p2);
                        }
                        virtual CFunctionBase* Clone()
                        {
                            return new CClassMemFun(this->_pObj, this->_pMemFun);
                        }

                    private:
                        TClassPtr _pObj;
                        TMemFunType _pMemFun;
                    };

                public:
                    template<typename TFunType>
                    CEventHandler(const TFunType& f): _pFun(new CFunction<TFunType>(f)) { }

                    template<typename TClassPtr, typename TMemFunType>
                    CEventHandler(TClassPtr pObj, TMemFunType pFun): _pFun(new CClassMemFun<TClassPtr, TMemFunType>(pObj, pFun)) { }

                    CEventHandler(const CEventHandler& f)
                    {
                        _pFun = f._pFun->Clone(); 
                    }

                    ~CEventHandler()
                    {
                        delete _pFun;
                    }

                    CEventHandler& operator = (const CEventHandler& f)
                    {
                        if(this != &f)
                        {
                            delete _pFun;
                            _pFun = f._pFun->Clone(); 
                        }

                        return *this;
                    }

                    return_type operator()(first_type p1, second_type p2)
                    {
                        return_type ret = return_type();
                        if(_pFun != nullptr)
                        {
                            _pFun->Invoke(p1, p2);
                        }
                        else
                        {
                            assert(false);
                        }

                        return ret;
                    }

                private:
                    CFunctionBase* _pFun;

             

            posted on 2013-01-31 15:39 Richard Wei 閱讀(3431) 評論(1)  編輯 收藏 引用 所屬分類: C++

            FeedBack:
            # re: 在C++中實現事件(委托)(續)
            2013-03-04 16:26 | tb
            嗯 比較詳細 。。  回復  更多評論
              
            久久精品aⅴ无码中文字字幕不卡 久久精品aⅴ无码中文字字幕重口 | 综合久久精品色| 久久精品国产亚洲7777| 日日狠狠久久偷偷色综合免费| 一级A毛片免费观看久久精品| 一本色道久久88精品综合| 精品人妻久久久久久888| 久久精品二区| 国产成人久久激情91| 久久婷婷人人澡人人| 亚洲中文字幕无码久久精品1| 久久精品免费观看| 亚洲精品国精品久久99热一| 国产成人香蕉久久久久| 人妻无码αv中文字幕久久琪琪布| 久久精品国产一区| 蜜臀av性久久久久蜜臀aⅴ麻豆| 狠狠久久综合| 国产精品一久久香蕉国产线看观看| 国产成人综合久久精品尤物| 天天躁日日躁狠狠久久| 99久久精品国产一区二区| 久久婷婷五月综合97色一本一本| 美女久久久久久| 青青草原综合久久大伊人精品| 色妞色综合久久夜夜| 久久久精品国产免大香伊| 国产成人精品久久亚洲高清不卡 | 久久er99热精品一区二区| 99久久国产精品免费一区二区| 伊人久久成人成综合网222| 久久噜噜久久久精品66| 国产91久久综合| 国产精品女同一区二区久久| 久久精品国产99国产精品澳门| 久久国产精品99精品国产| 久久ww精品w免费人成| 久久久噜噜噜久久熟女AA片| 亚洲AV乱码久久精品蜜桃| 久久夜色精品国产噜噜亚洲AV | 亚洲日韩欧美一区久久久久我|