[Insight-developers] SmartPointer and Get/Set Input/Output

Bill Hoffman bill.hoffman at kitware.com
Wed Sep 6 10:05:29 EDT 2000


To sum things up it should be this:

//1. All functions that return a pointer, should return a smart pointer
TImageOutput::Pointer GetOutput();

//2. All functions that accept a pointer should take only a real pointer
// This is to allow automatic casting up the hierarchy 
// The smart pointers will automatically convert to real pointers
// for this call, i.e. you do not have to call GetPointer()
void SetInput(  TImageInput * );  

//3. The only time GetPointer should be called is if you need to do
//   a static_cast<> or dynamic_cast<> on the smartpointer.
SubClass::Pointer p = dynamic_cast<SubClass*>(parentPointer.GetPointer());


Here is what the filter code should look like:
1. typedef   itk::Image< double, 3 >   myImageType;
2. myImageType::Pointer  theImage = myImgeType::New();
3. myFilterType  theFilter = myFilterType::New();
4. theFilter.SetInput (   theImage  );
5. myImageType::Pointer theOutput = theFilter.GetOutput();


1) Do we want to pass images to filters as normal pointers or as
smartpointers ?

Images should be passed in to filters as smartpointers to functions
that take real pointers, the conversion will be automatic.
This is not dangerous, because the smartpointer will have a reference
to the object for at least the scope of the function call.
If the function needs to keep the object longer than its scope,
it will have to assign it to a smartpointer.

2)If we want SmartPointers, should they be passed by copy or by
reference ?
SmartPoitners should almost always be passed by copy.  They are
no bigger than a real pointer, and should be just about as
efficient to copy.


Will and I just checked in a new ProcessObject and ImageSource
that reflect the above.  I have also added more comments to
the SmartPointerTest in Testing/Code/Common.


The only time GetPointer should be called is if you need to do
a static_cast<> or dynamic_cast<> on the smartpointer.





>2) If we want SmartPointers, should they be passed by copy or by

At 09:31 AM 9/6/00 -0400, Luis Ibanez wrote:

>Hi,
>
>I'm still confused about the use of  Normal vs Smart pointers.
>
>This time with the methods SetInput() , and GetOutput() for
>the filters.
>
>Currently they are defined like
>
>TImageOutput   *   GetOutput();
>
>
>Shouldn't  they be:              TImageOutput::Pointer
>GetOutput();   ?

They should be TImageOutput::Pointer GetOutput();

>or maybe:                   const TImageOutput::Pointer & GetOutput();
>?
>
>------
>
>And the SetInput() method it is currently:
>
>void SetInput(  TImageInput * );
>
>shouldn't it be:    void SetInput(             TImageInput::Pointer
>);   ?
>or maybe :            void SetInput(  const TImageInput::Pointer &  );
>?
>
>--------------------------
>
>With the current code, in order to connect
>images to filters we have to do :
>
>1  typedef   itk::Image< double, 3 >   myImageType;
>2  myImageType::Pointer  theImage = myImgeType::New();
>3  myFilterType  theFilter = myFilterType::New();
>4  theFilter.SetInput (   theImage.GetPointer()  );
>5  myImageType::Pointer theOutput = theFilter.GetOutput();
>
>----------------------------
>
>Passing SmartPointers, lines 4 and 5 become:
>
>4  theFilter.SetInput (   theImage  );
>5  myImageType::Pointer theOutput = theFilter.GetOutput().GetPointer();
>
>------------------------------
>
>The points are:
>
>1) Do we want to pass images to filters as normal pointers or as
>smartpointers ?
>2) If we want SmartPointers, should they be passed by copy or by
>reference ?
>
>
>
>Thanks
>
>
>Luis
>
>
>
>
>_______________________________________________
>Insight-developers mailing list
>Insight-developers at public.kitware.com
>http://public.kitware.com/mailman/listinfo/insight-developers 





More information about the Insight-developers mailing list