[Insight-developers] CastWithRangeCheck implmentation and usage

Bradley Lowekamp blowekamp at mail.nih.gov
Thu Dec 17 17:43:33 EST 2009


Hello,

I was thinking that this newly created method, needs some work before it's used much further. This is the implementation that I was thinking about:

template <typename TType>
inline TType CastWithRangeCheck( TType x )
{
  itkConceptMacro( OnlyDefinedForIntegerTypes, (Concept::Integer<TType>) );
  return x;
}
template <typename TReturn,typename TInput>
inline TReturn CastWithRangeCheck(TInput x)
{
  itkConceptMacro( OnlyDefinedForIntegerTypes1, (Concept::Integer<TReturn>) );
  itkConceptMacro( OnlyDefinedForIntegerTypes2, (Concept::Integer<TInput>) );
  TReturn ret = static_cast<TReturn>(x);
  if ( ret != x ||
       ( NumericTraits<TInput>::IsPositive(x) != NumericTraits<TReturn>::IsPositive(x) ) )
    {
    // this should likely be a new exception class
    RangeError _e(__FILE__, __LINE__);
    throw _e;
    }
  return ret;
}


What I had not considered was that this method has started to already be used to convert double to floats. I believe that we should fill in the behavior of this methods relatively soon.

Brad

========================================================
Bradley Lowekamp  
Lockheed Martin Contractor for
Office of High Performance Computing and Communications
National Library of Medicine 
blowekamp at mail.nih.gov


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/mailman/private/insight-developers/attachments/20091217/5a858d74/attachment.htm>


More information about the Insight-developers mailing list