Skip to content

Commit

Permalink
ENH: Add DataObject::UpdateSource() alternative to GetSource()->Update()
Browse files Browse the repository at this point in the history
Offers a more efficient, more concise alternative to the following common pattern:

    if (x->GetSource())
    {
      x->GetSource()->Update();
    }

Calling `DataObject::UpdateSource()` instead is more efficient, because it
internally calls `DataObject::GetSource()` just once, instead of twice.
  • Loading branch information
N-Dekker authored and hjmjohnson committed Sep 10, 2022
1 parent 0b47b7b commit 11dac9e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Modules/Core/Common/include/itkDataObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,10 @@ class ITK_FORCE_EXPORT_MACRO(ITKCommon) DataObject : public Object
DataObjectPointerArraySizeType
GetSourceOutputIndex() const;

/** Does an Update() of its source, if it has one. Does nothing, otherwise. */
void
UpdateSource() const;

/** Restore the data object to its initial state. This means releasing
* memory. */
virtual void
Expand Down
11 changes: 11 additions & 0 deletions Modules/Core/Common/src/itkDataObject.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,17 @@ DataObject::GetSourceOutputIndex() const
return m_Source->MakeIndexFromOutputName(m_SourceOutputName);
}

void
DataObject::UpdateSource() const
{
const auto source = this->GetSource();

if (source)
{
source->Update();
}
}

//----------------------------------------------------------------------------
void
DataObject::PrintSelf(std::ostream & os, Indent indent) const
Expand Down

0 comments on commit 11dac9e

Please sign in to comment.