C++中实现C#的delegate机制

peimoxu 2007-12-29 05:16:54
由于这段时间较忙,自从上次我发布了一个供初学者学习的“MFC”程序后,就没怎么写文章了。
这段时间在忙新的项目,项目逻辑还算复杂,用了好多有闪光点的技术,下面我要讨论的是闪光点之一:在C++中实现C#的delegate机制。

用过C#的朋友都觉得C#的确是个好语言,但有时由于特殊需要,不得不用C++,在C++中要想实现C#中的Thread等也是可行的,
不过代码要稍复杂。

以例子来说明:

class Sdk
{
public:
void DoSomething();
};

class client
{
public:
Sdk m_sdk;
void DoSomething{}
{
m_sdk.DoSomething();
}

void OnMessage()
{
//
}
}

这个例子比较简单,Sdk用来实现某个功能,client是客户程序,要想在m_sdk做处理的时候能发送通知到client,有几个方法可以实现,
其一是发线程消息,其一是回调,但传统的回调对面向对象的支持并不好,这就是实现delegate的出发点。
下面是一个实现:
class Delegate
{
public:
virtual ~Delegate(void){ }
virtual void operator()(void) = 0;
};
template<typename C>
class DelegateImpl : public Delegate
{
private:
typedef void (C::*F)(void);
C* m_class;
F m_fun;
public:
DelegateImpl(C* c, F f){ m_class = c; m_fun = f; }
virtual void operator()(void)
{
if(m_class)
return (m_class->*m_fun)();
}
};
有了上面代码,再稍加修改sdk和client代码就能实现事件通知了:
class Sdk
{
public:
Delegate* pEvent;
sdk(): pEvent(NULL) { }
void DoSomething()
{
cout<<"opened"<<endl;
if(pEvent != NULL)
(*pEvent)( );
}
};

class client
{
private:
Sdk m_sdk;

public:
client()
{
m_sdk.pEvent = new Delegate<client>(this, OnOpen);
}
void DoSomething()
{
m_sdk.DoSomething();
}

void OnDoSomething()
{
cout<<"event fired"<<endl;
}
}

上面的实现有不少局限性,事件处理方法的返回值和参数设置不是十分灵活,因此可以用宏定义优代以上代码,
下面给出我的全部代码,请朋友指正。

//////////////////////////////////////////////////////////////////////////
//delegate 0
#define DEFINE_DELEGATE(NAME, R)\
class NAME##Delegate\
{\
public:\
virtual ~NAME##Delegate(void){ }\
virtual R operator()(void) = 0;\
};\
template<typename C>\
class NAME##DelegateImpl : public NAME##Delegate\
{\
private:\
typedef R (C::*F)(void);\
C* m_class;\
F m_fun;\
public:\
NAME##DelegateImpl(C* c, F f){ m_class = c; m_fun = f; }\
virtual R operator()(void)\
{\
if(m_class)\
return (m_class->*m_fun)();\
}\
};\
template<typename C, typename F>\
NAME##Delegate* Make##NAME##Delegate(C* c, F f)\
{\
return new NAME##DelegateImpl<C>(c, f);\
}

//////////////////////////////////////////////////////////////////////////
//delegate 1
#define DEFINE_DELEGATE(NAME, R, P1)\
class NAME##Delegate\
{\
public:\
virtual ~NAME##Delegate(void){ }\
virtual R operator()(P1 p1) = 0;\
};\
template<typename C>\
class NAME##DelegateImpl : public NAME##Delegate\
{\
private:\
typedef R (C::*F)(P1);\
C* m_class;\
F m_fun;\
public:\
NAME##DelegateImpl(C* c, F f){ m_class = c; m_fun = f; }\
virtual R operator()(P1 p1)\
{\
if(m_class)\
return (m_class->*m_fun)(p1);\
}\
};\
template<typename C, typename F>\
NAME##Delegate* Make##NAME##Delegate(C* c, F f)\
{\
return new NAME##DelegateImpl<C>(c, f);\
}

//////////////////////////////////////////////////////////////////////////
//delegate 2
#define DEFINE_DELEGATE(NAME, R, P1, P2)\
class NAME##Delegate\
{\
public:\
virtual ~NAME##Delegate(void){ }\
virtual R operator()(P1 p1, P2 p2) = 0;\
};\
template<typename C>\
class NAME##DelegateImpl : public NAME##Delegate\
{\
private:\
typedef R (C::*F)(P1, P2);\
C* m_class;\
F m_fun;\
public:\
NAME##DelegateImpl(C* c, F f){ m_class = c; m_fun = f; }\
virtual R operator()(P1 p1, P2 p2)\
{\
if(m_class)\
return (m_class->*m_fun)(p1, p2);\
}\
};\
template<typename C, typename F>\
NAME##Delegate* Make##NAME##Delegate(C* c, F f)\
{\
return new NAME##DelegateImpl<C>(c, f);\
}





/*
//////////////////////////////////////////////////////////////////////////
//sample

DEFINE_DELEGATE(Open, void, int, string)

class sdk
{
public:
OpenDelegate* pEvent;
sdk(): pEvent(NULL) { }
void Open()
{
cout<<"opened"<<endl;
if(pEvent != NULL)
(*pEvent)(100, "你好");
}
};

class client
{
private:
sdk m_sdk;

public:
client()
{
m_sdk.pEvent = MakeOpenDelegate(this, OnOpen2);
}
void Open()
{
m_sdk.Open();
}

void OnOpen()
{
cout<<"Open event fired"<<endl;
}
void OnOpen2(int t, string str)
{
cout<<"Open event fired, Param = "<<t<<"param2 = "<<str.c_str()<<endl;
}
};
*/






...全文
350 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
rotApple 2008-04-24
  • 打赏
  • 举报
回复
very good!
flyingwow99 2008-01-04
  • 打赏
  • 举报
回复
good work
peimoxu 2008-01-02
  • 打赏
  • 举报
回复
Delegate0<void> *p = MakeDelegate(&test, Test::test);
(*p)();
Delegate1<void, int> *p2 = MakeDelegate(&test, Test::test2);
(*p2)(100);
Delegate3<void, int, int ,int> *p3 = MakeDelegate(&test, Test::test4);
(*p3)(100, 55,55);
peimoxu 2008-01-02
  • 打赏
  • 举报
回复
我的最代码已确定:

//////////////////////////////////////////////////////////////////////////
//delegate0
template<typename R>
class Delegate0
{
public:
virtual ~Delegate0(void){ }
virtual R operator()(void) = 0;
};
template<typename C, typename R>
class Delegate0Impl : public Delegate0<R>
{
private:
typedef R (C::*F)(void);
C* m_class;
F m_fun;
public:
Delegate0Impl(C* c, F f){ m_class = c; m_fun = f; }
virtual R operator()(void)
{
if(m_class)
return (m_class->*m_fun)();
}
};
template<typename C, typename R>
Delegate0<R>* MakeDelegate(C* c, R (C::*f)(void))
{
return new Delegate0Impl<C, R>(c, f);
}

//////////////////////////////////////////////////////////////////////////
//delegate1
template<typename R, typename P1>
class Delegate1
{
public:
virtual ~Delegate1(void){ }
virtual R operator()(P1 p1) = 0;
};
template<typename C, typename R, typename P1>
class Delegate1Impl : public Delegate1<R, P1>
{
private:
typedef R (C::*F)(P1);
C* m_class;
F m_fun;
public:
Delegate1Impl(C* c, F f){ m_class = c; m_fun = f; }
virtual R operator()(P1 p1)
{
if(m_class)
return (m_class->*m_fun)(p1);
}
};
template<typename C, typename R, typename P1>
Delegate1<R, P1>* MakeDelegate(C* c, R (C::*f)(P1))
{
return new Delegate1Impl<C, R, P1>(c, f);
}

//////////////////////////////////////////////////////////////////////////
//delegate2
template<typename R, typename P1, typename P2>
class Delegate2
{
public:
virtual ~Delegate2(void){ }
virtual R operator()(P1 p1, P2 p2) = 0;
};
template<typename C, typename R, typename P1, typename P2>
class Delegate2Impl : public Delegate2<R, P1, P2>
{
private:
typedef R (C::*F)(P1, P2);
C* m_class;
F m_fun;
public:
Delegate2Impl(C* c, F f){ m_class = c; m_fun = f; }
virtual R operator()(P1 p1, P2 p2)
{
if(m_class)
return (m_class->*m_fun)(p1, P2);
}
};
template<typename C, typename R, typename P1, typename P2>
Delegate2<R, P1, P2>* MakeDelegate(C* c, R (C::*f)(P1, P2))
{
return new Delegate2Impl<C, R, P1, P2>(c, f);
}
peimoxu 2007-12-30
  • 打赏
  • 举报
回复
多谢楼上支持。
我们代码的唯一区别在申明事件参数及返回值类型的位置不同,
我的是在定义变量之前:
DEFINE_DELEGATE(Open, void, int, string)
OpenDelegate* pEvent;
而你是在定义变量之时:
BinaryDelegate<void, int, std::string>* event_;

至于delete event_
这个我当然知道,搞C++的倒不可能把这个忘了的
Vitin 2007-12-29
  • 打赏
  • 举报
回复
另外Sdk类需要增加一个析构函数,毕竟这不是C#:

Sdk::~Sdk()
{
delete event_;
}
Vitin 2007-12-29
  • 打赏
  • 举报
回复
支持楼主。

不过不需要使用宏,用模版就可以了。以双参数为例

#include <iostream>
#include <string>

//////////////////////////////////////////////////////////////////////////
//delegate 2
template <typename result_type, typename argument_type1, typename argument_type2>
class BinaryDelegate
{
public:
virtual ~BinaryDelegate(void) {}
virtual result_type operator()(argument_type1 arg1, argument_type2 arg2) = 0;
};

template <typename concrete_type, typename result_type, typename argument_type1, typename argument_type2>
class BinaryDelegateImpl : public BinaryDelegate<result_type, argument_type1, argument_type2>
{
typedef result_type (concrete_type::*func)(argument_type1, argument_type2);
concrete_type* c_;
func func_;
public:
BinaryDelegateImpl(concrete_type* c, func f) : c_(c), func_(f) {}
virtual result_type operator()(argument_type1 arg1, argument_type2 arg2)
{
if (c_ != 0)
return (c_->*func_)(arg1, arg2);
}
};

template <typename concrete_type, typename result_type, typename argument_type1, typename argument_type2>
BinaryDelegate<result_type, argument_type1, argument_type2>*
make_binary_delegate(concrete_type* c, result_type (concrete_type::* f)(argument_type1, argument_type2))
{
return new BinaryDelegateImpl<concrete_type, result_type, argument_type1, argument_type2>(c, f);
}

//////////////////////////////////////////////////////////////////////////
//sdk
class Sdk
{
public:
BinaryDelegate<void, int, std::string>* event_;
// 请注意,因为在本类中会调用event_,设计者必须知道其原型,因此在此处指定函数返回值和参数类型是合适的

Sdk(): event_(0) {}
void open()
{
std::cout << "opened" << std::endl;
if (event_ != 0)
(*event_)(100, "你好");
}
};

class Client
{
private:
Sdk sdk_;

public:
Client()
{
sdk_.event_ = make_binary_delegate(this, &Client::on_open2);
}
void open()
{
sdk_.open();
}
void on_open()
{
std::cout << "Open event fired" << std::endl;
}
void on_open2(int t, std::string str)
{
std::cout << "Open event fired, Argument = " << t << "Argument2 = " << str << std::endl;
}
};

int main()
{
Client client;
}

peimoxu 2007-12-29
  • 打赏
  • 举报
回复
本程序需VS2003及以上版本

64,647

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧