This class implements event receivers for void and write events. The idea is similar to the mtsFunction classes (e.g., mtsFunctionVoid, mtsFunctionRead) – these event receivers should be added to a required interface and will be bound to the corresponding event generators in the connected provided interface. Then, it is possible for the component to specify an event handler at any time (see SetHandler method), or to wait for the event to occur (see Wait method).
Previously, it was only possible to add event handlers to a required interface (see mtsInterfaceRequired::AddEventHandlerVoid, mtsInterfaceRequired::AddEventHandlerWrite). For backward compatibility, this design continues to support direct addition of an event handler to a required interface. Thus, the following three blocks of code can all be used to set an event handler:
One thing to note about an event receiver is that, by default, it is considered required (i.e., MTS_REQUIRED) when added to a required interface, whereas event handlers are not.
There are two main reasons for the introduction of event handlers:
- They allow the component to wait for an event.
- They allow event handlers to be added at any time. Previously, if an event handler was added to a required interface AFTER that interface was connected, it was ignored (i.e., it was not added as an observer to the event generator in the provided interface).
The implementation of the event receiver class requires an osaThreadSignal to block the component when the Wait method is called. This design uses the ThreadSignal member of the required interface mailbox, which is present for any component derived from mtsTask. If the required interface does not have a mailbox, the event receiver will create its own instance of osaThreadSignal. This is done to support use of event receivers by low-level components that do not have their own thread of execution (e.g., mtsComponent), as long as the Wait method is only called from a single thread. Note that the Wait method returns a bool; a false return indicates that the Wait failed for some reason (such as being called from more than one thread). An alternate implementation would be to introduce an osaThreadSignal member to mtsComponent and use that instead of the one in the required interface mailbox.