Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
mitk::ITKEventObserverGuard Class Reference

Convenience class that helps to manage the lifetime of itk event observers. More...

#include <mitkITKEventObserverGuard.h>

Public Member Functions

 ITKEventObserverGuard ()
 Construct an uninitialized guard (no observer is guarded). More...
 
 ITKEventObserverGuard (const itk::Object *sender, unsigned long observerTag)
 Construct a guard for an existing observer tag. More...
 
 ITKEventObserverGuard (const itk::Object *sender, const itk::EventObject &event, itk::Command *command)
 Construct a guard that adds and manages an observer using an itk::Command. More...
 
 ITKEventObserverGuard (const itk::Object *sender, const itk::EventObject &event, std::function< void(const itk::EventObject &)> function)
 Construct a guard that adds and manages an observer using a lambda or function. More...
 
 ITKEventObserverGuard (ITKEventObserverGuard &&)
 Move constructor. Transfers ownership of the guarded observer. More...
 
ITKEventObserverGuard & operator= (ITKEventObserverGuard &&)
 Move assignment. Releases any currently guarded observer and takes ownership from the other guard. More...
 
 ~ITKEventObserverGuard ()
 Destructor. Removes the guarded observer from the sender if still alive. More...
 
void Reset ()
 Reset the guard by removing the currently guarded observer. More...
 
void Reset (const itk::Object *sender, unsigned long observerTag)
 Reset the guard and start guarding a new observer tag. More...
 
void Reset (const itk::Object *sender, const itk::EventObject &event, itk::Command *command)
 Reset the guard and add a new observer using an itk::Command. More...
 
void Reset (const itk::Object *sender, const itk::EventObject &event, std::function< void(const itk::EventObject &)> function)
 Reset the guard and add a new observer using a lambda or function. More...
 
bool IsInitialized () const
 Check whether this guard is currently managing an observer. More...
 

Detailed Description

Convenience class that helps to manage the lifetime of itk event observers.

This helper class can be used to ensure itk event observers are removed from a sender object at the end of a certain scope. This class behaves similar to a std::unique_ptr but for event observers. Therefore the observer will be removed from the sender when one of the following conditions are met:

  • the guard is destroyed
  • the guard is reset (by Reset() or move assignment)

Sample usage:

{
auto &objRef = *o.GetPointer();
auto guard = ITKEventObserverGuard(o, itk::AnyEvent(), [&objRef](const itk::EventObject &event)
{ std::cout << "Object: " << objRef.GetNameOfClass() << " Event: " << event << std::endl; });
//some code
}
//now the guard is destroyed
ITKEventObserverGuard()
Construct an uninitialized guard (no observer is guarded).

This will add an observer to o executing the lambda for any event as long as the guard exists.

Note
If the sender is already destroyed at the moment when the guard wants to remove the observer, the removal will be skipped.

Definition at line 57 of file mitkITKEventObserverGuard.h.

Constructor & Destructor Documentation

◆ ITKEventObserverGuard() [1/5]

mitk::ITKEventObserverGuard::ITKEventObserverGuard ( )

Construct an uninitialized guard (no observer is guarded).

◆ ITKEventObserverGuard() [2/5]

mitk::ITKEventObserverGuard::ITKEventObserverGuard ( const itk::Object *  sender,
unsigned long  observerTag 
)

Construct a guard for an existing observer tag.

Parameters
senderThe ITK object that owns the observer.
observerTagThe tag returned by itk::Object::AddObserver().

◆ ITKEventObserverGuard() [3/5]

mitk::ITKEventObserverGuard::ITKEventObserverGuard ( const itk::Object *  sender,
const itk::EventObject &  event,
itk::Command *  command 
)

Construct a guard that adds and manages an observer using an itk::Command.

Parameters
senderThe ITK object to observe.
eventThe event type to observe.
commandThe command to execute when the event occurs.

◆ ITKEventObserverGuard() [4/5]

mitk::ITKEventObserverGuard::ITKEventObserverGuard ( const itk::Object *  sender,
const itk::EventObject &  event,
std::function< void(const itk::EventObject &)>  function 
)

Construct a guard that adds and manages an observer using a lambda or function.

Parameters
senderThe ITK object to observe.
eventThe event type to observe.
functionThe function to execute when the event occurs.

◆ ITKEventObserverGuard() [5/5]

mitk::ITKEventObserverGuard::ITKEventObserverGuard ( ITKEventObserverGuard &&  )

Move constructor. Transfers ownership of the guarded observer.

◆ ~ITKEventObserverGuard()

mitk::ITKEventObserverGuard::~ITKEventObserverGuard ( )

Destructor. Removes the guarded observer from the sender if still alive.

Member Function Documentation

◆ IsInitialized()

bool mitk::ITKEventObserverGuard::IsInitialized ( ) const

Check whether this guard is currently managing an observer.

Returns
true if the guard is initialized and managing an observer, false otherwise.

◆ operator=()

ITKEventObserverGuard& mitk::ITKEventObserverGuard::operator= ( ITKEventObserverGuard &&  )

Move assignment. Releases any currently guarded observer and takes ownership from the other guard.

◆ Reset() [1/4]

void mitk::ITKEventObserverGuard::Reset ( )

Reset the guard by removing the currently guarded observer.

After the reset, the guard is uninitialized.

Note
Resetting an uninitialized guard has no effect.

◆ Reset() [2/4]

void mitk::ITKEventObserverGuard::Reset ( const itk::Object *  sender,
const itk::EventObject &  event,
itk::Command *  command 
)

Reset the guard and add a new observer using an itk::Command.

First removes the currently guarded observer, then adds a new observer for the passed sender with the passed event and command. The new observer is now guarded.

Parameters
senderThe ITK object to observe.
eventThe event type to observe.
commandThe command to execute when the event occurs.

◆ Reset() [3/4]

void mitk::ITKEventObserverGuard::Reset ( const itk::Object *  sender,
const itk::EventObject &  event,
std::function< void(const itk::EventObject &)>  function 
)

Reset the guard and add a new observer using a lambda or function.

First removes the currently guarded observer, then adds a new observer for the passed sender with the passed event and function. The new observer is now guarded.

Parameters
senderThe ITK object to observe.
eventThe event type to observe.
functionThe function to execute when the event occurs.

◆ Reset() [4/4]

void mitk::ITKEventObserverGuard::Reset ( const itk::Object *  sender,
unsigned long  observerTag 
)

Reset the guard and start guarding a new observer tag.

First removes the currently guarded observer, then guards the passed observer tag for the passed sender.

Parameters
senderThe ITK object that owns the observer.
observerTagThe tag returned by itk::Object::AddObserver().

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