共享:发放一个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_