template<typename T>
class CSIRO::DataExecution::ObservableEventType< T >
- Parameters
-
T | This must be the type of the class deriving from ObservableEventType. While specifying the deriving class on the base class template like this may seem unusual to some, it is actually a well known and used pattern called CRTP (the "Curiously Recurring Template Pattern"). |
This template class takes care of a number of things which make defining new events and creating observers for them relatively straightforward. The first responsibility of this class is to implement the virtual getEventID() function declared in the ObservableEvent base class. An interesting feature of this is that it changes the return type to a subclass of EventID which is specific to the template type T. This allows code used by the templated ObserverSet::add() member functions to automatically detect what the event type is by calling getEventID(). The usual pattern for creating an observable event and adding it to an ObserverSet all in one call is something like this:
{
};
class MyReceiver
{
void handleMyEventType(const MyEventType& e);
};
MySender sender;
MyReceiver receiver;
ObserverSet observers;
observers.add(sender, MyEventType::eventID(), receiver, &MyReceiver::handleMyEventType);
All events must be a subclass of this template.
Definition: observableevent.h:188
Base class for all observable classes.
Definition: observable.h:51
When you define your own new event type by subclassing ObserverableEventType, you also need to use the DECLARE_WORKSPACE_EVENT_INSTANCE and DEFINE_WORKSPACE_EVENT_INSTANCE macros (they were omitted from the above code example for brevity). The DECLARE_... macro should follow your event class definition, usually at the bottom of the same header file in which it is defined. The DEFINE_... macro should be at the top of the file in which your plugin's setup() function is implemented.
- See also
- ObservableEvent::getEventID(), ObserverSet