[Insight-developers] Templates and preprocessor macros

Benoit Scherrer benoitscherrer at gmail.com
Wed Jul 20 11:14:39 EDT 2011


Hi,

Wow thank you very much. I think that's a perfect way to do what i wanted to
do.

Thanks again!!
Benoit


On Wed, Jul 13, 2011 at 12:24 PM, Williams, Norman K <
norman-k-williams at uiowa.edu> wrote:

>  I think what Benoit wants is to execute different processing steps based
> on ImageType::ImageDimension.
>
>  What seems like you'd want -- specialization of template member function,
> isn't legal C++.  And if partial specialization of non-class helper
> functions works, I haven't figured out the syntax recipe for it.
>
>  Here, though is a mockup of how you could do it with member function
> overloads:
>
>   /*
>  * Demonstrate using template + overload pattern to
>  * handle different ImageDimension cases differently
>  */
> #include <iostream>
> #include <itkImage.h>
>
>  template <typename TImage>
> class TestClass
> {
> public:
>   typedef TImage ImageType;
>   typedef typename ImageType::Pointer ImagePointer;
>   void Execute();
> private:
>   /** fallback case -- doesn't do anything */
>   template <typename TImage2>
>   ImagePointer _Execute(TImage2 *input);
>   /** specifically handle 2D */
>   ImagePointer _Execute(typename itk::Image<typename TImage::PixelType,2>
> *input);
>   /** specifically handle 3D */
>   ImagePointer _Execute(typename itk::Image<typename TImage::PixelType,3>
> *input);
> };
>
>
>  /** ND case */
> template <class TImage>
> template <class TImage2>
> typename TestClass<TImage>::ImagePointer
> TestClass<TImage>
> ::_Execute(TImage2 *input)
> {
>   std::cerr << "Unsupported Dimension" << std::endl;
>   return input;
> }
>
>  /** 2D Case */
> template <class TImage>
> typename TestClass<TImage>::ImagePointer
> TestClass<TImage>
> ::_Execute(typename itk::Image<typename TImage::PixelType,2> *input)
> {
>   std::cerr << "2D" << std::endl;
>   ImagePointer output;
>   // do 2D processing
>   return output;
> }
>
>  /** 3D Case */
> template <class TImage>
> typename TImage::Pointer
> TestClass<TImage>
> ::_Execute(typename itk::Image<typename TImage::PixelType,3> *input)
> {
>   std::cerr << "3D" << std::endl;
>   ImagePointer output;
>   // do 3D processing
>   return output;
> }
>
>  template <class TImage>
> void
> TestClass<TImage>
> ::Execute()
> {
>   ImagePointer p;
>   this->_Execute(p.GetPointer());
> }
>
>  int main(int argc, char *argv[])
> {
>   typedef itk::Image<unsigned char,4> Image4DType;
>   typedef itk::Image<unsigned char,3> Image3DType;
>   typedef itk::Image<unsigned char,2> Image2DType;
>
>    TestClass<Image4DType> test4D;
>   TestClass<Image3DType> test3D;
>   TestClass<Image2DType> test2D;
>   test4D.Execute();
>   test3D.Execute();
>   test2D.Execute();
> }
>
>
>  On 7/12/11 8:48 PM, "Bradley Lowekamp" <blowekamp at mail.nih.gov> wrote:
>
>  With C++0x there is the static_assert:
>
>  http://en.wikipedia.org/wiki/C%2B%2B0x#Static_assertions
>
>  Which is by far the easiest was to check what you are looking for, as it
> is just :
>
>  static_assert( VImageDimension == 3 );
>
>  *poof* compile time assertion. However, this is not supported by all
> compilers that ITK needs to support so we can't do that for the main code.
> But if it's just your own project you could use it.
>
>  I would be  in favor for adding a itkStaticAssert as a macro to ITKv4.
> But I am also a meta-programing wacko, so I may be alone. We could use the
> C++0x implementation if available or fall back to something else, or just do
> nothing.
>
>  Brad
>
>  On Jul 11, 2011, at 1:28 PM, Williams, Norman K wrote:
>
>  The only way to solve this is with template metaprogramming.  This makes
> for some extra work getting the syntax right, but you can write the code
> so that it only uses a filter on 3D.
>  On the other hand, as one of the people who worked on OrientImageFilter
> back in the day, I don't think anyone should be using OrientImageFilter
> and it should be deprecated, because it destroys information. It only does
> the right thing if all of the direction cosines are aligned with one of
> the world coordinate axes.
>   On 7/11/11 11:10 AM, "Benoit Scherrer" <benoitscherrer at gmail.com> wrote:
>
> Hi,
>  Is there a way to use the C++ preprocessor macros that test the value of
> a template parameter?
> For example i want to do a generic image filter ( using itk::Image<type,
> VImageDimension> ) that
> will use itk::OrientImageFilter only for VImageDimension==3
>  However, it seems that the preprocessor tool does not 'expand' the
> templates and
> just don't know when VImageDimension==3 or not.
>  Is there any trick to do that, or is it a lost cause ?
>  Thanks a lot!
>  Benoit
> _______________________________________________
> Powered by www.kitware.com
>  Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>  Kitware offers ITK Training Courses, for more information visit:
> http://kitware.com/products/protraining.html
>  Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
>  Follow this link to subscribe/unsubscribe:
> http://www.itk.org/mailman/listinfo/insight-developers
>
>   ________________________________
> Notice: This UI Health Care e-mail (including attachments) is covered by
> the Electronic Communications Privacy Act, 18 U.S.C. 2510-2521, is
> confidential and may be legally privileged.  If you are not the intended
> recipient, you are hereby notified that any retention, dissemination,
> distribution, or copying of this communication is strictly
> prohibited.  Please reply to the sender that you have received the message
> in error, then delete it.  Thank you.
> ________________________________
> _______________________________________________
> Powered by www.kitware.com
>  Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>  Kitware offers ITK Training Courses, for more information visit:
> http://kitware.com/products/protraining.html
>  Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
>  Follow this link to subscribe/unsubscribe:
> http://www.itk.org/mailman/listinfo/insight-developers
>
>
>
>
>
> ------------------------------
> Notice: This UI Health Care e-mail (including attachments) is covered by
> the Electronic Communications Privacy Act, 18 U.S.C. 2510-2521, is
> confidential and may be legally privileged.  If you are not the intended
> recipient, you are hereby notified that any retention, dissemination,
> distribution, or copying of this communication is strictly prohibited.
> Please reply to the sender that you have received the message in error, then
> delete it.  Thank you.
> ------------------------------
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/mailman/private/insight-developers/attachments/20110720/3f74bf38/attachment.htm>


More information about the Insight-developers mailing list