[vtkusers] A template to avoid calling vtkObjectBase->Delete()
William A. Hoffman
billlist at nycap.rr.com
Wed Apr 28 11:42:23 EDT 2004
In cvs VTK there is a class called vtkSmartPointer that does just this.
It is used like this:
#include "vtkSmartPointer.h"
....
vtkSmartPointer<vtkPoints> outputPoints = vtkSmartPointer<vtkPoints>::New();
-Bill
At 11:13 AM 4/28/2004, REGAT-BARREL Aurélien wrote:
>Hello world,
>In C++, the need to keep track of created objects in order to call Delete on exit is cumbersome.
>So, I plan to use a small template to avoid calling Delete.
>I would like your opinion :
>
>#ifndef __vtkClassPtr_h
>#define __vtkClassPtr_h
>template<class T>
>class vtkClassPtr
>{
>public:
> vtkClassPtr( T * O ) : ptr( O ) {}
> ~vtkClassPtr() { this->ptr->Delete(); }
> operator T*() const { return this->ptr; }
> T& operator*() const { return *this->ptr; }
> T* operator->() const { return this->ptr; }
> vtkClassPtr( const vtkClassPtr & P )
> {
> this->ptr = P.ptr;
> P.ptr->Register( this->ptr );
> }
>
>private:
> T * ptr;
>};
>#endif
>------------------------------------------------------------------------
>Now, have a look at the adapted Cone.cxx example :
>
>#include "vtkConeSource.h"
>#include "vtkPolyDataMapper.h"
>#include "vtkRenderWindow.h"
>#include "vtkRenderWindowInteractor.h"
>#include "vtkCamera.h"
>#include "vtkActor.h"
>#include "vtkRenderer.h"
>#include "vtkInteractorStyleTrackballCamera.h"
>
>#include "vtkClassPtr.h"
>typedef vtkClassPtr< vtkConeSource > vtkConeSourcePtr;
>typedef vtkClassPtr< vtkPolyDataMapper > vtkPolyDataMapperPtr;
>typedef vtkClassPtr< vtkActor > vtkActorPtr;
>typedef vtkClassPtr< vtkRenderer > vtkRendererPtr;
>typedef vtkClassPtr< vtkRenderWindow > vtkRenderWindowPtr;
>typedef vtkClassPtr< vtkRenderWindowInteractor > vtkRenderWindowInteractorPtr;
>typedef vtkClassPtr< vtkInteractorStyleTrackballCamera > vtkInteractorStyleTrackballCameraPtr;
>
>int main( int argc, char *argv[] )
>{
> vtkConeSourcePtr cone = vtkConeSource::New();
> {
> vtkConeSourcePtr cone2 = cone;
> cone2->SetHeight( 3.0 );
> cone2->SetRadius( 1.0 );
> cone2->SetResolution( 10 );
> }
> vtkPolyDataMapperPtr coneMapper = vtkPolyDataMapper::New();
> coneMapper->SetInput( cone->GetOutput() );
> vtkActorPtr coneActor = vtkActor::New();
> coneActor->SetMapper( coneMapper );
> vtkRendererPtr ren1= vtkRenderer::New();
> ren1->AddActor( coneActor );
> ren1->SetBackground( 0.1, 0.2, 0.4 );
> vtkRenderWindowPtr renWin = vtkRenderWindow::New();
> renWin->AddRenderer( ren1 );
> renWin->SetSize( 300, 300 );
> vtkRenderWindowInteractorPtr iren = vtkRenderWindowInteractor::New();
> iren->SetRenderWindow(renWin);
> vtkInteractorStyleTrackballCameraPtr style =
> vtkInteractorStyleTrackballCamera::New();
> iren->SetInteractorStyle(style);
> iren->Initialize();
> iren->Start();
> return 0;
>}
>
>
>
>Aurélien REGAT-BARREL
>
>
>Yahoo! Mail : votre e-mail personnel et gratuit qui vous suit partout !
><http://fr.rd.yahoo.com/mail/taglines/*http://fr.rd.yahoo.com/mail/mail_taglines/default/*http://fr.benefits.yahoo.com/>Créez votre Yahoo! Mail
>
>Dialoguez en direct avec vos amis grâce à <http://fr.rd.yahoo.com/mail/taglines/*http://fr.rd.yahoo.com/messenger/mail_taglines/default/*http://fr.messenger.yahoo.com/>Yahoo! Messenger !
More information about the vtkusers
mailing list