[vtk-developers] vtkNew<> (was: Smart pointer declaration macro?)

pat marion pat.marion at kitware.com
Thu Jan 28 17:31:22 EST 2010


The main difference between vtkNew and vtkSmartPointer is: vtkNew has
no assignment operator or copy constructor.  Removing these features
makes you think about vtkNew in a different way than vtkSmartPointer.
Quoting from Brad:

  A local variable declared with vtkNew<> would create and own one
object for its whole lifetime.


Here is an example using both:


vtkSmartPointer<vtkPolyData> output; // null, no instance allocated

if (extension == ".stl")
  {
  vtkNew<vtkSTLReader> reader; // instance constructed
  reader->SetFileName(filename);
  reader->Update();
  output = reader->GetOutput();
  }
else if (extension = ".vtk")
  {
  vtkNew<vtkPolyDataReader> reader; // instance constructed
  reader->SetFileName(filename);
  reader->Update();
  output = reader->GetOutput();
  }

if (readerOutput)
  {
  cout << "Number of points read:" << readerOutput->GetNumberOfPoints();
  }


Pat

On Thu, Jan 28, 2010 at 5:07 PM, David Gobbi <david.gobbi at gmail.com> wrote:
> Bill,
>
> Yup, that's exactly how it would be used.  It's essentially Marcus'
> original proposal, but re-named and no longer derived from
> vtkSmartPointer in order to avoid certain gotcha's that Brad explained
> but which I didn't fully understand.
>
>   David
>
>
> On Thu, Jan 28, 2010 at 2:56 PM, Bill Lorensen <bill.lorensen at gmail.com> wrote:
>> I admit I don't understand the "how", but the "what" looks great.
>>
>> So,
>> instead of:
>>
>> vtkSmartPointer<vtkImageRectilinearWipe> wipe =
>>  vtkSmartPointer<vtkImageRectilinearWipe>::New();
>>
>> it would be what?
>> vtkNew<vtkImageRectilinearWipe> wipe;
>>
>>
>> On Thu, Jan 28, 2010 at 4:41 PM, Brad King <brad.king at kitware.com> 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).
>>>
>>> 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<>.
>>>
>>> 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>();
>>>
>>> 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.
>>>
>>> -Brad
>>>
>>>
>>> template <class T> class vtkNew
>>> {
>>> public:
>>>  vtkNew(): Object(T::New()) {}
>>>  ~vtkNew() { this->Object->Delete(); }
>>>  T* operator->() const { return this->Object; }
>>>  operator T*() const   { return this->Object; }
>>> private:
>>>  vtkNew(vtkNew<T> const&);         // Not implemented.
>>>  void operator=(vtkNew<T> const&); // Not implemented.
>>>  T* Object;
>>> };
>>>
>>> template <class T>
>>> class vtkSmartPointer: public vtkSmartPointerBase
>>> {
>>>  static T* CheckType(T* t) { return t; }
>>> public:
>>>  ...
>>>  template <class U>
>>>  vtkSmartPointer(const vtkNew<U>& r): vtkSmartPointerBase(CheckType(r)) {}
>>>  ...
>>> };
>>> _______________________________________________
>>> Powered by www.kitware.com
>>>
>>> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>>>
>>> Follow this link to subscribe/unsubscribe:
>>> http://www.vtk.org/mailman/listinfo/vtk-developers
>>>
>>>
>>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtk-developers
>
>



More information about the vtk-developers mailing list