Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitk::Message< A > Class Template Reference

Event/message/notification class. More...

#include <mitkMessage.h>

Inheritance diagram for mitk::Message< A >:
Collaboration diagram for mitk::Message< A >:

Public Types

typedef MessageBase< MessageAbstractDelegate< A > > Super
 
typedef Super::ListenerList ListenerList
 
- Public Types inherited from mitk::MessageBase< MessageAbstractDelegate< A > >
typedef std::vector< MessageAbstractDelegate< A > * > ListenerList
 

Public Member Functions

void Send ()
 
void operator() ()
 
- Public Member Functions inherited from mitk::MessageBase< MessageAbstractDelegate< A > >
virtual ~MessageBase ()
 
 MessageBase ()
 
 MessageBase (const MessageBase &o)
 
MessageBaseoperator= (const MessageBase &o)
 
void AddListener (const MessageAbstractDelegate< A > &delegate) const
 
void operator+= (const MessageAbstractDelegate< A > &delegate) const
 
void RemoveListener (const MessageAbstractDelegate< A > &delegate) const
 
void operator-= (const MessageAbstractDelegate< A > &delegate) const
 
const ListenerListGetListeners () const
 
bool HasListeners () const
 
bool IsEmpty () const
 

Additional Inherited Members

- Protected Attributes inherited from mitk::MessageBase< MessageAbstractDelegate< A > >
ListenerList m_Listeners
 List of listeners. More...
 
itk::SimpleFastMutexLock m_Mutex
 

Detailed Description

template<typename A = void>
class mitk::Message< A >

Event/message/notification class.

See also
mitk::BinaryThresholdTool
QmitkBinaryThresholdToolGUI

This totally ITK, Qt, VTK, whatever toolkit independent class allows one class to send out messages and another class to receive these message. This class is templated over the return type (A) of the callback functions. There are variations of this class (Message1, Message2, etc.) for sending one, two or more parameters along with the messages.

This is an implementation of the Observer pattern.

  • There is no guarantee about the order of which observer is notified first. At the moment the observers which register first will be notified first.
  • Notifications are synchronous, by direct method calls. There is no support for asynchronous messages.

To conveniently add methods for registering/unregistering observers to Message variables of your class, you can use the mitkNewMessageMacro macros.

Here is an example how to use the macros and templates:

// An object to be send around
class Law
{
private:
std::string m_Description;
public:
Law(const std::string law) : m_Description(law)
{ }
std::string GetDescription() const
{
return m_Description;
}
};
// The NewtonMachine will issue specific events
class NewtonMachine
{
mitkNewMessageMacro(AnalysisStarted);
mitkNewMessage1Macro(AnalysisStopped, bool);
mitkNewMessage1Macro(LawDiscovered, const Law&);
public:
void StartAnalysis()
{
// send the "started" signal
m_AnalysisStartedMessage();
// we found a new law of nature by creating one :-)
Law massLaw("F=ma");
m_LawDiscoveredMessage(massLaw);
}
void StopAnalysis()
{
// send the "stop" message with false, indicating
// that no error occured
m_AnalysisStoppedMessage(false);
}
};
class Observer
{
private:
NewtonMachine* m_Machine;
public:
Observer(NewtonMachine* machine) : m_Machine(machine)
{
// Add "observers", i.e. function pointers to the machine
m_Machine->AddAnalysisStartedListener(
::mitk::MessageDelegate<Observer>(this, &Observer::MachineStarted));
m_Machine->AddAnalysisStoppedListener(
::mitk::MessageDelegate1<Observer, bool>(this, &Observer::MachineStopped));
m_Machine->AddLawDiscoveredListener(
::mitk::MessageDelegate1<Observer, const Law&>(this, &Observer::LawDiscovered));
}
~Observer()
{
// Always remove your observers when finished
m_Machine->RemoveAnalysisStartedListener(
::mitk::MessagDelegate<Observer>(this, &Observer::MachineStarted));
m_Machine->RemoveAnalysisStoppedListener(
::mitk::MessageDelegate1<Observer, bool>(this, &Observer::MachineStopped));
m_Machine->RemoveLawDiscoveredListener(
::mitk::MessageDelegate1<Observer, const Law&>(this, &Observer::LawDiscovered));
}
void MachineStarted()
{
std::cout << "Observed machine has started" << std::endl;
}
void MachineStopped(bool error)
{
std::cout << "Observed machine stopped " << (error ? "with an error" : "") << std::endl;
}
void LawDiscovered(const Law& law)
{
std::cout << "New law of nature discovered: " << law.GetDescription() << std::endl;
}
};
NewtonMachine newtonMachine;
Observer observer(&newtonMachine);
// This will send two events to registered observers
newtonMachine.StartAnalysis();
// This will send one event to registered observers
newtonMachine.StopAnalysis();

Another example of how to use these message classes can be found in the directory Testing, file mitkMessageTest.cpp

Definition at line 572 of file mitkMessage.h.

Member Typedef Documentation

template<typename A = void>
typedef Super::ListenerList mitk::Message< A >::ListenerList

Definition at line 576 of file mitkMessage.h.

template<typename A = void>
typedef MessageBase<MessageAbstractDelegate<A> > mitk::Message< A >::Super

Definition at line 575 of file mitkMessage.h.

Member Function Documentation

template<typename A = void>
void mitk::Message< A >::operator() ( )
inline

Definition at line 595 of file mitkMessage.h.

References mitk::Message< A >::Send().


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