C++实现的委托机制(1)(一)

2014-11-24 12:43:46 · 作者: · 浏览: 0

1.引言

下面的委托实现使用的MyGUI里面的委托实现,MyGUI是一款强大的GUI库,想理解更多的MyGUI信息,猛击这里mygui.info

最终的代码可以在这里下载:http://up.2cto.com/2011/1009/20111009023128248.rar
我们的目标是要实现一个跟.NET几乎完全一样的委托,使用简单,支持多播,可以添加删除委托。同时支持C++的普通函数、模板函数、类成员函数,类的静态成员函数,并且支持多态。使用方式如下:

copy to clipboardprint // 普通函数
void normalFunc(){ cout << "func1" << endl; }
class Base
{
public:
// 类成员函数
void classFunc(){ cout << "Base func1" << endl; }
};
int main() www.2cto.com
{
Base b;
CMultiDelegate myDelegate;
myDelegate += newDelegate(normalFunc);
myDelegate += newDelegate(&b, &Base::classFunc);
myDelegate(); // 此时会调用normalFunc和classFunc
myDelegate -= newDelegate(&b, &Base::classFunc);
myDelegate(); // 此时会调用normalFunc
return 0;
}
// 普通函数
void normalFunc(){ cout << "func1" << endl; }
class Base
{
public:
// 类成员函数
void classFunc(){ cout << "Base func1" << endl; }
};
int main()
{
Base b;
CMultiDelegate myDelegate;
myDelegate += newDelegate(normalFunc);
myDelegate += newDelegate(&b, &Base::classFunc);
myDelegate(); // 此时会调用normalFunc和classFunc
myDelegate -= newDelegate(&b, &Base::classFunc);
myDelegate(); // 此时会调用normalFunc
return 0;
}


2.实现无参函数委托

要实现委托,首先要解决的是封装C++中的函数指针。因为在C++中,普通函数指针和类成员函数指针是完全不一样的。如下例子

copy to clipboardprint class CMyClass
{
public:
void func(int);
};
// 定义一个指向CMyClass类型,参数列表为(int),返回值为void的函数指针
typedef void (CMyClass::*ClassMethod) (int); // 注意定义时使用了特殊的运算符::*
class CMyClass
{
public:
void func(int);
};
// 定义一个指向CMyClass类型,参数列表为(int),返回值为void的函数指针
typedef void (CMyClass::*ClassMethod) (int); // 注意定义时使用了特殊的运算符::*

那么此函数指针只能指向CMyClass类型的成员函数,不能指向其他类或者普通函数

类成员函数指针不能直接调用,要通过一个类实例来调用,如下

copy to clipboardprint CMyClass *object = new CMyClass;
ClassMethod method = CMyClass::func;
(object->*method)(5); // 注意调用时使用了特殊运算符->*
CMyClass *object = new CMyClass;
ClassMethod method = CMyClass::func;
(object->*method)(5); // 注意调用时使用了特殊运算符->*

那么如何封装呢?我们先来定义下接口吧

(为了简单起见,下面的实现都是以无参函数为例,后续会讲到如何支持任意参数)

copy to clipboardprint class IDelegate
{
public:
virtual ~IDelegate() { }
virtual bool isType(const std::type_info& _type) = 0;
virtual void invoke() = 0;
virtual bool compare(IDelegate *_delegate) const = 0;
};
class IDelegate
{
public:
virtual ~IDelegate() { }
virtual bool isType(const std::type_info& _type) = 0;
virtual void invoke() = 0;
virtual bool compare(IDelegate *_delegate) const = 0;
};

IDelegate类的接口很少,也很简单,必要接口只有一个,就是invoke,用于触发函数

但为了可以方便管理,使用了isType和compare函数来进行相等判断。

下面是封装的普通函数指针


copy to clipboardprint class CStaticDelegate : public IDelegate
{
public:
typedef void (*Func)();
CStaticDelegate(Func _func) : mFunc(_func) { }
virtual bool isType( const std::type_info& _type) { return typeid(CStaticDelegate) == _type; }
virtual void invoke() { mFunc(); }
virtual bool compare(IDelegate *_delegate) const
{
if (0 == _delegate || !_delegate->isType(typeid(CStaticDelegate)) ) return false;
CStaticDelegate * cast = static_cast(_delegate);
return cast->mFunc == mFunc;
}
private:
Func mFunc;
};
class CStaticDelegate : public IDelegate
{
public:
typedef void (*Func)();
CStaticDelegate(Func _func) : mFunc(_func) { }
virtual bool isType( const std::type_info& _type) { return typeid(CStaticDelegate) == _type; }
virtual void invoke() { mFunc(); }
virtual bool compare(IDelegate *_delegate) const
{
if (0 == _delegate || !_delegate->isType(typeid(CStaticDelegate)) ) return