[vtkusers] A template to avoid calling vtkObjectBase->Delete()

REGAT-BARREL Aurélien arbvtk at yahoo.fr
Wed Apr 28 11:13:44 EDT 2004


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 !
Créez votre Yahoo! Mail

Dialoguez en direct avec vos amis grâce à Yahoo! Messenger !
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20040428/26cddf0f/attachment.htm>


More information about the vtkusers mailing list