VTK
dox/Common/Core/vtkNew.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkNew.h
00005 
00006   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00007   All rights reserved.
00008   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00009 
00010      This software is distributed WITHOUT ANY WARRANTY; without even
00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00012      PURPOSE.  See the above copyright notice for more information.
00013 
00014 =========================================================================*/
00058 #ifndef __vtkNew_h
00059 #define __vtkNew_h
00060 
00061 #include "vtkIOStream.h"
00062 
00063 class vtkObjectBase;
00064 
00065 template <class T>
00066 class vtkNew
00067 {
00069 
00070   void CheckObjectBase(vtkObjectBase*) {}
00071 public:
00073 
00074 
00075   vtkNew() : Object(T::New())
00076     {
00077     this->CheckObjectBase(this->Object);
00078     }
00080 
00082 
00083   ~vtkNew()
00084     {
00085     T* obj = this->Object;
00086     if (obj)
00087       {
00088       this->Object = 0;
00089       obj->Delete();
00090       }
00091     }
00093 
00095 
00097   T* operator->() const
00098     {
00099     return this->Object;
00100     }
00102 
00104 
00108   T* GetPointer() const
00109     {
00110     return this->Object;
00111     }
00113 
00114 private:
00115   vtkNew(vtkNew<T> const&); // Not implemented.
00116   void operator=(vtkNew<T> const&);   // Not implemented.
00117   T* Object;
00118 };
00119 
00121 
00122 template <class T>
00123 inline ostream& operator << (ostream& os, const vtkNew<T>& p)
00124 {
00125   return os << p.GetPointer();
00126 }
00128 
00129 #endif
00130 // VTK-HeaderTest-Exclude: vtkNew.h