Skip to content

Commit

Permalink
STYLE: Replace DerefRawPointer from GTesting with ITK 5.4 itk::Deref
Browse files Browse the repository at this point in the history
Following ITK pull request InsightSoftwareConsortium/ITK#4278 commit InsightSoftwareConsortium/ITK@042d423 "ENH: Add `Deref(T *)`, to ease dereferencing a pointer safely".
  • Loading branch information
N-Dekker committed Jun 10, 2024
1 parent 995f471 commit 79272e1
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 97 deletions.
10 changes: 5 additions & 5 deletions Common/GTesting/itkImageFullSamplerGTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
using elx::CoreMainGTestUtilities::CreateImage;
using elx::CoreMainGTestUtilities::CreateImageFilledWithSequenceOfNaturalNumbers;
using elx::CoreMainGTestUtilities::CreateRandomImageDomain;
using elx::CoreMainGTestUtilities::DerefRawPointer;
using elx::CoreMainGTestUtilities::GenerateRandomSign;
using elx::CoreMainGTestUtilities::ImageDomain;
using elx::CoreMainGTestUtilities::minimumImageSizeValue;
using itk::Deref;


GTEST_TEST(ImageFullSampler, OutputHasSameSequenceOfPixelValuesAsInput)
Expand All @@ -49,7 +49,7 @@ GTEST_TEST(ImageFullSampler, OutputHasSameSequenceOfPixelValuesAsInput)
sampler.SetInput(image);
sampler.Update();

const auto & output = DerefRawPointer(sampler.GetOutput());
const auto & output = Deref(sampler.GetOutput());
const itk::ImageBufferRange imageBufferRange(*image);
const std::size_t numberOfSamples{ output.size() };

Expand Down Expand Up @@ -77,7 +77,7 @@ GTEST_TEST(ImageFullSampler, HasSameOutputWhenUsingMultiThread)
sampler.SetUseMultiThread(useMultiThread);
sampler.SetInput(image);
sampler.Update();
return std::move(DerefRawPointer(sampler.GetOutput()).CastToSTLContainer());
return std::move(Deref(sampler.GetOutput()).CastToSTLContainer());
};

EXPECT_EQ(generateSamples(true), generateSamples(false));
Expand Down Expand Up @@ -114,7 +114,7 @@ GTEST_TEST(ImageFullSampler, HasSameOutputWhenUsingFullyFilledMask)
}

sampler.Update();
return std::move(DerefRawPointer(sampler.GetOutput()).CastToSTLContainer());
return std::move(Deref(sampler.GetOutput()).CastToSTLContainer());
};

EXPECT_EQ(generateSamples(true), generateSamples(false));
Expand Down Expand Up @@ -177,7 +177,7 @@ GTEST_TEST(ImageFullSampler, ExactlyEqualVersusSlightlyDifferentMaskImageDomain)

sampler.SetMask(maskSpatialObject);
sampler.Update();
return std::move(DerefRawPointer(sampler.GetOutput()).CastToSTLContainer());
return std::move(Deref(sampler.GetOutput()).CastToSTLContainer());
};

const auto samplesOnExactlyEqualImageDomains = generateSamples(true);
Expand Down
19 changes: 10 additions & 9 deletions Common/GTesting/itkImageGridSamplerGTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "itkImageGridSampler.h"
#include "itkImageFullSampler.h"
#include "elxDefaultConstruct.h"
#include <itkDeref.h>
#include <itkImage.h>
#include <itkImageMaskSpatialObject.h>

Expand All @@ -35,8 +36,8 @@ using elx::CoreMainGTestUtilities::minimumImageSizeValue;
using elx::CoreMainGTestUtilities::CreateImage;
using elx::CoreMainGTestUtilities::CreateImageFilledWithSequenceOfNaturalNumbers;
using elx::CoreMainGTestUtilities::CreateRandomImageDomain;
using elx::CoreMainGTestUtilities::DerefRawPointer;
using elx::CoreMainGTestUtilities::ImageDomain;
using itk::Deref;

namespace
{
Expand Down Expand Up @@ -81,7 +82,7 @@ GTEST_TEST(ImageGridSampler, HasSameOutputAsFullSamplerByDefault)
const auto generateSamples = [image](auto && sampler) {
sampler.SetInput(image);
sampler.Update();
return std::move(DerefRawPointer(sampler.GetOutput()).CastToSTLContainer());
return std::move(Deref(sampler.GetOutput()).CastToSTLContainer());
};

const auto samples = generateSamples(elx::DefaultConstruct<ImageGridSampler<ImageType>>{});
Expand Down Expand Up @@ -110,7 +111,7 @@ GTEST_TEST(ImageGridSampler, MaxSampleGridSpacing)
sampler.SetInput(image);
sampler.SetUseMultiThread(false);
sampler.Update();
const auto & samples = DerefRawPointer(sampler.GetOutput()).CastToSTLContainer();
const auto & samples = Deref(sampler.GetOutput()).CastToSTLContainer();

ASSERT_FALSE(samples.empty());
EXPECT_EQ(samples.size(), 1);
Expand Down Expand Up @@ -142,7 +143,7 @@ GTEST_TEST(ImageGridSampler, SampleGridSpacingGreaterEqualToImageSize)
sampler.SetSampleGridSpacing(sampleGridSpacing);
sampler.SetInput(image);
sampler.Update();
const auto & samples = DerefRawPointer(sampler.GetOutput()).CastToSTLContainer();
const auto & samples = Deref(sampler.GetOutput()).CastToSTLContainer();

ASSERT_FALSE(samples.empty());
EXPECT_EQ(samples.size(), 1);
Expand Down Expand Up @@ -175,7 +176,7 @@ GTEST_TEST(ImageGridSampler, SampleGridSpacingOneLessThanImageSize)
sampler.SetSampleGridSpacing(sampleGridSpacing);
sampler.SetInput(image);
sampler.Update();
const auto & samples = DerefRawPointer(sampler.GetOutput()).CastToSTLContainer();
const auto & samples = Deref(sampler.GetOutput()).CastToSTLContainer();

ASSERT_FALSE(samples.empty());
EXPECT_EQ(samples.size(), 4);
Expand All @@ -202,7 +203,7 @@ GTEST_TEST(ImageGridSampler, SampleGridSpacingTwo)
sampler.SetSampleGridSpacing(itk::MakeFilled<SamplerType::SampleGridSpacingType>(2));
sampler.SetInput(image);
sampler.Update();
const auto & samples = DerefRawPointer(sampler.GetOutput()).CastToSTLContainer();
const auto & samples = Deref(sampler.GetOutput()).CastToSTLContainer();

ASSERT_FALSE(samples.empty());

Expand Down Expand Up @@ -234,7 +235,7 @@ GTEST_TEST(ImageGridSampler, HasSameOutputWhenUsingMultiThread)
sampler.SetUseMultiThread(useMultiThread);
sampler.SetInput(image);
sampler.Update();
return std::move(DerefRawPointer(sampler.GetOutput()).CastToSTLContainer());
return std::move(Deref(sampler.GetOutput()).CastToSTLContainer());
};
const auto samplesGeneratedUsingMultiThreading = generateSamples(true);

Expand Down Expand Up @@ -276,7 +277,7 @@ GTEST_TEST(ImageGridSampler, HasSameOutputWhenUsingFullyFilledMask)
}

sampler.Update();
return std::move(DerefRawPointer(sampler.GetOutput()).CastToSTLContainer());
return std::move(Deref(sampler.GetOutput()).CastToSTLContainer());
};

const auto samplesGeneratedUsingFullyFilledMask = generateSamples(true);
Expand Down Expand Up @@ -323,7 +324,7 @@ GTEST_TEST(ImageGridSampler, OneOutOfThreeMask)
sampler.SetSampleGridSpacing(itk::MakeFilled<SamplerType::SampleGridSpacingType>(2));
sampler.SetUseMultiThread(useMultiThread);
sampler.Update();
return std::move(DerefRawPointer(sampler.GetOutput()).CastToSTLContainer());
return std::move(Deref(sampler.GetOutput()).CastToSTLContainer());
};

const auto samplesGeneratedUsingMultiThreading = generateSamples(true);
Expand Down
9 changes: 5 additions & 4 deletions Common/GTesting/itkImageRandomCoordinateSamplerGTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@
#include "GTesting/elxCoreMainGTestUtilities.h"

// ITK header files:
#include <itkDeref.h>
#include <itkImage.h>
#include <itkMersenneTwisterRandomVariateGenerator.h>

#include <gtest/gtest.h>
#include <array>

// Using-declarations:
using elx::CoreMainGTestUtilities::DerefRawPointer;
using elx::CoreMainGTestUtilities::DerefSmartPointer;
using elx::CoreMainGTestUtilities::minimumImageSizeValue;
using elx::CoreMainGTestUtilities::CreateImageFilledWithSequenceOfNaturalNumbers;
using itk::Deref;
using itk::Statistics::MersenneTwisterRandomVariateGenerator;


Expand All @@ -55,7 +56,7 @@ GTEST_TEST(ImageRandomCoordinateSampler, CheckImageValuesOfSamples)
sampler.SetInput(image);
sampler.Update();

const auto & samples = DerefRawPointer(sampler.GetOutput()).CastToSTLConstContainer();
const auto & samples = Deref(sampler.GetOutput()).CastToSTLConstContainer();

ASSERT_EQ(samples.size(), numberOfSamples);

Expand Down Expand Up @@ -88,7 +89,7 @@ GTEST_TEST(ImageRandomCoordinateSampler, SetSeedMakesRandomizationDeterministic)
DerefSmartPointer(MersenneTwisterRandomVariateGenerator::GetInstance()).SetSeed(seed);
sampler.SetInput(image);
sampler.Update();
return std::move(DerefRawPointer(sampler.GetOutput()).CastToSTLContainer());
return std::move(Deref(sampler.GetOutput()).CastToSTLContainer());
};

const auto samples = generateSamples();
Expand Down Expand Up @@ -119,7 +120,7 @@ GTEST_TEST(ImageRandomCoordinateSampler, HasSameOutputWhenUsingMultiThread)
sampler.SetUseMultiThread(useMultiThread);
sampler.SetInput(image);
sampler.Update();
return std::move(DerefRawPointer(sampler.GetOutput()).CastToSTLContainer());
return std::move(Deref(sampler.GetOutput()).CastToSTLContainer());
};

EXPECT_EQ(generateSamples(true), generateSamples(false));
Expand Down
8 changes: 4 additions & 4 deletions Common/GTesting/itkImageRandomSamplerGTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
#include <array>

// Using-declarations:
using elx::CoreMainGTestUtilities::DerefRawPointer;
using elx::CoreMainGTestUtilities::DerefSmartPointer;
using elx::CoreMainGTestUtilities::minimumImageSizeValue;
using elx::CoreMainGTestUtilities::CreateImageFilledWithSequenceOfNaturalNumbers;
using itk::Deref;


GTEST_TEST(ImageRandomSampler, CheckImageValuesOfSamples)
Expand All @@ -53,7 +53,7 @@ GTEST_TEST(ImageRandomSampler, CheckImageValuesOfSamples)
sampler.SetInput(image);
sampler.Update();

const auto & samples = DerefRawPointer(sampler.GetOutput()).CastToSTLConstContainer();
const auto & samples = Deref(sampler.GetOutput()).CastToSTLConstContainer();

ASSERT_EQ(samples.size(), numberOfSamples);

Expand Down Expand Up @@ -83,7 +83,7 @@ GTEST_TEST(ImageRandomSampler, SetSeedMakesRandomizationDeterministic)
sampler.SetSeed(seed);
sampler.SetInput(image);
sampler.Update();
return std::move(DerefRawPointer(sampler.GetOutput()).CastToSTLContainer());
return std::move(Deref(sampler.GetOutput()).CastToSTLContainer());
};

const auto samples = generateSamples();
Expand Down Expand Up @@ -113,7 +113,7 @@ GTEST_TEST(ImageRandomSampler, HasSameOutputWhenUsingMultiThread)
sampler.SetSeed(1);
sampler.SetInput(image);
sampler.Update();
return std::move(DerefRawPointer(sampler.GetOutput()).CastToSTLContainer());
return std::move(Deref(sampler.GetOutput()).CastToSTLContainer());
};

EXPECT_EQ(generateSamples(true), generateSamples(false));
Expand Down
9 changes: 5 additions & 4 deletions Common/GTesting/itkImageRandomSamplerSparseMaskGTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "GTesting/elxCoreMainGTestUtilities.h"

// ITK header files:
#include <itkDeref.h>
#include <itkImage.h>
#include <itkMersenneTwisterRandomVariateGenerator.h>

Expand All @@ -30,13 +31,13 @@
#include <itkImageMaskSpatialObject.h>

// Using-declarations:
using elx::CoreMainGTestUtilities::DerefRawPointer;
using elx::CoreMainGTestUtilities::DerefSmartPointer;
using elx::CoreMainGTestUtilities::minimumImageSizeValue;
using elx::CoreMainGTestUtilities::CreateImage;
using elx::CoreMainGTestUtilities::CreateImageFilledWithSequenceOfNaturalNumbers;
using elx::CoreMainGTestUtilities::FillImageRegion;
using elx::CoreMainGTestUtilities::ImageDomain;
using itk::Deref;
using itk::Statistics::MersenneTwisterRandomVariateGenerator;


Expand Down Expand Up @@ -68,7 +69,7 @@ GTEST_TEST(ImageRandomSamplerSparseMask, CheckImageValuesOfSamples)
sampler.SetNumberOfSamples(numberOfSamples);
sampler.Update();

const auto & samples = DerefRawPointer(sampler.GetOutput()).CastToSTLConstContainer();
const auto & samples = Deref(sampler.GetOutput()).CastToSTLConstContainer();

ASSERT_EQ(samples.size(), numberOfSamples);

Expand Down Expand Up @@ -109,7 +110,7 @@ GTEST_TEST(ImageRandomSamplerSparseMask, SetSeedMakesRandomizationDeterministic)
sampler.SetInput(image);
sampler.SetMask(maskSpatialObject);
sampler.Update();
return std::move(DerefRawPointer(sampler.GetOutput()).CastToSTLContainer());
return std::move(Deref(sampler.GetOutput()).CastToSTLContainer());
};

const auto samples = generateSamples();
Expand Down Expand Up @@ -149,7 +150,7 @@ GTEST_TEST(ImageRandomSamplerSparseMask, HasSameOutputWhenUsingMultiThread)
sampler.SetInput(image);
sampler.SetMask(maskSpatialObject);
sampler.Update();
return std::move(DerefRawPointer(sampler.GetOutput()).CastToSTLContainer());
return std::move(Deref(sampler.GetOutput()).CastToSTLContainer());
};

EXPECT_EQ(generateSamples(true), generateSamples(false));
Expand Down
22 changes: 4 additions & 18 deletions Core/Main/GTesting/elxCoreMainGTestUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <elxParameterObject.h>
#include <elxConversion.h>

#include <itkDeref.h>
#include <itkImage.h>
#include <itkImageBase.h>
#include <itkImageBufferRange.h>
Expand Down Expand Up @@ -85,30 +86,15 @@ class Exception : public std::exception
} \
static_assert(true, "Expect a semi-colon ';' at the end of a macro call")

/// Dereferences the specified raw pointer. Throws an `Exception` instead, when the pointer is null.
template <typename TRawPointer>
decltype(auto)
DerefRawPointer(const TRawPointer ptr)
{
static_assert(std::is_pointer_v<TRawPointer>, "For smart pointers, use DerefSmartPointer instead!");

if (ptr == nullptr)
{
throw Exception("DerefRawPointer error: the pointer should not be null!");
}
return *ptr;
}


template <typename TSmartPointer>
decltype(auto)
DerefSmartPointer(const TSmartPointer & ptr)
{
static_assert(!std::is_pointer_v<TSmartPointer>, "For raw pointers, use DerefRawPointer instead!");
static_assert(!std::is_pointer_v<TSmartPointer>, "For raw pointers, use itk::Deref instead!");

if (ptr == nullptr)
{
throw Exception("DerefRawPointer error: the (smart) pointer should not be null!");
throw Exception("DerefSmartPointer error: the (smart) pointer should not be null!");
}
return *ptr;
}
Expand Down Expand Up @@ -286,7 +272,7 @@ std::vector<double>
GetTransformParametersFromFilter(TFilter & filter)
{
const auto transformParameterObject = filter.GetTransformParameterObject();
const auto & transformParameterMaps = DerefRawPointer(transformParameterObject).GetParameterMaps();
const auto & transformParameterMaps = itk::Deref(transformParameterObject).GetParameterMaps();
return GetTransformParametersFromMaps(transformParameterMaps);
}

Expand Down
Loading

0 comments on commit 79272e1

Please sign in to comment.