Skip to content

Commit

Permalink
COMP: Simplify static array length checking
Browse files Browse the repository at this point in the history
Use c++17 features to enforce compile-time
sanity checks on array lengths.
  • Loading branch information
hjmjohnson committed Apr 11, 2024
1 parent 877acba commit c672055
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions Modules/IO/ImageBase/test/itkImageIOBaseTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@

// Specific ImageIO test

// Macro to check that two arrays have the same size at compile time. It doesn't compile if they don't
// as it tries to create an array of size(-1)
// https://scaryreasoner.wordpress.com/2009/02/28/checking-sizeof-at-compile-time/
#define CHECK_ARRAYS_HAVE_SAME_SIZE_AT_COMPILE_TIME(array1, array2) \
((void)sizeof(char[1 - 2 * !!(std::size(array1) - std::size(array2))]))

int
itkImageIOBaseTest(int, char *[])
{
Expand Down Expand Up @@ -168,8 +162,12 @@ itkImageIOBaseTest(int, char *[])
"complex",
"fixed_array",
"matrix" };
CHECK_ARRAYS_HAVE_SAME_SIZE_AT_COMPILE_TIME(listComponentTypeString, listComponentType);
CHECK_ARRAYS_HAVE_SAME_SIZE_AT_COMPILE_TIME(listIOPixelType, listIOPixelTypeString);
// Compile time verification that the array lengths are correct for type and name
static_assert(std::size(listComponentType) == std::size(listComponentTypeString),
"listComponentType and listComponentTypeString must be same length");
static_assert(std::size(listIOPixelType) == std::size(listIOPixelTypeString),
"listIOPixelType and listIOPixelTypeString must be same length");

constexpr size_t listComponentSize = std::size(listComponentType);
constexpr size_t listPixelSize = std::size(listIOPixelType);
{ // Test the static version of the string <-> type conversions
Expand Down

0 comments on commit c672055

Please sign in to comment.