Skip to content

Commit

Permalink
STYLE: Use unique_ptr for m_CostFunctionAdaptor members in Optimizers
Browse files Browse the repository at this point in the history
Defaulted the corresponding Optimizer destructors.

Following C++ Core Guidelines, Oct 3, 2024, "Use `unique_ptr` or `shared_ptr`
to avoid forgetting to `delete` objects created using `new`",
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rh-smart
  • Loading branch information
N-Dekker authored and dzenanz committed Nov 4, 2024
1 parent dae9bef commit 17270fd
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "itkCommand.h"
#include "ITKOptimizersExport.h"

#include <memory> // For unique_ptr.

namespace itk
{
/** \class MultipleValuedNonLinearVnlOptimizer
Expand Down Expand Up @@ -126,8 +128,8 @@ class ITKOptimizers_EXPORT MultipleValuedNonLinearVnlOptimizer : public Multiple
void
IterationReport(const EventObject & event);

CostFunctionAdaptorType * m_CostFunctionAdaptor{};
bool m_UseGradient{};
std::unique_ptr<CostFunctionAdaptorType> m_CostFunctionAdaptor;
bool m_UseGradient{};

CommandType::Pointer m_Command{};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "itkCommand.h"
#include "ITKOptimizersExport.h"

#include <memory> // For unique_ptr.

namespace itk
{
/** \class SingleValuedNonLinearVnlOptimizer
Expand Down Expand Up @@ -139,7 +141,7 @@ class ITKOptimizers_EXPORT SingleValuedNonLinearVnlOptimizer : public SingleValu
void
IterationReport(const EventObject & event);

CostFunctionAdaptorType * m_CostFunctionAdaptor{};
std::unique_ptr<CostFunctionAdaptorType> m_CostFunctionAdaptor;

bool m_Maximize{};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,17 @@ MultipleValuedNonLinearVnlOptimizer::MultipleValuedNonLinearVnlOptimizer()
/**
* Destructor
*/
MultipleValuedNonLinearVnlOptimizer::~MultipleValuedNonLinearVnlOptimizer()
{
delete m_CostFunctionAdaptor;
m_CostFunctionAdaptor = nullptr;
}
MultipleValuedNonLinearVnlOptimizer::~MultipleValuedNonLinearVnlOptimizer() = default;

void
MultipleValuedNonLinearVnlOptimizer::SetCostFunctionAdaptor(CostFunctionAdaptorType * adaptor)
{
if (m_CostFunctionAdaptor == adaptor)
if (m_CostFunctionAdaptor.get() == adaptor)
{
return;
}

delete m_CostFunctionAdaptor;

m_CostFunctionAdaptor = adaptor;
m_CostFunctionAdaptor.reset(adaptor);

this->SetUseCostFunctionGradient(m_UseGradient);

Expand All @@ -63,21 +57,21 @@ MultipleValuedNonLinearVnlOptimizer::SetCostFunctionAdaptor(CostFunctionAdaptorT
const MultipleValuedNonLinearVnlOptimizer::CostFunctionAdaptorType *
MultipleValuedNonLinearVnlOptimizer::GetCostFunctionAdaptor() const
{
return m_CostFunctionAdaptor;
return m_CostFunctionAdaptor.get();
}

MultipleValuedNonLinearVnlOptimizer::CostFunctionAdaptorType *
MultipleValuedNonLinearVnlOptimizer::GetCostFunctionAdaptor()
{
return m_CostFunctionAdaptor;
return m_CostFunctionAdaptor.get();
}

/** The purpose of this method is to get around the lack of const
* correctness in vnl cost_functions and optimizers */
MultipleValuedNonLinearVnlOptimizer::CostFunctionAdaptorType *
MultipleValuedNonLinearVnlOptimizer::GetNonConstCostFunctionAdaptor() const
{
return m_CostFunctionAdaptor;
return m_CostFunctionAdaptor.get();
}

void
Expand Down Expand Up @@ -134,6 +128,6 @@ MultipleValuedNonLinearVnlOptimizer::PrintSelf(std::ostream & os, Indent indent)
os << indent << "Cached Derivative: " << m_CachedDerivative << std::endl;
os << indent << "Cached current positiion: " << m_CachedCurrentPosition << std::endl;
os << indent << "Command observer " << m_Command.GetPointer() << std::endl;
os << indent << "Cost Function adaptor" << m_CostFunctionAdaptor << std::endl;
os << indent << "Cost Function adaptor" << m_CostFunctionAdaptor.get() << std::endl;
}
} // end namespace itk
Original file line number Diff line number Diff line change
Expand Up @@ -33,45 +33,39 @@ SingleValuedNonLinearVnlOptimizer::SingleValuedNonLinearVnlOptimizer()
}

/** Destructor */
SingleValuedNonLinearVnlOptimizer::~SingleValuedNonLinearVnlOptimizer()
{
delete m_CostFunctionAdaptor;
m_CostFunctionAdaptor = nullptr;
}
SingleValuedNonLinearVnlOptimizer::~SingleValuedNonLinearVnlOptimizer() = default;

void
SingleValuedNonLinearVnlOptimizer::SetCostFunctionAdaptor(CostFunctionAdaptorType * adaptor)
{
if (m_CostFunctionAdaptor == adaptor)
if (m_CostFunctionAdaptor.get() == adaptor)
{
return;
}

delete m_CostFunctionAdaptor;

m_CostFunctionAdaptor = adaptor;
m_CostFunctionAdaptor.reset(adaptor);

m_CostFunctionAdaptor->AddObserver(IterationEvent(), m_Command);
}

const SingleValuedNonLinearVnlOptimizer::CostFunctionAdaptorType *
SingleValuedNonLinearVnlOptimizer::GetCostFunctionAdaptor() const
{
return m_CostFunctionAdaptor;
return m_CostFunctionAdaptor.get();
}

SingleValuedNonLinearVnlOptimizer::CostFunctionAdaptorType *
SingleValuedNonLinearVnlOptimizer::GetCostFunctionAdaptor()
{
return m_CostFunctionAdaptor;
return m_CostFunctionAdaptor.get();
}

/** The purpose of this method is to get around the lack of
* const-correctness in VNL cost-functions and optimizers */
SingleValuedNonLinearVnlOptimizer::CostFunctionAdaptorType *
SingleValuedNonLinearVnlOptimizer::GetNonConstCostFunctionAdaptor() const
{
return m_CostFunctionAdaptor;
return m_CostFunctionAdaptor.get();
}

/** The purpose of this method is to get around the lack of iteration reporting
Expand Down Expand Up @@ -103,6 +97,6 @@ SingleValuedNonLinearVnlOptimizer::PrintSelf(std::ostream & os, Indent indent) c
os << indent << "Cached Derivative: " << m_CachedDerivative << std::endl;
os << indent << "Cached current positiion: " << m_CachedCurrentPosition << std::endl;
os << indent << "Command observer " << m_Command.GetPointer() << std::endl;
os << indent << "Cost Function adaptor" << m_CostFunctionAdaptor << std::endl;
os << indent << "Cost Function adaptor" << m_CostFunctionAdaptor.get() << std::endl;
}
} // end namespace itk
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "itkSingleValuedVnlCostFunctionAdaptorv4.h"
#include "itkCommand.h"

#include <memory> // For unique_ptr.

namespace itk
{
/**
Expand Down Expand Up @@ -124,7 +126,7 @@ class ITKOptimizersv4_EXPORT SingleValuedNonLinearVnlOptimizerv4 : public Object
void
IterationReport(const EventObject & event);

CostFunctionAdaptorType * m_CostFunctionAdaptor{};
std::unique_ptr<CostFunctionAdaptorType> m_CostFunctionAdaptor;

CommandType::Pointer m_Command{};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,7 @@ SingleValuedNonLinearVnlOptimizerv4::SingleValuedNonLinearVnlOptimizerv4()
this->m_CachedDerivative.Fill(DerivativeType::ValueType{});
}

SingleValuedNonLinearVnlOptimizerv4::~SingleValuedNonLinearVnlOptimizerv4()
{
if (this->m_CostFunctionAdaptor)
{
delete this->m_CostFunctionAdaptor;
this->m_CostFunctionAdaptor = nullptr;
}
}
SingleValuedNonLinearVnlOptimizerv4::~SingleValuedNonLinearVnlOptimizerv4() = default;

void
SingleValuedNonLinearVnlOptimizerv4::StartOptimization(bool doOnlyInitialization)
Expand Down Expand Up @@ -67,37 +60,32 @@ SingleValuedNonLinearVnlOptimizerv4::StartOptimization(bool doOnlyInitialization
void
SingleValuedNonLinearVnlOptimizerv4::SetCostFunctionAdaptor(CostFunctionAdaptorType * adaptor)
{
if (this->m_CostFunctionAdaptor == adaptor)
if (this->m_CostFunctionAdaptor.get() == adaptor)
{
return;
}

if (this->m_CostFunctionAdaptor)
{
delete this->m_CostFunctionAdaptor;
}

this->m_CostFunctionAdaptor = adaptor;
this->m_CostFunctionAdaptor.reset(adaptor);

this->m_CostFunctionAdaptor->AddObserver(IterationEvent(), this->m_Command);
}

const SingleValuedNonLinearVnlOptimizerv4::CostFunctionAdaptorType *
SingleValuedNonLinearVnlOptimizerv4::GetCostFunctionAdaptor() const
{
return this->m_CostFunctionAdaptor;
return this->m_CostFunctionAdaptor.get();
}

SingleValuedNonLinearVnlOptimizerv4::CostFunctionAdaptorType *
SingleValuedNonLinearVnlOptimizerv4::GetCostFunctionAdaptor()
{
return this->m_CostFunctionAdaptor;
return this->m_CostFunctionAdaptor.get();
}

SingleValuedNonLinearVnlOptimizerv4::CostFunctionAdaptorType *
SingleValuedNonLinearVnlOptimizerv4::GetNonConstCostFunctionAdaptor() const
{
return this->m_CostFunctionAdaptor;
return this->m_CostFunctionAdaptor.get();
}

void
Expand All @@ -118,6 +106,6 @@ SingleValuedNonLinearVnlOptimizerv4::PrintSelf(std::ostream & os, Indent indent)
os << indent << "Cached Derivative: " << this->m_CachedDerivative << std::endl;
os << indent << "Cached current positiion: " << this->m_CachedCurrentPosition << std::endl;
os << indent << "Command observer " << this->m_Command.GetPointer() << std::endl;
os << indent << "Cost Function adaptor" << this->m_CostFunctionAdaptor << std::endl;
os << indent << "Cost Function adaptor" << this->m_CostFunctionAdaptor.get() << std::endl;
}
} // end namespace itk

0 comments on commit 17270fd

Please sign in to comment.