📄 eventlistener.h
字号:
/////////////////////////////////////////////////////////////////////////////
// EventListener.h : Declares a template for Event Listeners
//
#ifndef _XP_EVENTLISTENER_H__
#define _XP_EVENTLISTENER_H__
#include <vector>
#include <list>
#include "..\CppIface.h"
#include "..\Events\Event.h"
#define XP_IEVENTLISTENER_IID \
{ 0x8a189177, 0xb5e8, 0x4c7e, { 0x97, 0xfc, 0xa2, 0x2e, 0x57, 0x7, 0xe6, 0x5f } };
///////////////////////////////////////////////////////////////////////////
//@doc IEventListener
//
//@class IEventListener | Interface for receiving and handling events. The
// HandleEvent method takes a pointer to an event and returns TRUE if the
// event is handled.
//
//@base public | IRefCount
//@base public | IQueryGuid
//
class IEventListener : public IRefCount, public IQueryGuid
{
public:
//@cmember
/* Receive an event and attempt to handle it. */
virtual XPBool HandleEvent(IEvent* pIEvent) = 0;
XP_IMPL_IID(XP_IEVENTLISTENER_IID)
};
///////////////////////////////////////////////////////////////////////////
//@doc CEventListenerBase
//
//@class CEventListenerBase | This template class is provided as a convenient
// way to implement event listener interfaces in concrete classes. This
// template implements <mf CEventListenerBase::QueryInterface> and
// <mf CEventListenerBase::HandleEvent>. The event listener interface is one
// of the two template parameters, and is used as the base class in the
// template declaration. The interface passed in is assumed to be derived
// from <c IEventListener>. The other template parameter is a pointer to
// the interface ID for the event listener interface.
//
//@tcarg class | interfacebase_t | Event listener interface to implement.
//
//@mfunc HRESULT | CEventListenerBase | QueryInterface | Retrieve a pointer to
// an interface supported by this object.
//@rdesc S_OK if the interface is supported, E_NOINTERFACE if not.
//@parm REFIID | riid | Identifier of the interface being requested.
//@parm void ** | ppvObj | Address of pointer variable that receives the
// interface pointer requested in riid.
//@comm Checks for the interface ID passed in the template parameter
// list. Also checks for IEventListener and IUnknown.
//
//@mfunc XPUint32 | CEventListenerBase | AddRef | Add a reference to this object.
//@rdesc New reference count value.
//@comm The CEventListenerBase template does not implement reference counting.
// This function does nothing and returns 0. Derived classes can implement
// reference counting by overriding AddRef and Release.
//
//@mfunc XPUint32 | CEventListenerBase | Release | Release a reference to this object.
//@rdesc New reference count value.
//@comm The CEventListenerBase template does not implement reference counting.
// This function does nothing and returns 0. Derived classes can implement
// reference counting by overriding AddRef and Release.
//
//@mfunc XPBool | CEventListenerBase | HandleEvent | Receive an event and attempt
// to handle it.
//@rdesc TRUE if event is handled; otherwise FALSE.
//@parm IEvent* | pIEvent | Pointer to event object.
//@comm This function calls the function <mf IEvent::Dispatch> on
// the event passed in. The listener passes a pointer to itself to
// the event's dispatch message.
//
template<class interfacebase_t>
class CEventListenerBase : public interfacebase_t
{
public:
//@cmember
/* Retrieve a pointer to an interface supported by this object. */
virtual XPBool QueryGuid(XPREFIID guid, void **ppvObj)
{
*ppvObj = 0;
if (guid == interfacebase_t::IID())
{
*ppvObj = static_cast<interfacebase_t*>(this);
}
else if (guid == IEventListener::IID())
{
*ppvObj = static_cast<IEventListener*>(this);
}
if (*ppvObj != 0)
{
return XP_TRUE;
}
return XP_FALSE;
}
//@cmember
/* Add a reference to this object. */
virtual XPUint32 XPSTDMETHODCALLTYPE AddRef()
{
return 1;
}
//@cmember
/* Release a reference to this object. */
virtual XPUint32 XPSTDMETHODCALLTYPE Release()
{
return 1;
}
//@cmember
/* Receive an event and attempt to handle it. */
virtual XPBool HandleEvent(IEvent* pIEvent)
{
XPBool bHandled = XP_FALSE;
if (pIEvent != 0)
{
bHandled = pIEvent->Dispatch(this);
}
return bHandled;
}
};
///////////////////////////////////////////////////////////////////////////
//@type ListenerVector | STL vector of event listeners.
//
typedef std::vector<IEventListener*> ListenerVector;
///////////////////////////////////////////////////////////////////////////
//@type ListenerList | STL LIST of event listeners.
//
typedef std::list<IEventListener*> ListenerList;
#endif // #ifndef _XP_EVENTLISTENER_H__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -