Skip to content

Commit

Permalink
STYLE: Clean-up ByteSwapper, using SwapWriteRange helper function
Browse files Browse the repository at this point in the history
Aims to reduce code duplication.

- Follow-up to pull request InsightSoftwareConsortium#4855
commit 3b04ad1
"STYLE: Clean-up ByteSwapper, using private `SwapBytes` helper function"
  • Loading branch information
N-Dekker authored and dzenanz committed Oct 3, 2024
1 parent 58ff179 commit 9413dc4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
4 changes: 4 additions & 0 deletions Modules/Core/Common/include/itkByteSwapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ class ITK_TEMPLATE_EXPORT ByteSwapper : public Object
static void
SwapBytes(T &);

/** Swaps and writes the specified elements to the specified output stream. */
static void
SwapWriteRange(const T * ptr, SizeValueType numberOfElements, std::ostream & outputStream);

static constexpr bool m_SystemIsBigEndian{
#ifdef CMAKE_WORDS_BIGENDIAN
true
Expand Down
53 changes: 24 additions & 29 deletions Modules/Core/Common/include/itkByteSwapper.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,7 @@ ByteSwapper<T>::SwapWriteRangeFromSystemToBigEndian(const T * p, int num, std::o
}
else
{
switch (sizeof(T))
{
case 2:
Self::SwapWrite2Range(p, num, fp);
return;
case 4:
Self::SwapWrite4Range(p, num, fp);
return;
case 8:
Self::SwapWrite8Range(p, num, fp);
return;
default:
itkGenericExceptionMacro("Cannot swap number of bytes requested");
}
SwapWriteRange(p, num, *fp);
}
}

Expand Down Expand Up @@ -118,20 +105,7 @@ ByteSwapper<T>::SwapWriteRangeFromSystemToLittleEndian(const T * p, int num, std
{
if constexpr (m_SystemIsBigEndian && sizeof(T) > 1)
{
switch (sizeof(T))
{
case 2:
Self::SwapWrite2Range(p, num, fp);
return;
case 4:
Self::SwapWrite4Range(p, num, fp);
return;
case 8:
Self::SwapWrite8Range(p, num, fp);
return;
default:
itkGenericExceptionMacro("Cannot swap number of bytes requested");
}
SwapWriteRange(p, num, *fp);
}
else
{
Expand Down Expand Up @@ -308,7 +282,7 @@ ByteSwapper<T>::SwapWrite8Range(const void * ptr, BufferSizeType num, std::ostre
}


// The following member function is private:
// The following member functions are private:

template <typename T>
void
Expand All @@ -333,6 +307,27 @@ ByteSwapper<T>::SwapBytes(T & value)
}
}

template <typename T>
void
ByteSwapper<T>::SwapWriteRange(const T * buffer, SizeValueType numberOfElements, std::ostream & outputStream)
{
auto chunkSize = std::min(numberOfElements, SizeValueType{ 1000000 });

const auto chunk = make_unique_for_overwrite<T[]>(chunkSize);

while (numberOfElements > 0)
{
std::copy_n(buffer, chunkSize, chunk.get());
std::for_each_n(chunk.get(), chunkSize, [](T & element) { SwapBytes(element); });

outputStream.write(reinterpret_cast<const char *>(chunk.get()),
static_cast<std::streamsize>(chunkSize * sizeof(T)));
buffer += chunkSize;
numberOfElements -= chunkSize;
chunkSize = std::min(numberOfElements, chunkSize);
}
}

} // end namespace itk

#endif

0 comments on commit 9413dc4

Please sign in to comment.