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

Bill Lorensen bill.lorensen at gmail.com
Thu Jan 28 16:56:22 EST 2010


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
>
>



More information about the vtk-developers mailing list