• <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>
            隨筆-60  評(píng)論-111  文章-0  trackbacks-0
            參考了CPPblog上一位同學(xué)的文章 原文在這里

            基本是照搬了代碼 但是不知道是作者沒有在VC71中測(cè)試 還是發(fā)出來的代碼還是有問題的 我在VC71中編譯還是有點(diǎn)點(diǎn)小小的曲折

            下面是我自己根據(jù)VC71修改的!

              1class CEvent;
              2
              3class CFunImpl  
              4{
              5public:
              6    CFunImpl() {}
              7    virtual ~CFunImpl() {}
              8    virtual void operator()(CEvent& e) = 0;
              9    virtual bool operator== (const CFunImpl& fun) = 0;
             10    virtual CFunImpl* Clone() = 0;
             11}
            ;
             12
             13template <typename ClassName>
             14class CMemberFunc : public CFunImpl
             15{
             16public:
             17
             18    typedef CMemberFunc<ClassName> this_type;
             19    typedef void (ClassName::*fEventHandler) ( CEvent & e );
             20
             21    CMemberFunc( ClassName& obj, fEventHandler impl ) : m_Object(obj), m_pImpl(impl) {}
             22
             23    void operator ()( CEvent & e ) 
             24    
             25        if ( m_pImpl!=NULL ) (m_Object.*(m_pImpl))( e );
             26    }
             
             27
             28    CFunImpl* Clone()
             29    {
             30        return new this_type(m_Object, m_pImpl);
             31    }

             32
             33    bool operator== (const CFunImpl& fun)
             34    {
             35        if ( typeid(*this== typeid(fun) )
             36        {
             37            const this_type& rFun = dynamic_cast<const this_type& >(fun);
             38
             39            return (&m_Object == &rFun.m_Object && m_pImpl == rFun.m_pImpl);
             40        }

             41
             42        return false;
             43    }

             44
             45    virtual ~CMemberFunc()
             46    {
             47    }

             48protected:
             49    ClassName& m_Object;
             50    fEventHandler m_pImpl;
             51}
            ;
             52
             53class CStaticFunc : public CFunImpl
             54{
             55public:
             56    typedef void (*fEventHandler) ( CEvent & e );
             57
             58    CStaticFunc( fEventHandler impl ) : m_pImpl(impl) {}
             59
             60    void operator ()( CEvent & e ) 
             61    
             62        if ( m_pImpl!=NULL ) m_pImpl( e );
             63    }
             
             64
             65    CFunImpl* Clone()
             66    {
             67        return new CStaticFunc(m_pImpl);
             68    }

             69
             70    bool operator== (const CFunImpl& fun)
             71    {
             72        if ( typeid(*this== typeid(fun) )
             73        {
             74            const CStaticFunc& rFun = dynamic_cast<const CStaticFunc& >(fun);
             75
             76            return (m_pImpl == rFun.m_pImpl);
             77        }

             78
             79        return false;
             80    }

             81
             82    virtual ~CStaticFunc()
             83    {
             84    }

             85protected:
             86    fEventHandler m_pImpl;
             87}
            ;
             88
             89class CEventHandler
             90{
             91private:
             92    void Clear(){if(m_pImpl){delete m_pImpl ;m_pImpl = NULL ;}} 
             93    CFunImpl* m_pImpl;
             94public:
             95    ~CEventHandler()
             96    {
             97        Clear();
             98    }

             99    template<typename ClassName>
            100    CEventHandler( ClassName& obj, void (ClassName::*impl)(CEvent&) ) : m_pImpl( new CMemberFunc<ClassName>(obj,impl) ) {}
            101    CEventHandler( void(*impl)(CEvent&) ) : m_pImpl( new CStaticFunc(impl) ) {}
            102    CEventHandler( const CEventHandler&fun ) : m_pImpl( NULL ) *this=fun; }
            103    void operator() ( CEvent & e ) 
            104    
            105        (*m_pImpl)(e);
            106    }

            107    CEventHandler& operator= ( const CEventHandler& fun )
            108    {
            109        Clear();
            110        if (fun.m_pImpl) m_pImpl = fun.m_pImpl->Clone();
            111        return *this;
            112    }

            113    bool operator== ( const CEventHandler & handler ) 
            114    
            115        if ( m_pImpl==NULL || handler.m_pImpl==NULL ) return true ;
            116        if ( typeid(*m_pImpl) == typeid(*(handler.m_pImpl)) ) 
            117        
            118            return (*m_pImpl) == (*(handler.m_pImpl)); 
            119        }
             
            120        return false ; 
            121    }
             
            122}
            ;
            123
            124class CEvent
            125{
            126private:
            127    std::list<CEventHandler>m_Funcs;
            128    void Register( CEventHandler handle ) 
            129    {
            130        m_Funcs.push_back(handle);
            131    }

            132    void UnRegister( const CEventHandler & handler ) 
            133    
            134        m_Funcs.remove(handler); 
            135    }

            136    void* lpData;
            137public:
            138    void* GetPointer() return lpData; }
            139    CEvent & operator<< ( const CEventHandler & handler ) 
            140    
            141        Register ( handler ); 
            142        return * this ; 
            143    }
             
            144
            145    CEvent & operator>> ( const CEventHandler & handler ) 
            146    
            147        UnRegister ( handler ); 
            148        return * this ; 
            149    }
             
            150
            151    void operator ( )( void* pData=NULL ) 
            152    {
            153        lpData=pData;
            154        for(std::list<CEventHandler>::iterator pos=m_Funcs.begin(); pos!=m_Funcs.end(); ++pos )
            155            (*pos)(*this);
            156    }

            157}
            ;
            158

            編譯的時(shí)候要打開RTTI!
            posted on 2006-09-04 12:27 shaker(太子) 閱讀(528) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C++
            久久精品国产99久久无毒不卡 | 久久久久亚洲AV综合波多野结衣| 久久久综合九色合综国产| 久久久久四虎国产精品| 久久人人爽人爽人人爽av| 久久无码人妻一区二区三区午夜| 国产精品久久久久9999| 亚洲精品无码久久久久AV麻豆| 久久久久99精品成人片试看| 国产激情久久久久影院老熟女| 狠狠色丁香久久婷婷综合蜜芽五月 | 久久婷婷色综合一区二区| 亚洲午夜久久久久久噜噜噜| 国产精品美女久久久久AV福利| 久久婷婷色香五月综合激情| 精品久久久久久久久中文字幕| 亚洲欧美日韩久久精品| 国产成人精品久久亚洲高清不卡| 一本久久a久久精品亚洲| 久久无码AV中文出轨人妻 | 国内精品伊人久久久影院 | 色欲av伊人久久大香线蕉影院 | 久久精品亚洲一区二区三区浴池| 久久久噜噜噜久久| 青青热久久综合网伊人| 久久精品人人做人人妻人人玩| 久久亚洲中文字幕精品一区| 少妇被又大又粗又爽毛片久久黑人 | 国产精品成人99久久久久 | 国产99久久久国产精免费| 久久久久亚洲av无码专区导航 | 天天爽天天狠久久久综合麻豆| 亚洲精品乱码久久久久久不卡| 久久国产福利免费| 国产精品成人久久久久久久| 青青热久久综合网伊人| 2021国产成人精品久久| 国产精品成人精品久久久| 国产精自产拍久久久久久蜜| 国产精品美女久久久网AV| 天天综合久久久网|