Skip to content

Commit 55fb520

Browse files
N-Dekkerdzenanz
authored andcommitted
STYLE: Remove std::allocator from TestImportImageContainer (Common/test)
Replaced the use of `std::allocator` by the equivalent `new Element[]` and `delete[]` statements. Renamed `m_MemoryAllocatedByAllocator` to `m_MemoryAllocatedByTestImportImageContainer`. Aims to simplify the code, and make it easier to upgrade to C++17 and further. C++17 has deprecated the `std::allocator::allocate` and `std::allocator::destroy` function calls that are removed with this commit.
1 parent bbc691a commit 55fb520

File tree

1 file changed

+5
-27
lines changed

1 file changed

+5
-27
lines changed

Modules/Core/Common/test/itkFactoryTestLib.cxx

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ class TestImportImageContainer : public itk::ImportImageContainer<TElementIdenti
4343
using typename Superclass::ElementIdentifier;
4444
using typename Superclass::Element;
4545

46-
using Allocator = std::allocator<TElement>;
47-
4846
// Methods from itkObject
4947
virtual ~TestImportImageContainer()
5048
{
@@ -67,21 +65,9 @@ class TestImportImageContainer : public itk::ImportImageContainer<TElementIdenti
6765
TElement * data;
6866
try
6967
{
70-
// allocate normally only requires 1 argument.
71-
// MSVC 6.0 makes it require 2, we set the second to be
72-
// a null pointer which means no allocation hint
73-
// Sun cc compiler needs a cast to assign a void pointer to another pointer
74-
data = static_cast<TElement *>(m_Allocator.allocate(size, 0));
75-
if (data)
76-
{
77-
new (data) Element[size];
78-
}
68+
data = new Element[size];
7969
}
8070
catch (...)
81-
{
82-
data = 0;
83-
}
84-
if (!data)
8571
{
8672
// We cannot construct an error string here because we may be out
8773
// of memory. Do not use the exception macro.
@@ -91,7 +77,7 @@ class TestImportImageContainer : public itk::ImportImageContainer<TElementIdenti
9177
m_TotalSize = size * sizeof(TElement);
9278
itkTotalMemoryUsed += m_TotalSize;
9379

94-
m_MemoryAllocatedByAllocator = true;
80+
m_MemoryAllocatedByTestImportImageContainer = true;
9581

9682
std::cout << "TestImportImageContainer: Total memory used is " << itkTotalMemoryUsed << " bytes" << std::endl;
9783

@@ -105,16 +91,9 @@ class TestImportImageContainer : public itk::ImportImageContainer<TElementIdenti
10591
<< typeid(TElement).name() << " totaling " << sizeof(TElement) * this->Capacity() << " bytes"
10692
<< std::endl;
10793

108-
if (m_MemoryAllocatedByAllocator)
94+
if (m_MemoryAllocatedByTestImportImageContainer)
10995
{
110-
TElement * ptr = this->GetImportPointer();
111-
const TElement * const end = ptr + this->Capacity();
112-
for (TElement * base = ptr; base < end; ++base)
113-
{
114-
m_Allocator.destroy(base);
115-
}
116-
m_Allocator.deallocate(ptr, this->Capacity());
117-
96+
delete[] this->GetImportPointer();
11897
this->SetImportPointer(0);
11998
this->SetCapacity(0);
12099
this->SetSize(0);
@@ -130,8 +109,7 @@ class TestImportImageContainer : public itk::ImportImageContainer<TElementIdenti
130109
private:
131110
mutable TElementIdentifier m_TotalSize;
132111

133-
mutable Allocator m_Allocator;
134-
mutable bool m_MemoryAllocatedByAllocator{ false };
112+
mutable bool m_MemoryAllocatedByTestImportImageContainer{ false };
135113
};
136114

137115
class ImportImageContainerFactory : public itk::ObjectFactoryBase

0 commit comments

Comments
 (0)