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 typeT(a subclass ofvtkObject), and returns avtkSmartPointer<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 toobj. -
vtk::TakeWeakPointer(T *obj)creates a newvtkWeakPointer<T>that referencesobj.
I’ve found these (especially TakeSmartPointer to be very useful, would others like to see these added? Comments and reviews are welcome.