[vtk-developers] Smart pointer declaration macro?

Marcus D. Hanwell marcus.hanwell at kitware.com
Fri Jan 8 16:08:34 EST 2010


On Friday 08 January 2010 15:52:40 David Cole wrote:
> On Fri, Jan 8, 2010 at 3:41 PM, David Gobbi <david.gobbi at gmail.com> wrote:
> > On Fri, Jan 8, 2010 at 1:38 PM, David Doria
> > <daviddoria+vtk at gmail.com<daviddoria%2Bvtk at gmail.com>>
> > >> On Fri, Jan 8, 2010 at 3:26 PM, David Gobbi <david.gobbi at gmail.com>
> >
> > wrote:
> > >>> On Fri, Jan 8, 2010 at 1:15 PM, David Doria
> > >>> <daviddoria+vtk at gmail.com<daviddoria%2Bvtk at gmail.com>>
> >
> > wrote:
> > >>>> I've seen the use of something like this:
> > >>>>
> > >>>> #define SPNEW(instance, type) \
> > >>>> vtkSmartPointer<type> instance = vtkSmartPointer<type>::New();
> > >>>>
> > >>>> in many of the tests and elsewhere. It seems like a reasonable
> > >>>> savings of a whole bunch of characters that appears many many times
> > >>>> in most functions. Could we standardize something like this so it
> > >>>> can be used universally without having to see this little #define in
> > >>>> every file it is used in?
> > >>>
> > >>> Doesn't VTK have enough macros already? ;)
> > >>>
> > >>> There are other ways to reduce the repetition, without using macros:
> > >>>
> > >>> 1) Add a new constructor argument for smart pointers:
> > >>>
> > >>> vtkSmartPointer<type> instance(1); // create an smart pointer and
> > >>> allocate an object at the same time
> > >>> vtkSmartPointer<type> instance(0); // create a smart pointer with
> > >>> "null" as the initial pointer
> > >>>
> > >>> 2) Add a non-static "New" method for smart pointers:
> > >>>
> > >>> vtkSmartPointer<type> instance;
> > >>> instance.InstantiateNew();
> > >>>
> > > Sure, I hate macros :)
> > >
> > > So how do we turn these good suggestions into a final conclusion and
> > > course of action?
> > >
> > > My choice/thought after seeing these initial comments is that it would
> > > be nice if ITK and VTK shared a similar style (the ::SmartPointer).
> > >
> > > Bill - there is plenty of mystery even with the additional typing :)
> >
> > Actually, I'll give a "+1" to Bill's comment because I do like code to
> > be as explicit as possible.
> 
> Me too. +1.
> 
> Which sort of means eliminating all the existing SPNEW macros from test
> files, doesn't it...? :-)
> 
I would like to add my -1, I think needing to split lines in order to declare 
a new local variable is a little much. I came from a C++ background where any 
object could be declared on the stack though. For things like the examples it 
seems to hurt readability to me.

Pointer:
vtkFloatArray *myTable = vtkFloatArray::New();
myTable->Delete();
myTable = NULL;

Smart pointer:
vtkSmartPointer<vtkFloatArray> myTable =  
     vtkSmartPointer<vtkFloatArray>::New();

Smart pointer with macro:
VTK_CREATE(vtkFloatArray, myTable);

Stack:
vtkFloatArray myTable;

I would prefer to be able to use something like the first or the last. In 
classes etc it is often a different story. It seems like there should be some 
macro or template function to generate variables with less repetition.

Just my $0.02...
-- 
Marcus D. Hanwell, Ph.D.
R&D Engineer, Kitware Inc.
(518) 881-4937



More information about the vtk-developers mailing list