[Insight-users] itk::SmartPointers and STL?

Luis Ibanez luis.ibanez@kitware.com
Wed, 19 Mar 2003 12:02:50 -0500


Hi Parag,

You can safely use SmartPointers as elements of an STL
container. This is done in ITK in the itk::ProcessObject
for example, in order to hold the list of input and output
ObjectData elements.

For example:

typedef itk::DataObject::Pointer   ObjectDataPointer;

typedef std::vector< ObjectDataPointer > ObjectDataContainer;

The copy semantics of STL shouldn't be a problem since
at each copy, the SmartPointers will update the reference
count of their respective objects.

The VectorContainer interface is basically a std::vector
that has been ITK-asimilated. So, it will also manage
smart pointer elements correctly.

The only concern you may have with this is: performance.

If you actually plan to manipulate thousands of those
elements, or anticipate to make many copies of the
container.

The reason for having performance concerns is that in
order to be thread-safe, the update of the reference
count is currently placed inside a Mutex section.
The mutex lock is a slow process, and you will have to
pay for this overhead every time one SmartPointer is
copied into another one.



Please let us know if you have any other questions,



Thanks


    Luis


--------------------
Parag Chandra wrote:
> Hi all,
>  
> I need to maintain lists of ITK classes (via corresponding 
> SmartPointers) in my application. I thought I remember something about 
> STL and SmartPointers not behaving well because of copy semantics, so 
> what is the approach I should take to putting ITK classes into STL 
> containers? Does the itkVectorContainerInterface automatically handle 
> these details for me? Thanks.
>  
> Best regards,
> -Parag Chandra
>