共享:发放一个C++的对象事件实现

bomdy 2006-04-19 04:48:51

原来用Delphi比较多,觉得Delphi的对象事件机制用起来非常方便,能够很容易地在两个无关联对象间建立事件通知关系。

但在C++中我一直没找着类似的方法,于是自己写了一个事件类,不敢独享,发之,愿有助于君。

本打算用模板,但无法在模板中实现可变的事件参数,只得用宏来实现,如有哪位大虾能提供用模板解决的方法,不胜感激。

这个事件类使用方法上与Delphi的事件很相似:
1、定义事件类型
2、声明事件变量
3、给事件变量赋值
4、调用事件


/***************************************************************
ClassEvent.h: interface for the CLASSEVENT macro.

Zhao.H.
zhaoh23@21cn.com
2005/10/27

Testing passed in VC++6.0

CLASSEVENT( RET_TYPE, NAME, ARG_LIST )
RET_TYPE The datatype of event return value
NAME Specify the name of the defined event class
such as: MyEvent
ARG_LIST Arguments list of event handler. ARG_LIST must
be define as format:
(CObject* obj, int nCmd, int nData)
if no arguments, define ARG_LIST as ()

This macro define a class and a type of function pointer.
The class is named with the gived NAME appended with a letter
C as the first letter. This class support class event with class
member function. The defined class can be use to declare event
object in classes, functions and so on. The event object can be
assigned with a member function of another object, soon later,
this member function can be invoke with the event object.
The type of function pointer is named with the gived NAME.
This type is a pointer to member function of class. This type
should be used when assign a member function of class as an event
handler to an event object. The member function need be converted
with type NAME when call the Assign() method.
***************************************************************/

#ifndef _CLASSEVENT_H_
#define _CLASSEVENT_H_


/* ASM code used to jmp_call the event handler (m_obj->*m_handler)() */
#define CLASSEVENT_ASM_CALL_HANDLE \
_asm \
{ \
/* store m_obj to [ebp-4] */\
_asm mov eax,dword ptr [ecx+0] \
_asm mov dword ptr [ebp-4], eax \
/* store m_handler to [ebp-8] */\
_asm mov eax,dword ptr [ecx+4] \
_asm mov dword ptr [ebp-8], eax \
/* restore registers */\
_asm pop edi \
_asm pop esi \
_asm pop ebx \
_asm mov esp,ebp \
_asm pop ebp \
/* mov m_obj to ecx for call function of (*m_obj) */\
_asm mov ecx, dword ptr [esp-8] \
/* jmp to the entry of event handler and will return from the handler direct */\
_asm jmp dword ptr [esp-12] \
}

#define CLASSEVENT(RET_TYPE, NAME, ARG_LIST) \
\
typedef RET_TYPE (CObject::*NAME)ARG_LIST; \
\
class C##NAME \
{\
public: \
/* constructor functions */\
C##NAME() { m_obj=NULL; m_handler=NULL; }; \
C##NAME(CObject& obj, NAME func) { m_obj=&obj; m_handler=func; }; \
C##NAME(CObject* obj, NAME func) { m_obj=obj; m_handler=func; }; \
/* event handler assign methods */\
void Assign(CObject& obj, NAME func) { m_obj=&obj; m_handler=func; }; \
void Assign(CObject* obj, NAME func) { m_obj=obj; m_handler=func; }; \
BOOL Valid() { return m_obj&&m_handler; }; \
/* assigne operator = */\
C##NAME& operator =(C##NAME& Event) \
{\
m_obj=Event.m_obj; \
m_handler=Event.m_handler; \
return (*this); \
}; \
/* method to trigger the event handler */\
RET_TYPE operator () ARG_LIST \
{ \
CLASSEVENT_ASM_CALL_HANDLE;\
RET_TYPE* ret;\
return *ret;\
}; \
RET_TYPE Exec ARG_LIST \
{ \
CLASSEVENT_ASM_CALL_HANDLE;\
RET_TYPE* ret;\
return *ret;\
}; \
private: \
friend C##NAME; \
CObject* m_obj; \
NAME m_handler; \
}

#endif // ifndef _CLASSEVENT_H_


...全文
61 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
bomdy 2006-04-19
  • 打赏
  • 举报
回复

Example:
// -----------------------------------------------------
// declare a event class CEventA with handler:
// int (class::*)(int EventData)

CLASSEVENT( int, EventA, (int EventData) );

class ClassA
{
public:
CEventA OnTestEvent;
void Test() { if(OnTestEvent.Valid()) OnTestEvent(1234); };
};

class ClassB: public CObject
{
public:
int SaveData;
int EventHandler(int Data)
{
char buf[8];
MessageBox(NULL, itoa(Data, buf, 10), NULL, MB_OK);
return SaveData=Data;
};
};

void test()
{
ClassA obj_a;
ClassB obj_b;
obj_a.OnTestEvent.Assign(obj_b, (EventA)obj_b.EventHandler );
obj_a.Test();
}

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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