From 3ad51b4ac271724a1b826f581047ffd71ecae049 Mon Sep 17 00:00:00 2001 From: Mihail Isakov Date: Sat, 2 Dec 2023 13:35:42 +0100 Subject: [PATCH] STYLE: Update comments and parameter in the ImportImageContainer Updated comments and the parameter 'UseValueInitialization' better describe the behavior of the ImportImageContainer. The same parameter in VectorImage has also been updated. Also removed comment from AllocateElements about 'new' and throwing or not throwing an exception if allocation fails. Closes https://github.com/InsightSoftwareConsortium/ITK/issues/4359 --- .../Common/include/itkImportImageContainer.h | 13 ++++++------- .../Common/include/itkImportImageContainer.hxx | 17 +++++++---------- Modules/Core/Common/include/itkVectorImage.h | 2 +- Modules/Core/Common/include/itkVectorImage.hxx | 4 ++-- 4 files changed, 16 insertions(+), 20 deletions(-) diff --git a/Modules/Core/Common/include/itkImportImageContainer.h b/Modules/Core/Common/include/itkImportImageContainer.h index 4d51f437708..fca74e8a714 100644 --- a/Modules/Core/Common/include/itkImportImageContainer.h +++ b/Modules/Core/Common/include/itkImportImageContainer.h @@ -118,12 +118,12 @@ class ITK_TEMPLATE_EXPORT ImportImageContainer : public Object * container. However, in this particular case, Reserve as a Resize * semantics that is kept for backward compatibility reasons. * - * If UseDefaultConstructor is true, then * the default constructor is used - * to initialize each element. POD date types initialize to zero. + * If UseValueInitialization is true, then POD types will be + * zero-initialized. * * \sa SetImportPointer() */ void - Reserve(ElementIdentifier size, const bool UseDefaultConstructor = false); + Reserve(ElementIdentifier size, const bool UseValueInitialization = false); /** Tell the container to try to minimize its memory usage for * storage of the current number of elements. If new memory is @@ -162,12 +162,11 @@ class ITK_TEMPLATE_EXPORT ImportImageContainer : public Object PrintSelf(std::ostream & os, Indent indent) const override; /** - * Allocates elements of the array. If UseDefaultConstructor is true, then - * the default constructor is used to initialize each element. POD date types - * initialize to zero. + * Allocates elements of the array. If UseValueInitialization is true, then + * POD types will be zero-initialized. */ virtual TElement * - AllocateElements(ElementIdentifier size, bool UseDefaultConstructor = false) const; + AllocateElements(ElementIdentifier size, bool UseValueInitialization = false) const; virtual void DeallocateManagedMemory(); diff --git a/Modules/Core/Common/include/itkImportImageContainer.hxx b/Modules/Core/Common/include/itkImportImageContainer.hxx index acc6456dd20..106f0a4e8cd 100644 --- a/Modules/Core/Common/include/itkImportImageContainer.hxx +++ b/Modules/Core/Common/include/itkImportImageContainer.hxx @@ -44,7 +44,7 @@ ImportImageContainer::~ImportImageContainer() */ template void -ImportImageContainer::Reserve(ElementIdentifier size, const bool UseDefaultConstructor) +ImportImageContainer::Reserve(ElementIdentifier size, const bool UseValueInitialization) { // Reserve has a Resize semantics. We keep it that way for // backwards compatibility . @@ -53,7 +53,7 @@ ImportImageContainer::Reserve(ElementIdentifier si { if (size > m_Capacity) { - TElement * temp = this->AllocateElements(size, UseDefaultConstructor); + TElement * temp = this->AllocateElements(size, UseValueInitialization); // only copy the portion of the data used in the old buffer std::copy_n(m_ImportPointer, m_Size, temp); @@ -73,7 +73,7 @@ ImportImageContainer::Reserve(ElementIdentifier si } else { - m_ImportPointer = this->AllocateElements(size, UseDefaultConstructor); + m_ImportPointer = this->AllocateElements(size, UseValueInitialization); m_Capacity = size; m_Size = size; m_ContainerManageMemory = true; @@ -153,22 +153,19 @@ ImportImageContainer::SetImportPointer(TElement * template TElement * ImportImageContainer::AllocateElements(ElementIdentifier size, - bool UseDefaultConstructor) const + bool UseValueInitialization) const { - // Encapsulate all image memory allocation here to throw an - // exception when memory allocation fails even when the compiler - // does not do this by default. TElement * data; try { - if (UseDefaultConstructor) + if (UseValueInitialization) { - data = new TElement[size](); // POD types initialized to 0, others use default constructor. + data = new TElement[size](); } else { - data = new TElement[size]; // Faster but uninitialized + data = new TElement[size]; } } catch (...) diff --git a/Modules/Core/Common/include/itkVectorImage.h b/Modules/Core/Common/include/itkVectorImage.h index ce93b3943a3..ca6797d4f12 100644 --- a/Modules/Core/Common/include/itkVectorImage.h +++ b/Modules/Core/Common/include/itkVectorImage.h @@ -203,7 +203,7 @@ class ITK_TEMPLATE_EXPORT VectorImage : public ImageBase /** Allocate the image memory. The size of the image must * already be set, e.g. by calling SetRegions(). */ void - Allocate(bool UseDefaultConstructor = false) override; + Allocate(bool UseValueInitialization = false) override; /** Restore the data object to its initial state. This means releasing * memory. */ diff --git a/Modules/Core/Common/include/itkVectorImage.hxx b/Modules/Core/Common/include/itkVectorImage.hxx index 2ff98e10c50..60b71a6e11d 100644 --- a/Modules/Core/Common/include/itkVectorImage.hxx +++ b/Modules/Core/Common/include/itkVectorImage.hxx @@ -35,7 +35,7 @@ namespace itk //---------------------------------------------------------------------------- template void -VectorImage::Allocate(const bool UseDefaultConstructor) +VectorImage::Allocate(const bool UseValueInitialization) { if (m_VectorLength == 0) { @@ -46,7 +46,7 @@ VectorImage::Allocate(const bool UseDefaultConstructor) this->ComputeOffsetTable(); num = this->GetOffsetTable()[VImageDimension]; - m_Buffer->Reserve(num * m_VectorLength, UseDefaultConstructor); + m_Buffer->Reserve(num * m_VectorLength, UseValueInitialization); } template