AKever

导航

Delegate的实现与测试:

转:http://www.oschina.net/code/snippet_109684_3356

码贴:

Delegate.h

#ifndef __DELEGATE_H__
#define __DELEGATE_H__
// lrhnfs@gmail.com [2011/2/21 YMD] [11:03:54]
// 简单委托类
// 
 
namespace delegate
{
    // 委托基类
    // Arg: 目标函数参数
    // Invoke: 调用目标函数[虚函数]
    // operator(): 功能=Invoke
    template<typename Arg>
    class DelegateBase
    {
    public:
        virtual void Invoke(Arg* pArg) = 0;
        void operator()(Arg* pArg) { Invoke(pArg); };
    }; // class DelegateBase
 
 
    // function类型委托
    // m_method: 目标函数
    template<typename Arg>
    class DelegateFunction
        : public DelegateBase<Arg>
    {
    public:
        typedef void(*Method)(Arg* arg);
 
        DelegateFunction(Method method)
            : m_method(method)
        { }
 
        void Invoke(Arg* pArg) { m_method(pArg); }
 
        Method m_method;
    }; // class DelegateFunction
 
 
    // class类型委托
    // T: class类型
    // m_pObject: class对象
    template<typename Arg, typename T>
    class DelegateObject
        : public DelegateBase<Arg>
    {
    public:
        typedef void (T::*Method)(Arg* pArg);
 
        DelegateObject(T* pObject, Method method)
            : m_pObject(pObject)
            , m_method(method)
        { }
 
        void Invoke(Arg* pArg) { (m_pObject->*m_method)(pArg); }
 
        T* m_pObject;
 
        Method m_method;
    }; // class DelegateObject
} // namespace delegate
#endif // __DELEGATE_H__

DelegateTest.h

#ifndef __DELEGATE_TEST_H__
#define __DELEGATE_TEST_H__
// lrhnfs@gmail.com [2011/2/21 YMD] [11:35:36]
// Delegate(委托)测试
 
#include "delegate.h"
#include <iostream>
 
using namespace std;
using namespace delegate;
 
// 按钮测试类
class ButtonTest
{
public:
    DelegateBase<int>* m_pDelegateObject;
    DelegateBase<int>* m_pDelegateFunction;
 
    ButtonTest()
        : m_pDelegateObject(0)
        , m_pDelegateFunction(0)
    { }
 
    ~ButtonTest()
    {
        delete m_pDelegateObject; m_pDelegateObject = 0;
        delete m_pDelegateFunction; m_pDelegateFunction = 0;
    }
 
    // 运行测试
    void Run()
    {
        int testNum = 3;
 
        if (m_pDelegateObject) (*m_pDelegateObject)(&testNum);
 
        if (m_pDelegateFunction) (*m_pDelegateFunction)(&testNum);
    }
};
 
 
// function委托的测试函数
void DelegateFunctionTest(int* pT)
{
    cout << "DelegateFunctionTest OK! Arg = " << *pT << endl;
}
 
// class委托的测试函数
class DelegateObjectTest
{
public:
    void _DelegateObjectTest(int* pT)
    {
        cout << "DelegateObjectTest OK! Arg = " << *pT << endl;
    }
};
 
// 运行测试
class DelegateTest
{
public:
    static void Test()
    {
        DelegateObjectTest delegateObjectTest;
        ButtonTest buttonTest;
        buttonTest.m_pDelegateObject = new DelegateObject<int, DelegateObjectTest>(&delegateObjectTest, &DelegateObjectTest::_DelegateObjectTest);
        buttonTest.m_pDelegateFunction = new DelegateFunction<int>(DelegateFunctionTest);
        buttonTest.Run();
    }
};
#endif // __DELEGATE_TEST_H__

main.cpp

#include "main.h"
#include <stdlib.h>
#include "src\DelegateTest.h"

int main(int arg, int args[])
{
    DelegateTest *test = new DelegateTest();
    test->Test();

    system("pause");
    return 0;
}

 

operator:http://www.cnblogs.com/TS-qrt/articles/operator.html

 

---- THE END !!

 

posted on 2015-07-01 11:30  AKever  阅读(166)  评论(0)    收藏  举报