Workspace 6.21.5
Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
ObservableEventType< T > Class Template Reference

All events must be a subclass of this template. More...

#include <Workspace/DataExecution/Events/observableevent.h>

Inheritance diagram for ObservableEventType< T >:
[legend]

Public Member Functions

CSIRO_WORKSPACE_API const EventIDSingleton< CSIRO::DataExecution::AllObservableEvents > & eventID ()
 
CSIRO_WORKSPACE_API const EventIDSingleton< CSIRO::DataExecution::ProgressEvent > & eventID ()
 
const EventIDSingleton< T > & getEventID () const override
 
- Public Member Functions inherited from ObservableEvent
virtual ~ObservableEvent ()=default
 
virtual const EventIDgetEventID () const =0
 

Static Public Member Functions

static const EventIDSingleton< T > & eventID ()
 

Protected Member Functions

 ObservableEventType ()=default
 

Detailed Description

template<typename T>
class CSIRO::DataExecution::ObservableEventType< T >
Parameters
TThis 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 MyEventType : public ObservableEventType\<MyEventType> {};
class MySender : public Observable
{
//...
};
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

Constructor & Destructor Documentation

◆ ObservableEventType()

ObservableEventType ( )
protecteddefault

Member Function Documentation

◆ eventID() [1/3]

static const EventIDSingleton< T > & eventID ( )
static
Returns
The EventID singleton associated with the event type T. Note that the return type is more specialized than the type returned by the getEventID() function. This is what allows the templated ObserverSet::add() functions to work out what the real event type is and match it to the receiving function's parameter type at compile type. Most client code that creates an observer on an event will rely on this feature.

◆ eventID() [2/3]

◆ eventID() [3/3]

◆ getEventID()

const EventIDSingleton< T > & getEventID ( ) const
inlineoverridevirtual
Returns
The event ID object for the event type associated with the subclass.

The ObservableEventType subclass implements this function to return an EventID object that is unique to the subclass, but which remains constant for the lifetime of the application.

Implements ObservableEvent.