[Insight-users] Forward references to ITK classes

Miller, James V (Research) millerjv@crd.ge.com
Tue, 1 Apr 2003 09:00:55 -0500


Paul,

There is a class called SmartPointerForwardReference that may help.
It is used internally so a DataObject can point back to a ProcessObject.
Read the docs in the header carefully, this class works a little different
than most of the files in ITK.  In particular, you include the .h file
wherever you want to declare the forward reference and you include the .txx
wherever you want to use the forward reference.

Jim



> -----Original Message-----
> From: Luis Ibanez [mailto:luis.ibanez@kitware.com]
> Sent: Monday, March 31, 2003 11:06 PM
> To: Paul Yushkevich
> Cc: insight
> Subject: Re: [Insight-users] Forward references to ITK classes
> 
> 
> Hi Paul,
> 
> I would suggest to avoid using SmartPointers as part
> of the forward referencing. If you only use raw pointers
> things may be easier since after all a forward referenced
> pointer is treated as a generic pointer.
> 
> In practice in an application, what you may want to do
> is to define a spacific image type to work with, and then
> wrap the templates inside a non-templated class.
> 
> 
> About your last questions:
> 
> 1) Not to my knowledge, you seem to be the first
>     trying to explore this territory...
> 2) Well, on way may be to instantiate images of specific
>     types. E.g. define a class Image3DF which is Image<float,3>
>     and then only forward reference this class.
> 3) If you can suggest a modification, we are willing to
>     try it   :-)
>     I will rather suggest to avoid using SmartPointers
>     in your non-templated code.
> 4) The manual instantiation flag that is present in
>     al the ITK files has never been tested in multiple
>     systems (AFAIK). It was designed with multiplatform
>     in mind but according the ad hoc rule :
> 
>     "If it is not build in the dashboard: it is broken".
> 
> 
> 
> 
> Regards,
> 
> 
> Luis
> 
> 
> --------------------------------------------
> Paul Yushkevich wrote:
> > I have a [rather long] question about using forward 
> references to ITK 
> > classes.  My guess is that I am not the first person to 
> face this issue, 
> > but I have not seen any questions regarding this on the 
> mailing list.
> > 
> > Suppose I have the following situation:
> > 
> > A templated class Wrapper<TPixel> encapsulates a generic image 
> > processing task and contains pointers to multiple images 
> and filters. 
> > This class is used throughout a large application.  The class is 
> > declared in the header file "Wrapper.h", and this file is 
> included by 
> > many other files in the application.
> > 
> > In order to reduce compilation time, I would like to use forward 
> > references to ITK classes needed to declare the class 
> Wrapper in the 
> > header "Wrapper.h".  The following declaration is possible:
> > 
> > // Forward references, no need to include ITK headers here
> > namespace itk {
> >    template <int i, class T> class Image;
> >    template <class TInput, class TOutput> class SomeFilter;
> > };
> > 
> > template <class TPixel> class Wrapper {
> >    typedef itk::Image<3,TPixel> ImageType; // and so on
> > private:
> >    ImageType *m_FirstImage,*m_SecondImage; // and so on
> > };
> > 
> > This works fine but... this declaration does not use 
> SmartPoitners.  As 
> > soon as I try to use smart pointers, foward referencing 
> seems to break.  
> > A declaration of type
> > 
> >    typedef typename ImageType::Pointer ImagePointer;
> > 
> > does not work because the compiler does not know that 
> ::Pointer is part 
> > of class itk::Image.  Neither does the following declaration, which 
> > makes a forward reference to itk::SmartPointer:
> > 
> > namespace itk {
> >    ...
> >    template <class T> class SmartPointer;
> > };
> > ...
> >    typedef typename itk::SmartPointer<ImageType> ImagePointer;
> > ...
> > 
> > This does not work because the compiler can not allocate room for 
> > smartpointers without having seen them declared. If I do 
> include the 
> > header itkSmartPointer.h, as in the following example, the 
> code still 
> > does not work
> > 
> > #include <itkSmartPointer.h>
> > namespace itk {
> >    template <int i, class T> class Image;
> >    template <class TInput, class TOutput> class SomeFilter;
> > };
> > 
> > template <class TPixel> class Wrapper {
> >    typedef itk::Image<3,TPixel> ImageType;
> >    typedef typename itk::SmartPointer<ImageType> ImagePointer;
> > private:
> >    ImagePointer m_FirstImage,m_SecondImage; // and so on
> > };
> > 
> > This code does not compile (at least with VC60), and I get 
> an error in 
> > itkSmartPoitner.h in the function Unregister().  This 
> inline function 
> > can not be compiled using forward references to the class 
> over which the 
> > SmartPointer is templated (in this case, itk::Image).  If I 
> comment out 
> > the code in this function, the code does compile.  Curiously, the 
> > function Register() compiles just fine; the difference between them 
> > seems to come from the fact that UnRegister is used in the 
> desctructor 
> > ~SmartPointer().
> > Finally, I tried another solution, which does not use forward 
> > references.  I declared Wrapper as an abstract class with 
> only virtual 
> > members and no attributes.  I declared a class 
> WrapperImplementation 
> > that iherits from Wrapper and declares all the ITK 
> SmartPointers and 
> > stuff.  The header for WrapperImplementation includes all the ITK 
> > headers.  I included "WrapperImplementation.h" only in the 
> places in the 
> > code where new instances of WrapperImplementation are 
> created.   This 
> > solution does not work well if I want to have an 
> inheritance structure 
> > rooted at Wrapper: e.g., classes ColorWrapper and GreyWrapper that 
> > extend WrapperImplementation.  In order to declare these 
> classes, I have 
> > to include "WrapperImplementation.h", and in order to use them 
> > throughout the application I would again end up including 
> ITK headers in 
> > many places in the application.
> > 
> > So that's the dilemma, and my questions are
> > 
> > 1. Has anyone been using forward references to ITK classes 
> successfully?
> > 2. Is there another way to avoid having to recompile lots 
> of code when 
> > including ITK headers in widely used headers such as Wrapper.h?
> > 3. Could the SmartPointer.h be modified to allow forward 
> referencing?
> > 4. There is a macro directive in the ITK .h files that 
> allows manual 
> > instantiation of templates.  Can that be used across platforms?
> > 
> > Thank you very much for replying!
> > 
> > Paul.
> > 
> > _______________________________________________
> > Insight-users mailing list
> > Insight-users@public.kitware.com
> > http://public.kitware.com/mailman/listinfo/insight-users
> > 
> 
> 
> 
> _______________________________________________
> Insight-users mailing list
> Insight-users@public.kitware.com
> http://public.kitware.com/mailman/listinfo/insight-users
>