Skip to content

Add helper functions to create smart / weak pointers.

Three new functions have been added to the vtk namespace:

  1. vtkSmartPointer<T> vtk::MakeSmartPointer(T*)
  2. vtkSmartPointer<T> vtk::TakeSmartPointer(T*)
  3. vtkWeakPointer<T> vtk::TakeWeakPointer(T*)

(1) takes an object of type T (which inherits vtkObject), increments its reference count, and returns the object wrapped in a vtkSmartPointer<T>. (2) does the same as (1), but without incrementing the reference count. (3) takes returns the object in a vtkWeakPointer without incrementing the reference count.

These are intended to simplify using VTK's smart pointer with the auto keyword. Now, instead of:

vtkSmartPointer<vtkInsertNameOfSomeVtkObjectHere> smartObj;
smartObj = someObj;

just do:

auto myObj = vtk::MakeSmartPointer(someObj);

The Take variant can be used to handle objects that already have a borrowed reference, such as a cell iterator. Instead of:

auto cellIter = vtkSmartPointer<vtkCellIterator>::Take(myDataSet->NewCellIterator());

or:

vtkSmartPointer<vtkCellIterator> cellIter;
cellIter.TakeReference(myDataSet->NewCellIterator());

just write:

auto cellIter = vtk::TakeSmartPointer(myDataSet->NewCellIterator());

Merge request reports