[vtk-developers] vtkNew<> (was: Smart pointer declaration macro?)
Marcus D. Hanwell
marcus.hanwell at kitware.com
Thu Jan 28 16:53:30 EST 2010
On Thursday 28 January 2010 16:41:46 Brad King wrote:
> David Gobbi wrote:
> > This is reply mainly to Brad about vtkLocalPointer<vtkClass>.
> >
> > I'm not quite sure why having a smart pointer declaration do a
> > allocation is such a bad thing, as long as it is explicit that an
> > object is being allocated. How about if we make the allocation
> > explicit in the name of the template, e.g.
> >
> > 4. vtkNew<vtkClass> object;
> >
> > I know that it appears that the object is being created on the stack,
> > but that shouldn't be an issue because the stl container classes use
> > heap allocation, too.
>
> Nice! It's okay if two conditions are satisfied:
>
> - the allocation is explicit in the name
> - the type is not a smart pointer
>
> If the name "Pointer" is not present then it takes away expectations
> of pointer-like operations (e.g. assignment).
Sounds good to me - I was not set on the name but the functionality. Is this a
reasonable compromise? I certainly give it a two thumbs up ;-)
>
> I propose a vtkNew<> that is not a subclass of vtkSmartPointer and
> has very few operations:
>
> - default constructor (allocate & take-reference)
> - arrow operator -> (access)
> - operator T* (conversion & raw pointer access)
> - destructor (deletes reference)
>
> We would specifically disable other operations:
>
> - no copy constructor
> - no assignment operator=
>
> We could teach vtkSmartPointer<> to construct from vtkNew<>.
That sounds great, and I think it still very much satisfies our initial
requirements, and would be a far better replacement to the existing options
(or a macro) IMHO.
>
> A local variable declared with vtkNew<> would create and own one
> object for its whole lifetime. The object will go away on its
> destruction unless ownership has been shared by assigning the
> object to a real smart pointer. The exact same class could be
> used in place of the vtkNew<> function template I proposed too:
>
> vtkSmartPointer<vtkBase> o = vtkNew<vtkDerived>();
I think this looks great.
>
> In this example the RHS expression creates a temporary instance
> of the vtkNew<> template. The vtkSmartPointer<> initialization
> takes its own reference to the object before the vtkNew<> template
> goes away.
>
> A basic implementation of this vtkNew<> appears below. It would
> just need some tweaking in case the object allocation fails.
> This class template is so simple we could consider defining it in
> a top-level VTK header so that no sources need to include it.
> Alternatively it could be provided in vtkSmartPointer.h.
>
Sounds great. I really like this solution. It would be great to see this make
it into VKT, it would only occupy the same amount of memory as a pointer, and
be very quick to allocate/deallocate. I think this would be a great step
forward.
Are there any objections to using this approach? It seems to address many of
the concerns about other alternatives.
Marcus
More information about the vtk-developers
mailing list