Skip to content

Commit

Permalink
STYLE: Use (const) unique_ptr for SparseFieldLayer::m_HeadNode
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

Also defaulted the destructor of `SparseFieldLayer`.
  • Loading branch information
N-Dekker committed Mar 27, 2024
1 parent 5f3adbd commit 821024a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
17 changes: 9 additions & 8 deletions Modules/Core/Common/include/itkSparseFieldLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "itkObjectFactory.h"
#include "itkObject.h"
#include <vector>
#include <memory> // For unique_ptr.

namespace itk
{
Expand Down Expand Up @@ -217,7 +218,7 @@ class ITK_TEMPLATE_EXPORT SparseFieldLayer : public Object
PopFront()
{
m_HeadNode->Next = m_HeadNode->Next->Next;
m_HeadNode->Next->Previous = m_HeadNode;
m_HeadNode->Next->Previous = m_HeadNode.get();
m_Size -= 1;
}

Expand All @@ -226,7 +227,7 @@ class ITK_TEMPLATE_EXPORT SparseFieldLayer : public Object
PushFront(NodeType * n)
{
n->Next = m_HeadNode->Next;
n->Previous = m_HeadNode;
n->Previous = m_HeadNode.get();
m_HeadNode->Next->Previous = n;
m_HeadNode->Next = n;
m_Size += 1;
Expand Down Expand Up @@ -260,22 +261,22 @@ class ITK_TEMPLATE_EXPORT SparseFieldLayer : public Object
Iterator
End()
{
return Iterator(m_HeadNode);
return Iterator(m_HeadNode.get());
}

/** Returns a const iterator pointing one node past the end of the list. */
ConstIterator
End() const
{
return ConstIterator(m_HeadNode);
return ConstIterator(m_HeadNode.get());
}

/** Returns TRUE if the list is empty, FALSE otherwise. Executes in constant
* time. */
bool
Empty() const
{
if (m_HeadNode->Next == m_HeadNode)
if (m_HeadNode->Next == m_HeadNode.get())
{
return true;
}
Expand All @@ -297,15 +298,15 @@ class ITK_TEMPLATE_EXPORT SparseFieldLayer : public Object

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

private:
/** The anchor node of the list. m_HeadNode->Next is the first node in the
* list. If m_HeadNode->Next == m_HeadNode, then the list is empty. */
NodeType * m_HeadNode{};
unsigned int m_Size{};
const std::unique_ptr<NodeType> m_HeadNode{ std::make_unique<NodeType>() };
unsigned int m_Size{};
};
} // end namespace itk

Expand Down
13 changes: 3 additions & 10 deletions Modules/Core/Common/include/itkSparseFieldLayer.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,18 @@ namespace itk
template <typename TNodeType>
SparseFieldLayer<TNodeType>::SparseFieldLayer()
{
m_HeadNode = new NodeType;
m_HeadNode->Next = m_HeadNode;
m_HeadNode->Previous = m_HeadNode;
m_HeadNode->Next = m_HeadNode.get();
m_HeadNode->Previous = m_HeadNode.get();
m_Size = 0;
}

template <typename TNodeType>
SparseFieldLayer<TNodeType>::~SparseFieldLayer()
{
delete m_HeadNode;
}

template <typename TNodeType>
void
SparseFieldLayer<TNodeType>::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);

os << indent << "HeadNode: " << m_HeadNode << std::endl;
os << indent << "HeadNode: " << m_HeadNode.get() << std::endl;
os << indent << "Size: " << m_Size << std::endl;
}

Expand Down

0 comments on commit 821024a

Please sign in to comment.