Skip to content

Commit

Permalink
STYLE: Make PrintSelf implementation style consistent
Browse files Browse the repository at this point in the history
Make `PrintSelf`implementation style consistent following the ITK SWG
coding style guideline and the available ITK macros:
- Always print the superclass first.
- Print only the class instance variables.
- Use the `os << indent << "{ivarName}: " << m_ivarName << std::endl`
  recipe: do not print the leading `m_` of the ivar; use a colon after
  printing its name; leave a whitespace between the colon and the
  printed value of the ivar.
- For boolean ivars print `On`/`Off` according to their values using the
  ternary operator.
- Use `itkPrintSelfObjectMacro` to print objects inheriting from
  `itk::LightObject` that can be null.
- For pointers that do not inherit from `itk::LightObject` check whether
  they are non-null before attempting to print them and print the
  `(empty)` string otherwise.
- Rely on the self printing ability of the ITK classes to avoid
  boilerplate code when printing the corresponding ivars in other
  classes' `PrintSelf` methods.
- Take advantage of the output stream `<<` insertion operator overload
  for `std::vector` types in the `itk::print_helper` namespace defined
  in `itkPrintHelper.h` to print such types consistently.
- Cast custom types to their numerical values using
  `itk::NumericTraits::PrinType` so that they are printed as expected.
- Remove statements printing the hard-coded class name and its own
  pointer, e.g.
  ```
  os << indent << "ArrowSpatialObject(" << this << ")" << std::endl;
  ```
  `PrintSelf` does this without requiring the removed line.
- Save typing `this->` to point to the instance of the current class.

Apply the same principles to the `<<` operator overloads.

Overload the ostream operator for the `FixedImageSamplePoint` class used
by the `itk::ImageToImageMetric` class.

Overload the ostream operator for the `ImageVoxel` class used by the
`itk::DeformableSimplexMesh3DGradientConstraintForceFilter` class.
  • Loading branch information
jhlegarreta authored and dzenanz committed Feb 6, 2023
1 parent 48025bd commit c47ed1c
Show file tree
Hide file tree
Showing 193 changed files with 2,359 additions and 1,446 deletions.
2 changes: 1 addition & 1 deletion Examples/Filtering/CompositeFilterExample.cxx
Expand Up @@ -237,7 +237,7 @@ CompositeExampleImageFilter<TImage>::PrintSelf(std::ostream & os,
{
Superclass::PrintSelf(os, indent);

os << indent << "Threshold:" << this->m_Threshold << std::endl;
os << indent << "Threshold: " << m_Threshold << std::endl;
}

} // end namespace itk
Expand Down
Expand Up @@ -33,7 +33,8 @@ void
VectorCentralDifferenceImageFunction<TInputImage, TCoordRep>::PrintSelf(std::ostream & os, Indent indent) const
{
this->Superclass::PrintSelf(os, indent);
os << indent << "UseImageDirection = " << this->m_UseImageDirection << std::endl;

os << indent << "UseImageDirection: " << m_UseImageDirection << std::endl;
}

template <typename TInputImage, typename TCoordRep>
Expand Down
3 changes: 2 additions & 1 deletion Modules/Core/Common/include/itkBSplineKernelFunction.h
Expand Up @@ -82,7 +82,8 @@ class ITK_TEMPLATE_EXPORT BSplineKernelFunction : public KernelFunctionBase<TRea
PrintSelf(std::ostream & os, Indent indent) const override
{
Superclass::PrintSelf(os, indent);
os << indent << "Spline Order: " << SplineOrder << std::endl;

os << indent << "SplineOrder: " << SplineOrder << std::endl;
}

private:
Expand Down
12 changes: 6 additions & 6 deletions Modules/Core/Common/include/itkColorTable.hxx
Expand Up @@ -19,6 +19,7 @@
#define itkColorTable_hxx

#include "itkNumericTraits.h"
#include "itkPrintHelper.h"
#include "vnl/vnl_sample.h"

#include <sstream>
Expand Down Expand Up @@ -365,14 +366,13 @@ template <typename TComponent>
void
ColorTable<TComponent>::PrintSelf(std::ostream & os, Indent indent) const
{
using namespace print_helper;

Superclass::PrintSelf(os, indent);

os << indent << "NumberOfColors = " << m_NumberOfColors << std::endl;
for (unsigned int i = 0; i < m_NumberOfColors; ++i)
{
os << indent << "ColorName[" << i << "] = " << m_ColorName[i] << ", "
<< "Color[" << i << "] = " << m_Color[i] << std::endl;
}
os << indent << "NumberOfColors: " << m_NumberOfColors << std::endl;
os << indent << "ColorName: " << m_ColorName << std::endl;
os << indent << "Color: " << m_Color << std::endl;
}
} // namespace itk

Expand Down
3 changes: 2 additions & 1 deletion Modules/Core/Common/include/itkConstNeighborhoodIterator.hxx
Expand Up @@ -566,6 +566,8 @@ template <typename TImage, typename TBoundaryCondition>
void
ConstNeighborhoodIterator<TImage, TBoundaryCondition>::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);

DimensionValueType i;

os << indent;
Expand Down Expand Up @@ -623,7 +625,6 @@ ConstNeighborhoodIterator<TImage, TBoundaryCondition>::PrintSelf(std::ostream &
os << m_InnerBoundsHigh[i] << ' ';
}
os << "} }" << std::endl;
Superclass::PrintSelf(os, indent.GetNextIndent());
}

template <typename TImage, typename TBoundaryCondition>
Expand Down
Expand Up @@ -399,6 +399,8 @@ template <typename TImage>
void
ConstNeighborhoodIteratorWithOnlyIndex<TImage>::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);

DimensionValueType i;

os << indent;
Expand Down Expand Up @@ -448,7 +450,6 @@ ConstNeighborhoodIteratorWithOnlyIndex<TImage>::PrintSelf(std::ostream & os, Ind
os << m_InnerBoundsHigh[i] << ' ';
}
os << "} }" << std::endl;
Superclass::PrintSelf(os, indent.GetNextIndent());
}

template <typename TImage>
Expand Down
Expand Up @@ -23,16 +23,16 @@ template <typename TImage, typename TBoundaryCondition>
void
ConstShapedNeighborhoodIterator<TImage, TBoundaryCondition>::PrintSelf(std::ostream & os, Indent indent) const
{
os << indent << "ConstShapedNeighborhoodIterator {this = " << this;
os << " m_ActiveIndexList = [";
Superclass::PrintSelf(os, indent);

os << "ActiveIndexList: [";
for (auto it = m_ActiveIndexList.begin(); it != m_ActiveIndexList.end(); ++it)
{
os << *it << ' ';
}
os << "] ";
os << " m_CenterIsActive = " << m_CenterIsActive;
os << '}' << std::endl;
Superclass::PrintSelf(os, indent.GetNextIndent());

os << "CenterIsActive: " << (m_CenterIsActive ? "On" : "Off") << std::endl;
}

template <typename TImage, typename TBoundaryCondition>
Expand Down
10 changes: 7 additions & 3 deletions Modules/Core/Common/include/itkImageDuplicator.hxx
Expand Up @@ -60,9 +60,13 @@ void
ImageDuplicator<TInputImage>::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "Input Image: " << m_InputImage << std::endl;
os << indent << "Output Image: " << m_DuplicateImage << std::endl;
os << indent << "Internal Image Time: " << m_InternalImageTime << std::endl;

itkPrintSelfObjectMacro(InputImage);
itkPrintSelfObjectMacro(DuplicateImage);

os << indent
<< "InternalImageTime: " << static_cast<typename NumericTraits<ModifiedTimeType>::PrintType>(m_InternalImageTime)
<< std::endl;
}
} // end namespace itk

Expand Down
12 changes: 2 additions & 10 deletions Modules/Core/Common/include/itkInPlaceImageFilter.hxx
Expand Up @@ -37,17 +37,9 @@ void
InPlaceImageFilter<TInputImage, TOutputImage>::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);

os << indent << "InPlace: " << (m_InPlace ? "On" : "Off") << std::endl;
if (this->CanRunInPlace())
{
os << indent << "The input and output to this filter are the same type. The filter can be run in place."
<< std::endl;
}
else
{
os << indent << "The input and output to this filter are different types. The filter cannot be run in place."
<< std::endl;
}
os << indent << "RunningInPlace: " << (m_RunningInPlace ? "On" : "Off") << std::endl;
}

template <typename TInputImage, typename TOutputImage>
Expand Down
15 changes: 9 additions & 6 deletions Modules/Core/Common/include/itkLoggerThreadWrapper.hxx
Expand Up @@ -226,12 +226,15 @@ LoggerThreadWrapper<SimpleLoggerType>::PrintSelf(std::ostream & os, Indent inden
{
Superclass::PrintSelf(os, indent);

os << indent << "Thread ID: " << this->m_Thread.get_id() << std::endl;
os << indent << "Low-priority Message Delay: " << this->m_Delay << std::endl;
os << indent << "Operation Queue Size: " << this->m_OperationQ.size() << std::endl;
os << indent << "Message Queue Size: " << this->m_MessageQ.size() << std::endl;
os << indent << "Level Queue Size: " << this->m_LevelQ.size() << std::endl;
os << indent << "Output Queue Size: " << this->m_OutputQ.size() << std::endl;
os << indent << "Thread ID: " << m_Thread.get_id() << std::endl;
os << indent << "TerminationRequested: " << m_TerminationRequested << std::endl;

os << indent << "OperationQ size: " << m_OperationQ.size() << std::endl;
os << indent << "MessageQ size: " << m_MessageQ.size() << std::endl;
os << indent << "LevelQ size: " << m_LevelQ.size() << std::endl;
os << indent << "OutputQ size: " << m_OutputQ.size() << std::endl;

os << indent << "Delay: " << m_Delay << std::endl;
}

} // namespace itk
Expand Down
8 changes: 4 additions & 4 deletions Modules/Core/Common/include/itkNeighborhood.h
Expand Up @@ -327,10 +327,10 @@ template <typename TPixel, unsigned int VDimension, typename TContainer>
std::ostream &
operator<<(std::ostream & os, const Neighborhood<TPixel, VDimension, TContainer> & neighborhood)
{
os << "Neighborhood:" << std::endl;
os << " Radius:" << neighborhood.GetRadius() << std::endl;
os << " Size:" << neighborhood.GetSize() << std::endl;
os << " DataBuffer:" << neighborhood.GetBufferReference() << std::endl;
os << "Neighborhood: " << std::endl;
os << " Radius: " << neighborhood.GetRadius() << std::endl;
os << " Size: " << neighborhood.GetSize() << std::endl;
os << " DataBuffer: " << neighborhood.GetBufferReference() << std::endl;

return os;
}
Expand Down
17 changes: 11 additions & 6 deletions Modules/Core/Common/include/itkObjectStore.hxx
Expand Up @@ -18,6 +18,9 @@
#ifndef itkObjectStore_hxx
#define itkObjectStore_hxx

#include "itkNumericTraits.h"
#include "itkPrintHelper.h"


namespace itk
{
Expand Down Expand Up @@ -136,14 +139,16 @@ template <typename TObjectType>
void
ObjectStore<TObjectType>::PrintSelf(std::ostream & os, Indent indent) const
{
using namespace print_helper;

Superclass::PrintSelf(os, indent);

os << indent << "m_GrowthStrategy: " << m_GrowthStrategy << std::endl;
os << indent << "m_Size: " << m_Size << std::endl;
os << indent << "m_LinearGrowthSize: " << static_cast<SizeValueType>(m_LinearGrowthSize) << std::endl;
os << indent << "Free list size: " << static_cast<SizeValueType>(m_FreeList.size()) << std::endl;
os << indent << "Free list capacity: " << static_cast<SizeValueType>(m_FreeList.capacity()) << std::endl;
os << indent << "Number of blocks in store: " << static_cast<SizeValueType>(m_Store.size()) << std::endl;
os << indent << "GrowthStrategy: " << m_GrowthStrategy << std::endl;
os << indent << "Size: " << static_cast<typename NumericTraits<SizeValueType>::PrintType>(m_Size) << std::endl;
os << indent
<< "LinearGrowthSize: " << static_cast<typename NumericTraits<SizeValueType>::PrintType>(m_LinearGrowthSize)
<< std::endl;
os << indent << "FreeList: " << m_FreeList << std::endl;
}
} // end namespace itk

Expand Down
Expand Up @@ -39,10 +39,10 @@ PhasedArray3DSpecialCoordinatesImage<TPixel>::PrintSelf(std::ostream & os, Inden
{
Superclass::PrintSelf(os, indent);

os << indent << "m_RadiusSampleSize = " << m_RadiusSampleSize << std::endl;
os << indent << "m_AzimuthAngularSeparation = " << m_AzimuthAngularSeparation << std::endl;
os << indent << "m_ElevationAngularSeparation = " << m_ElevationAngularSeparation << std::endl;
os << indent << "m_FirstSampleDistance = " << m_FirstSampleDistance << std::endl;
os << indent << "RadiusSampleSize: " << m_RadiusSampleSize << std::endl;
os << indent << "AzimuthAngularSeparation: " << m_AzimuthAngularSeparation << std::endl;
os << indent << "ElevationAngularSeparation: " << m_ElevationAngularSeparation << std::endl;
os << indent << "FirstSampleDistance: " << m_FirstSampleDistance << std::endl;
}
} // end namespace itk

Expand Down
4 changes: 2 additions & 2 deletions Modules/Core/Common/include/itkSparseFieldLayer.hxx
Expand Up @@ -42,8 +42,8 @@ SparseFieldLayer<TNodeType>::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);

os << indent << "m_HeadNode: " << m_HeadNode << std::endl;
os << indent << "Empty? : " << this->Empty() << std::endl;
os << indent << "HeadNode: " << m_HeadNode << std::endl;
os << indent << "Size: " << m_Size << std::endl;
}

template <typename TNodeType>
Expand Down
15 changes: 9 additions & 6 deletions Modules/Core/Common/src/itkThreadLogger.cxx
Expand Up @@ -208,12 +208,15 @@ ThreadLogger::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);

os << indent << "Thread ID: " << this->m_Thread.get_id() << std::endl;
os << indent << "Low-priority Message Delay: " << this->m_Delay << std::endl;
os << indent << "Operation Queue Size: " << this->m_OperationQ.size() << std::endl;
os << indent << "Message Queue Size: " << this->m_MessageQ.size() << std::endl;
os << indent << "Level Queue Size: " << this->m_LevelQ.size() << std::endl;
os << indent << "Output Queue Size: " << this->m_OutputQ.size() << std::endl;
os << indent << "Thread ID: " << m_Thread.get_id() << std::endl;
os << indent << "TerminationRequested: " << m_TerminationRequested << std::endl;

os << indent << "OperationQ size: " << m_OperationQ.size() << std::endl;
os << indent << "MessageQ size: " << m_MessageQ.size() << std::endl;
os << indent << "LevelQ size: " << m_LevelQ.size() << std::endl;
os << indent << "OutputQ size: " << m_OutputQ.size() << std::endl;

os << indent << "Delay: " << m_Delay << std::endl;
}

} // namespace itk
12 changes: 11 additions & 1 deletion Modules/Core/GPUCommon/include/itkGPUReduction.hxx
Expand Up @@ -50,7 +50,17 @@ GPUReduction<TElement>::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);

// GetTypenameInString( typeid(TElement), os);
itkPrintSelfObjectMacro(GPUKernelManager);
itkPrintSelfObjectMacro(GPUDataManager);

os << indent << "ReduceGPUKernelHandle: " << m_ReduceGPUKernelHandle << std::endl;
os << indent << "TestGPUKernelHandle: " << m_TestGPUKernelHandle << std::endl;

os << indent << "Size: " << m_Size << std::endl;
os << indent << "SmallBlock: " << (m_SmallBlock ? "On" : "Off") << std::endl;

os << indent << "GPUResult: " << static_cast<typename NumericTraits<TElement>::PrintType>(m_GPUResult) << std::endl;
os << indent << "CPUResult: " << static_cast<typename NumericTraits<TElement>::PrintType>(m_CPUResult) << std::endl;
}

template <typename TElement>
Expand Down
16 changes: 10 additions & 6 deletions Modules/Core/GPUCommon/src/itkGPUDataManager.cxx
Expand Up @@ -264,12 +264,16 @@ GPUDataManager::Initialize()
void
GPUDataManager::PrintSelf(std::ostream & os, Indent indent) const
{
os << indent << "GPUDataManager (" << this << ')' << std::endl;
os << indent << "m_BufferSize: " << m_BufferSize << std::endl;
os << indent << "m_IsGPUBufferDirty: " << m_IsGPUBufferDirty << std::endl;
os << indent << "m_GPUBuffer: " << m_GPUBuffer << std::endl;
os << indent << "m_IsCPUBufferDirty: " << m_IsCPUBufferDirty << std::endl;
os << indent << "m_CPUBuffer: " << m_CPUBuffer << std::endl;
Superclass::PrintSelf(os, indent);

os << indent << "BufferSize: " << m_BufferSize << std::endl;
os << indent << "ContextManager: " << m_ContextManager << std::endl;
os << indent << "CommandQueueId: " << m_CommandQueueId << std::endl;
os << indent << "MemFlags: " << m_MemFlags << std::endl;
os << indent << "GPUBuffer: " << m_GPUBuffer << std::endl;
os << indent << "CPUBuffer: " << m_CPUBuffer << std::endl;
os << indent << "IsGPUBufferDirty: " << (m_IsGPUBufferDirty ? "On" : "Off") << std::endl;
os << indent << "IsCPUBufferDirty: " << m_IsCPUBufferDirty ? "On" : "Off") << std::endl;
}

} // namespace itk
Expand Up @@ -294,22 +294,20 @@ GPUFiniteDifferenceImageFilter<TInputImage, TOutputImage, TParentImageFilter>::P
Indent indent) const
{
GPUSuperclass::PrintSelf(os, indent);
CPUSuperclass::PrintSelf(os, indent);
/*
os << indent << "UseImageSpacing: " << ( m_UseImageSpacing ? "On" : "Off" ) << std::endl;
os << indent << "State: " << m_State << std::endl;
os << std::endl;
if ( m_DifferenceFunction )
{
os << indent << "DifferenceFunction: " << std::endl;
m_DifferenceFunction->Print( os, indent.GetNextIndent() );
}
else
{
os << indent << "DifferenceFunction: " << "(None)" << std::endl;
}
os << std::endl;
*/

os << indent << "InitTime: " << static_cast<typename NumericTraits<TimeProbe>::PrintType>(m_InitTime) << std::endl;
os << indent
<< "ComputeUpdateTime: " << static_cast<typename NumericTraits<TimeProbe>::PrintType>(m_ComputeUpdateTime)
<< std::endl;
os << indent << "ApplyUpdateTime: " << static_cast<typename NumericTraits<TimeProbe>::PrintType>(m_ApplyUpdateTime)
<< std::endl;
os << indent << "SmoothFieldTime: " << static_cast<typename NumericTraits<TimeProbe>::PrintType>(m_SmoothFieldTime)
<< std::endl;

itkPrintSelfObjectMacro(DifferenceFunction);

os << indent << "UseImageSpacing: " << (m_UseImageSpacing ? "On" : "Off") << std::endl;
os << indent << "State: " << m_State << std::endl;
}

} // end namespace itk
Expand Down

0 comments on commit c47ed1c

Please sign in to comment.