tsc_EventDelegate

This macro is used to generate a monitor class which is used to consume events produced by tsc_Event.   While it is possible to manually write the monitor class, the macro provides a simple and concise implementation.

Note that it is possible to simply add tsc_IEventMonitor to the inhertance list for a class and use the usual SC Monitor pattern, however only one event type may be handled in this way.  Using tsc_EventDelegate allows any number of different events to be handled.  

There are four important things to do when using tsc_EventDelegate in an event-handling class:

The sample code further down this page illustrates how to do this.

Macro

tsc_EventDelegate (eventName, handlerClass);
This generates the event delegate class as well as an instance of it called eventName.  The eventName can be any name that identifies the event and must be a different name from other members of the class. The handlerClass parameter identifies the class type which will be handling the events, and this class must contain a method called OneventName, with the correct signature for an event handler.  The example below shows how to set up a complete event-handling system.

eventName.Bind (<handlerClass>* handlerInstance);
This method is a member of the generated delegate class and must initially be called to bind the delegate to the class that is using it.  Bind is typically called in the constructor of the class that defines the delegate. The handlerInstance parameter should always be this, such that the delegate may call this->OnEventName() to handle events.  Bind also starts the monitor which binds event handling to the current thread, so it is important to call bind on the correct thread.

Sample code

The event-producing class is not shown; assume that we have an instance somewhere called "AlarmClock" and it contains a public tsc_Event object called Wakeup, and that AlarmClock will raise a Wakeup event at some appropriate time by calling Wakeup.Invoke(). See tsc_Event for a more in-depth sample.


class MyEventConsumer 

    tsc_EventDelegate (Wake, MyEventConsumer);   // Define an event delegate called "Wake".


    // This method handles the Alarm Clock's Wakeup event. 

    void OnWake (void* sender, tsc_EventArgs* eargs)

    { 

        tsc_MessageBox::Show ("Hey!", "Wake up lazy bones!"); 

    }  

    

public: 

    MyEventConsumer() 

    { 

        Wake.Bind(this);            // Bind the delegate to the handler class.  

        AlarmClock.Wakeup += Wake;  // Subscribe to the event. 

    } 

    

    ~MyEventConsumer() 

    { 

        AlarmClock.Wakeup -= Wake;  // Unsubscribe when destroyed. 

    } 

};