Chaining Event Routers
The
Implementation of CComboRouterListener
template <class base_t>
class CComboRouterListener : public base_t
{
public:
virtual bool HandleEvent(IEvent* pIEvent)
{
bool bHandled = false;
if (pIEvent != NULL)
{
bHandled =
pIEvent->Dispatch(guid_cast<IEventRouter*>(this));
}
if (!bHandled)
bHandled = base_t::RouteEvent(pIEvent);
return bHandled;
}
};
The following code shows how you can use CComboRouterListener to create an object that is both an event router and an event listener. The CFoobarBase class inherits IEventRouterImpl and mixes in the IEventListener interface. CFoobarBase is an abstract base class because it does not implement the HandleEvent() method inherited from IEventListener. Wrapping CFoobarBase with the CComboRouterListener template class implements HandleEvent(). As a result, instances of CFoobar can be added as listeners to other event routers. They can also route events to listeners.
class CFoobarBase : public IEventRouterImpl, public IEventListener
{
};
typedef CComboRouterListener<CFoobarBase> CFoobar;