Medical Imaging Interaction Toolkit  2024.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 ()
 
 ITKEventObserverGuard (const itk::Object *sender, unsigned long observerTag)
 
 ITKEventObserverGuard (const itk::Object *sender, const itk::EventObject &event, itk::Command *command)
 
 ITKEventObserverGuard (const itk::Object *sender, const itk::EventObject &event, std::function< void(const itk::EventObject &)> function)
 
 ITKEventObserverGuard (ITKEventObserverGuard &&)
 
ITKEventObserverGuardoperator= (ITKEventObserverGuard &&)
 
 ~ITKEventObserverGuard ()
 
void Reset ()
 
void Reset (const itk::Object *sender, unsigned long observerTag)
 
void Reset (const itk::Object *sender, const itk::EventObject &event, itk::Command *command)
 
void Reset (const itk::Object *sender, const itk::EventObject &event, std::function< void(const itk::EventObject &)> function)
 
bool IsInitialized () const
 

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 form 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 resetted (by Reset() or operator = )

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

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

Remarks
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 55 of file mitkITKEventObserverGuard.h.

Constructor & Destructor Documentation

◆ ITKEventObserverGuard() [1/5]

mitk::ITKEventObserverGuard::ITKEventObserverGuard ( )

◆ ITKEventObserverGuard() [2/5]

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

◆ ITKEventObserverGuard() [3/5]

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

◆ ITKEventObserverGuard() [4/5]

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

◆ ITKEventObserverGuard() [5/5]

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

◆ ~ITKEventObserverGuard()

mitk::ITKEventObserverGuard::~ITKEventObserverGuard ( )

Member Function Documentation

◆ IsInitialized()

bool mitk::ITKEventObserverGuard::IsInitialized ( ) const

◆ operator=()

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

◆ Reset() [1/4]

void mitk::ITKEventObserverGuard::Reset ( )

Resets the guard by removing the currently guarded observer. After the reset the guard is uninitialized.

Remarks
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 
)

Resets the guard by first removing the currently guarded observer. Then a observer will be added for the passed sender with the passed event and command. The new observer is now guarded.

◆ Reset() [3/4]

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

Resets the guard by first removing the currently guarded observer. Then a observer will be added for the passed sender with the passed event and lambda function. The new observer is now guarded.

◆ Reset() [4/4]

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

Resets the guard by first removing the currently guarded observer. Then the passed observer tag for the passed sender will be guarded.


The documentation for this class was generated from the following file:
mitk::ITKEventObserverGuard::ITKEventObserverGuard
ITKEventObserverGuard()