VTK
dox/Common/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 =========================================================================*/
00055 #ifndef __vtkNew_h
00056 #define __vtkNew_h
00057 
00058 #include "vtkIOStream.h"
00059 
00060 class vtkObjectBase;
00061 
00062 template <class T>
00063 class vtkNew
00064 {
00066 
00067   void CheckObjectBase(vtkObjectBase*) {}
00068 public:
00070 
00071 
00072   vtkNew() : Object(T::New())
00073     {
00074     this->CheckObjectBase(this->Object);
00075     }
00077 
00079 
00080   ~vtkNew()
00081     {
00082     T* obj = this->Object;
00083     if (obj)
00084       {
00085       this->Object = 0;
00086       obj->Delete();
00087       }
00088     }
00090 
00092 
00094   T* operator->() const
00095     {
00096     return this->Object;
00097     }
00099 
00101 
00105   T* GetPointer() const
00106     {
00107     return this->Object;
00108     }
00110 
00111 private:
00112   vtkNew(vtkNew<T> const&); // Not implemented.
00113   void operator=(vtkNew<T> const&);   // Not implemented.
00114   T* Object;
00115 };
00116 
00118 
00119 template <class T>
00120 inline ostream& operator << (ostream& os, const vtkNew<T>& p)
00121 {
00122   return os << p.GetPointer();
00123 }
00125 
00126 #endif