From 126f9c5c3acf5ad78278f0ed74f3ea334038d438 Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Sat, 2 Nov 2024 12:55:29 +0100 Subject: [PATCH] STYLE: Use `unique_ptr` for m_CostFunctionAdaptor member in Optimizerv4 Defaulted the default-constructor of SingleValuedNonLinearVnlOptimizerv4. 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 --- .../itkSingleValuedNonLinearVnlOptimizerv4.h | 4 +++- ...itkSingleValuedNonLinearVnlOptimizerv4.cxx | 24 +++++-------------- 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/Modules/Numerics/Optimizersv4/include/itkSingleValuedNonLinearVnlOptimizerv4.h b/Modules/Numerics/Optimizersv4/include/itkSingleValuedNonLinearVnlOptimizerv4.h index 4e7aa804a1d..7f96a010d20 100644 --- a/Modules/Numerics/Optimizersv4/include/itkSingleValuedNonLinearVnlOptimizerv4.h +++ b/Modules/Numerics/Optimizersv4/include/itkSingleValuedNonLinearVnlOptimizerv4.h @@ -24,6 +24,8 @@ #include "itkSingleValuedVnlCostFunctionAdaptorv4.h" #include "itkCommand.h" +#include // For unique_ptr. + namespace itk { /** @@ -124,7 +126,7 @@ class ITKOptimizersv4_EXPORT SingleValuedNonLinearVnlOptimizerv4 : public Object void IterationReport(const EventObject & event); - CostFunctionAdaptorType * m_CostFunctionAdaptor{}; + std::unique_ptr m_CostFunctionAdaptor; CommandType::Pointer m_Command{}; diff --git a/Modules/Numerics/Optimizersv4/src/itkSingleValuedNonLinearVnlOptimizerv4.cxx b/Modules/Numerics/Optimizersv4/src/itkSingleValuedNonLinearVnlOptimizerv4.cxx index 80d637fc942..f39e6f10f46 100644 --- a/Modules/Numerics/Optimizersv4/src/itkSingleValuedNonLinearVnlOptimizerv4.cxx +++ b/Modules/Numerics/Optimizersv4/src/itkSingleValuedNonLinearVnlOptimizerv4.cxx @@ -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) @@ -67,17 +60,12 @@ 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); } @@ -85,19 +73,19 @@ SingleValuedNonLinearVnlOptimizerv4::SetCostFunctionAdaptor(CostFunctionAdaptorT 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