Skip to content

Commit

Permalink
STYLE: Use std::unique_ptr for GradientImageFilter::m_BoundaryCondition
Browse files Browse the repository at this point in the history
Following C++ Core Guidelines, February 15, 2024, "Use `unique_ptr` or
`shared_ptr` to avoid forgetting to `delete` objects created using `new`",
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rh-smart

Defaulted the destructor of `GradientImageFilter`.
  • Loading branch information
N-Dekker authored and hjmjohnson committed Mar 17, 2024
1 parent c0ebd8e commit db095cd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "itkImageToImageFilter.h"
#include "itkCovariantVector.h"
#include "itkImageRegionIterator.h"
#include "itkZeroFluxNeumannBoundaryCondition.h"
#include <memory> // For unique_ptr.

namespace itk
{
Expand Down Expand Up @@ -163,7 +165,7 @@ class ITK_TEMPLATE_EXPORT GradientImageFilter : public ImageToImageFilter<TInput

protected:
GradientImageFilter();
~GradientImageFilter() override;
~GradientImageFilter() override = default;
void
PrintSelf(std::ostream & os, Indent indent) const override;

Expand Down Expand Up @@ -226,7 +228,9 @@ class ITK_TEMPLATE_EXPORT GradientImageFilter : public ImageToImageFilter<TInput
bool m_UseImageDirection{ true };

// allow setting the the m_BoundaryCondition
ImageBoundaryCondition<TInputImage, TInputImage> * m_BoundaryCondition{};
std::unique_ptr<ImageBoundaryCondition<TInputImage, TInputImage>> m_BoundaryCondition{
std::make_unique<ZeroFluxNeumannBoundaryCondition<TInputImage>>()
};
};
} // end namespace itk

Expand Down
15 changes: 3 additions & 12 deletions Modules/Filtering/ImageGradient/include/itkGradientImageFilter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,16 @@ namespace itk
template <typename TInputImage, typename TOperatorValueType, typename TOutputValueType, typename TOutputImageType>
GradientImageFilter<TInputImage, TOperatorValueType, TOutputValueType, TOutputImageType>::GradientImageFilter()
{
// default boundary condition
m_BoundaryCondition = new ZeroFluxNeumannBoundaryCondition<TInputImage>();
this->DynamicMultiThreadingOn();
this->ThreaderUpdateProgressOff();
}

template <typename TInputImage, typename TOperatorValueType, typename TOutputValueType, typename TOutputImageType>
GradientImageFilter<TInputImage, TOperatorValueType, TOutputValueType, TOutputImageType>::~GradientImageFilter()
{
delete m_BoundaryCondition;
}

template <typename TInputImage, typename TOperatorValueType, typename TOutputValue, typename TOutputImage>
void
GradientImageFilter<TInputImage, TOperatorValueType, TOutputValue, TOutputImage>::OverrideBoundaryCondition(
ImageBoundaryCondition<TInputImage> * boundaryCondition)
{
delete m_BoundaryCondition;
m_BoundaryCondition = boundaryCondition;
m_BoundaryCondition.reset(boundaryCondition);
}

template <typename TInputImage, typename TOperatorValueType, typename TOutputValueType, typename TOutputImageType>
Expand Down Expand Up @@ -165,7 +156,7 @@ GradientImageFilter<TInputImage, TOperatorValueType, TOutputValueType, TOutputIm
{
nit = ConstNeighborhoodIterator<InputImageType>(radius, inputImage, face);
ImageRegionIterator<OutputImageType> it(outputImage, face);
nit.OverrideBoundaryCondition(m_BoundaryCondition);
nit.OverrideBoundaryCondition(m_BoundaryCondition.get());
nit.GoToBegin();

while (!nit.IsAtEnd())
Expand Down Expand Up @@ -220,7 +211,7 @@ GradientImageFilter<TInputImage, TOperatorValueType, TOutputValueType, TOutputIm
os << indent << "BoundaryCondition: ";
if (m_BoundaryCondition != nullptr)
{
os << m_BoundaryCondition << std::endl;
os << m_BoundaryCondition.get() << std::endl;
}
else
{
Expand Down

0 comments on commit db095cd

Please sign in to comment.