[Insight-developers] concept checking macros

Joshua Cates cates@sci.utah.edu
Mon, 13 Jan 2003 16:03:23 -0700 (MST)


Hello,

I'm trying to use the itk concept checking macro to restrict some filter
output image type to floating point.  I have two ideas for doing this and
would like some input on what people think looks better--or other ideas.  
Perhaps there is even a way to do this with Concepts which have already
defined?



1) Add to itkConceptChecking.h

/** Concept requiring T1 to be a floating point type. */
template <typename T1>
struct IsFloating
{
  struct Constraints
  {
    void constraints()
    {
      a = std::_Is_integer<T1>();
      Detail::IgnoreUnusedVariable(a);
    }
    std::__false_type a;
  };
  itkConceptConstraintsMacro();
};


2) Add a typedef void IsFloating to numeric traits classes for float, 
double, long double.

Then add to itkConceptChecking.h

template <typename T1>
struct IsFloating
{
  struct Constraints
  {
    void constraints()
    {
      typename T1::IsFloating a;
      Detail::IgnoreUnusedVariable(a);
    } 
  };
  itkConceptConstraintsMacro();
};


Here is my test program:

-----------------------------------------------------------------
#include "itkImageToImageFilter.h"
#include "itkImage.h"

namespace itk {

template <class TInputImage, class TOutputImage>
class ConceptChecker : public ImageToImageFilter<TInputImage, 
TOutputImage>
{
public:
  /** Standard "Self" & Superclass typedef. */
  typedef ConceptChecker Self;
  typedef ImageToImageFilter< TInputImage, TOutputImage > Superclass;
  typedef       SmartPointer<Self> Pointer;
  typedef SmartPointer<const Self> ConstPointer;

  /** Method for creation through the object factory. */
  itkNewMacro(Self);

  /** Run-time type information (and related methods). */
  itkTypeMacro(ConceptChecker, ImageToImageFilter);

  /** Extract some information from the image types.  Dimensionality
   * of the two images is assumed to be the same. */
  itkStaticConstMacro(ImageDimension, unsigned int,
                      TOutputImage::ImageDimension);

  typedef typename TOutputImage::PixelType OutputPixelType;
  
  /** Concept checking.  Ensure that the output pixel type is a floating  point type. */
  itkConceptMacro(PixelTypeIsFloating, (Concept::IsFloating<OutputPixelType>));

};

}// end namespace itk


struct bad_pixel_type
{    int i; };

int main()
{
  
  typedef itk::Image<float, 2> FloatImageType;
  typedef itk::Image<bad_pixel_type, 2> BadImageType;

  itk::ConceptChecker<FloatImageType, BadImageType>::Pointer c
    = itk::ConceptChecker<FloatImageType, BadImageType>::New();
  
  return 0;
}
-----------------------------------------------------------------------------------------

Option 1 gives output which I think folks will find confusing:

/home/cates/Insight/Code/Common/itkConceptChecking.h: In method `void
itk::Concept::IsFloating<T1>::Constraints::constraints () [with T1 = 
bad_pixel_type]':
/home/cates/Insight/Code/Common/itkConceptChecking.h:354:   instantiated 
from `itk::Concept::IsFloating<bad_pixel_type>'
/home/cates/SampleProject/concept.cxx:48:   instantiated from 
`itk::ConceptChecker<main ()::FloatImageType, main ()::BadImageType>'
/home/cates/SampleProject/concept.cxx:48:   instantiated from here
/home/cates/Insight/Code/Common/itkConceptChecking.h:349: no match for 
`__false_type & =
_Is_integer<bad_pixel_type>'
/usr/include/g++-3/type_traits.h:59: candidates are: __false_type
&__false_type::operator= (const __false_type &)

Also I do not know if <type_traits.h> is standard on all platforms.


Option 2 gives output which is maybe only slightly less confusing:

/home/cates/Insight/Code/Common/itkConceptChecking.h: In method `void
itk::Concept::IsFloating<T1>::Constraints::constraints () [with T1 = 
bad_pixel_type]':
/home/cates/Insight/Code/Common/itkConceptChecking.h:368:   instantiated 
from `itk::Concept::IsFloating<bad_pixel_type>'
/home/cates/SampleProject/concept.cxx:48:   instantiated from 
`itk::ConceptChecker<main ()::FloatImageType, main ()::BadImageType>'
/home/cates/SampleProject/concept.cxx:48:   instantiated from here
/home/cates/Insight/Code/Common/itkConceptChecking.h:364: no type named 
`IsFloating' in `struct
bad_pixel_type'
/home/cates/Insight/Code/Common/itkConceptChecking.h:365: `a' undeclared 
(first use this function)
/home/cates/Insight/Code/Common/itkConceptChecking.h:365: (Each undeclared 
identifier is reported only


______________________________
 Josh Cates			
 School of Computer Science	
 University of Utah
 Email: cates@sci.utah.edu
 Phone: (801) 587-7697
 URL:   http://www.sci.utah.edu/~cates