vtk::MakeSmartPointer(T*), vtk::TakeSmartPointer(T*)

proposal
(Allison Vacanti) #1

One of the remaining tedious parts of using vtkSmartPointer is their creation. For instance, consider creating a new vtkCompositeDataSetIterator from vtkCompositeDataSet::NewIterator() and putting it into a smart pointer. There are two ways of doing this currently:

vtkSmartPointer<vtkCompositeDataSetIterator> cdsIter;
cdsIter.TakeReference(cds->NewIterator)();

or

auto cdsIter = 
  vtkSmartPointer<vtkCompositeDataSetIterator>::Take(cds->NewIterator());

This merge request adds a couple of new functions that are similar to std::make_shared, but for vtkSmartPointer and vtkWeakPointer. This allows the above examples to be written much more concisely:

// Equivalent to the above examples.
auto cdsIter = vtk::TakeSmartPointer(cds->NewIterator());

The new methods are:

  • vtk::TakeSmartPointer(T *obj) takes an instance of type T (a subclass of vtkObject), and returns a vtkSmartPointer<T> without adding a new reference. That is, it “takes” the existing reference.
  • vtk::MakeSmartPointer(T *obj) is similar, but it “makes” a new reference to obj.
  • vtk::TakeWeakPointer(T *obj) creates a new vtkWeakPointer<T> that references obj.

I’ve found these (especially TakeSmartPointer to be very useful, would others like to see these added? Comments and reviews are welcome.