From 77a2f45224dd1ccf00fa1b49b94136141663927e Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Fri, 16 Aug 2024 21:34:36 +0200 Subject: [PATCH] STYLE: Replace READ_ELEMENT_IF with TryToReadFromStore variadic template MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaced the original `READ_ELEMENT_IF` macro with a variadic template, using a C++17 fold-expression. Following C++ Core Guidelines, May 11, 2024, "Don’t use macros for program text manipulation", https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-macros --- src/itkOMEZarrNGFFImageIO.cxx | 54 ++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/src/itkOMEZarrNGFFImageIO.cxx b/src/itkOMEZarrNGFFImageIO.cxx index ca354d9..a8317a3 100644 --- a/src/itkOMEZarrNGFFImageIO.cxx +++ b/src/itkOMEZarrNGFFImageIO.cxx @@ -190,6 +190,33 @@ ReadFromStore(const tensorstore::TensorStore<> & store, const ImageIORegion & st } } +// Reads from the store if the specified pixel type and the ITK component type match. +template +bool +ReadFromStoreIfTypesMatch(const IOComponentEnum componentType, + const tensorstore::TensorStore<> & store, + const ImageIORegion & storeIORegion, + void * buffer) +{ + if (tensorstoreToITKComponentType(tensorstore::dtype_v) == componentType) + { + ReadFromStore(store, storeIORegion, static_cast(buffer)); + return true; + } + return false; +} + +// Tries to read from the specified store, trying any of the specified pixel types. +template +bool +TryToReadFromStore(const IOComponentEnum componentType, + const tensorstore::TensorStore<> & store, + const ImageIORegion & storeIORegion, + void * buffer) +{ + return (ReadFromStoreIfTypesMatch(componentType, store, storeIORegion, buffer) || ...); +} + // Update an existing "read" specification for an "http" driver to retrieve remote files. // Note that an "http" driver specification may operate on an HTTP or HTTPS connection. void @@ -601,14 +628,6 @@ OMEZarrNGFFImageIO::ReadImageInformation() ReadArrayMetadata(std::string(this->GetFileName()) + "/" + path, driver); } -// We call tensorstoreToITKComponentType for each type. -// Hopefully compiler will optimize it away via constant propagation and inlining. -#define READ_ELEMENT_IF(typeName) \ - else if (tensorstoreToITKComponentType(tensorstore::dtype_v) == this->GetComponentType()) \ - { \ - ReadFromStore(store, storeIORegion, reinterpret_cast(buffer)); \ - } - void OMEZarrNGFFImageIO::Read(void * buffer) { @@ -634,22 +653,11 @@ OMEZarrNGFFImageIO::Read(void * buffer) << storeIORegion; } - if (false) + if (const IOComponentEnum componentType{ this->GetComponentType() }; + !TryToReadFromStore( + componentType, store, storeIORegion, buffer)) { - } - READ_ELEMENT_IF(int8_t) - READ_ELEMENT_IF(uint8_t) - READ_ELEMENT_IF(int16_t) - READ_ELEMENT_IF(uint16_t) - READ_ELEMENT_IF(int32_t) - READ_ELEMENT_IF(uint32_t) - READ_ELEMENT_IF(int64_t) - READ_ELEMENT_IF(uint64_t) - READ_ELEMENT_IF(float) - READ_ELEMENT_IF(double) - else - { - itkExceptionMacro("Unsupported component type: " << GetComponentTypeAsString(this->GetComponentType())); + itkExceptionMacro("Unsupported component type: " << GetComponentTypeAsString(componentType)); } }