CFugue
Classes | Public Member Functions | Friends | List of all members
OIL::CEventT< TEventSource, TEventHandlerArgs > Class Template Reference

#include <EventHandler.h>

Public Member Functions

template<typename TEventReceiver >
void Subscribe (TEventReceiver *pReceiver, void(TEventReceiver::*lpfnHandler)(TEventSource *, TEventHandlerArgs *))
 
void Subscribe (void(*lpfnHandler)(TEventSource *, TEventHandlerArgs *))
 
void UnSubscribe (void(*lpfnHandler)(TEventSource *, TEventHandlerArgs *))
 
template<typename TReceiverClass >
void UnSubscribe (TReceiverClass *pReceiver)
 
void UnSubscribeAll ()
 
size_t SubscriberCount () const
 

Friends

class CEventSource
 

Detailed Description

template<typename TEventSource = const CEventSource, typename TEventHandlerArgs = CEventHandlerArgs>
class OIL::CEventT< TEventSource, TEventHandlerArgs >

A Receiver can Subscribe for an Event only once. If a Receiver requests a second subscription for the same Event, its previous subscription would be replaced with the new subscription.

A Receiver can subscribe for as many different events as it wants. But for each, only once.

And an Event can have as many Receivers as it likes.

CEventT take template arguments that indicate the prototype of the handler proc that should used to subscribe to that event. By default, CEvent uses the handlers of the form

void EventReceptionFunc(const CEventSource* pSrc, CEventHandlerArgs* pArgs)

You can use different prototypes by simply defining the sender and handler args in the template definition for CEventT.

For example, CEventT<Sender, HandlerArgs> expects its subscriber functions to be of the form

void EventReceptionFunc(Sender* pSrc, HandlerArgs* pArgs)

Following Snippet demonstrates sample usage.

<pre>
\#include "EventHandler.h"
using namespace OIL;
struct MyEventHandlerArgs : public CEventHandlerArgs
{
int m_nVal;
};
class MySender : public CEventSource
{
public:
CEvent m_Event; // Default handler prototype
CEventT < const MySender, MyEventHandlerArgs > m_Event2; // Custom handler prototype
void MyFunction()
{
RaiseEvent(&m_Event, &CEventHandlerArgs()); // Invoke the Event
RaiseEvent(&m_Event2, &MyEventHandlerArgs()); // Invoke the Event with custom prototype
}
};
class MyReceiver: public CEventReceiver
{
MySender* m_pMySenderObject;
public:
MyReceiver(MySender* pSender) : m_pMySenderObject(pSender) // Subscribe for the Events
{
m_pMySenderObject->m_Event.Subscribe(this, & MyReceiver::EventReceptionFunc);
m_pMySenderObject->m_Event2.Subscribe(this, & MyReceiver::MyEventReceptionFunc2);
}
void EventReceptionFunc(const CEventSource* pSrc, CEventHandlerArgs* pArgs)
{
printf("\n Event Received\n"); // Code for Handling the Event
}
void MyEventReceptionFunc2(const MySender* pSrc, const MyEventHandlerArgs* pArgs)
{
printf("\nEvent2 Received"); // Code for Handling the Event
}
};
void main()
{
MySender senderObj;
MyReceiver receiverObj(senderObj);
senderObj.MyFunction(); // Do something on Sender that will Raise the Events to be received on Receiver
}
</pre>

Summary> Default form of CEventT that uses

void EventReceptionFunc(const CEventSource* pSrc, CEventHandlerArgs* pArgs)

as the Event handler procedure /Summary>

Definition at line 133 of file EventHandler.h.

Member Function Documentation

template<typename TEventSource = const CEventSource, typename TEventHandlerArgs = CEventHandlerArgs>
template<typename TEventReceiver >
void OIL::CEventT< TEventSource, TEventHandlerArgs >::Subscribe ( TEventReceiver *  pReceiver,
void(TEventReceiver::*)(TEventSource *, TEventHandlerArgs *)  lpfnHandler 
)

Creates an IEventFunctor object for the given function and subcribes it to be called upon the given object whenever the event is raised. Duplicates or Multiple Subscriptions are not allowed. If the Receiver object already has a subscription for this event, the old subscription is removed before adding the new subcription.

Definition at line 224 of file EventHandler.h.

template<typename TEventSource = const CEventSource, typename TEventHandlerArgs = CEventHandlerArgs>
void OIL::CEventT< TEventSource, TEventHandlerArgs >::Subscribe ( void(*)(TEventSource *, TEventHandlerArgs *)  lpfnHandler)

Subscribes the given Function to be called whenever the event is raised. Duplicates are not Allowed. If the given Function is already a subscriber, it would not be added again.

Definition at line 236 of file EventHandler.h.

template<typename TEventSource = const CEventSource, typename TEventHandlerArgs = CEventHandlerArgs>
size_t OIL::CEventT< TEventSource, TEventHandlerArgs >::SubscriberCount ( ) const

Gives the number of active Subcriptions for this event. It includes the Class-based Subscriptions as well the Function-based ones.

Definition at line 281 of file EventHandler.h.

template<typename TEventSource = const CEventSource, typename TEventHandlerArgs = CEventHandlerArgs>
void OIL::CEventT< TEventSource, TEventHandlerArgs >::UnSubscribe ( void(*)(TEventSource *, TEventHandlerArgs *)  lpfnHandler)

UnSubscribes the given Function from being called. Nothing Happens if the given Function is not a Subscriber.

Definition at line 248 of file EventHandler.h.

template<typename TEventSource = const CEventSource, typename TEventHandlerArgs = CEventHandlerArgs>
template<typename TReceiverClass >
void OIL::CEventT< TEventSource, TEventHandlerArgs >::UnSubscribe ( TReceiverClass *  pReceiver)

UnSubscribes the pReceiver from the Subscription. Nothing Happens if the supplied pReceiver object is not a Subscriber.

Definition at line 261 of file EventHandler.h.

template<typename TEventSource = const CEventSource, typename TEventHandlerArgs = CEventHandlerArgs>
void OIL::CEventT< TEventSource, TEventHandlerArgs >::UnSubscribeAll ( )

UnSubscribes all receivers. SubscriberCount() would become Zero.

Definition at line 269 of file EventHandler.h.


The documentation for this class was generated from the following file:

CFugue, the C++ Music Programming Library © Copyright 2009 Cenacle Research India Private Limited Gopalakrishna Palem