Skip to content

Commit

Permalink
ENH: allow calling RemoveObserver() from a const itkObject
Browse files Browse the repository at this point in the history
  • Loading branch information
tm9k1 authored and N-Dekker committed May 22, 2024
1 parent 163984c commit b7067a6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Modules/Core/Common/include/itkObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class ITKCommon_EXPORT Object : public LightObject

/** Remove the observer with this tag value. */
void
RemoveObserver(unsigned long tag);
RemoveObserver(unsigned long tag) const;

/** Remove all observers . */
void
Expand Down
2 changes: 1 addition & 1 deletion Modules/Core/Common/src/itkObject.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ Object::GetCommand(unsigned long tag)
}

void
Object::RemoveObserver(unsigned long tag)
Object::RemoveObserver(unsigned long tag) const
{
if (this->m_SubjectImplementation)
{
Expand Down
21 changes: 16 additions & 5 deletions Modules/Filtering/ImageGrid/test/itkBasicArchitectureTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "itkShrinkImageFilter.h"
#include "itkCommand.h"
#include "itkTextOutput.h"
#include "itkTestingMacros.h"

// The following three classes are used to support callbacks
// on the shrink filter in the pipeline that follows later
Expand Down Expand Up @@ -142,30 +143,40 @@ itkBasicArchitectureTest(int, char *[])
command = itk::SimpleMemberCommand<ShowProgressObject>::New();
command->SetCallbackFunction(&progressWatch, &ShowProgressObject::ShowProgress);
shrink->AddObserver(itk::ProgressEvent(), command);
ITK_TEST_EXPECT_TRUE(shrink->HasObserver(itk::ProgressEvent()));

// Create a command to call StartEndEvent when start event is triggered
StartEndEvent startEndWatch;
itk::SimpleMemberCommand<StartEndEvent>::Pointer start;
start = itk::SimpleMemberCommand<StartEndEvent>::New();
start->SetCallbackFunction(&startEndWatch, &StartEndEvent::Start);
unsigned long tag = shrink->AddObserver(itk::StartEvent(), start);
shrink->AddObserver(itk::StartEvent(), start);
ITK_TEST_EXPECT_TRUE(shrink->HasObserver(itk::StartEvent()));

// Create a command to call StartEndEvent when end event is triggered
itk::SimpleMemberCommand<StartEndEvent>::Pointer end;
end = itk::SimpleMemberCommand<StartEndEvent>::New();
end->SetCallbackFunction(&startEndWatch, &StartEndEvent::End);
shrink->AddObserver(itk::EndEvent(), end);
unsigned long endEventObserverTag = shrink->AddObserver(itk::EndEvent(), end);
ITK_TEST_EXPECT_TRUE(shrink->HasObserver(itk::EndEvent()));

// Create a command that to call AnyEvent when event is fired
AllEvents allWatch;
itk::MemberCommand<AllEvents>::Pointer allEvents;
allEvents = itk::MemberCommand<AllEvents>::New();
allEvents->SetCallbackFunction(&allWatch, &AllEvents::WatchEvents);
shrink->AddObserver(itk::AnyEvent(), allEvents);
unsigned long anyEventObserverTag = shrink->AddObserver(itk::AnyEvent(), allEvents);
ITK_TEST_EXPECT_TRUE(shrink->HasObserver(itk::AnyEvent()));
shrink->Update();

// test RemoveObserver code
shrink->RemoveObserver(tag);
// Test RemoveObserver code
shrink->RemoveObserver(anyEventObserverTag);
ITK_TEST_EXPECT_TRUE(!shrink->HasObserver(itk::AnyEvent()));

// Test RemoveObserver const code
const auto & constShrink = *shrink;
constShrink.RemoveObserver(endEventObserverTag);
ITK_TEST_EXPECT_TRUE(!constShrink.HasObserver(itk::EndEvent()));

return EXIT_SUCCESS;
}

0 comments on commit b7067a6

Please sign in to comment.