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:
00069   // Description:
00070   // Create a new T on construction.
00071   vtkNew() : Object(T::New())
00072     {
00073     this->CheckObjectBase(this->Object);
00074     }
00076 
00078 
00079   ~vtkNew()
00080     {
00081     T* obj = this->Object;
00082     if (obj)
00083       {
00084       this->Object = 0;
00085       obj->Delete();
00086       }
00087     }
00089 
00091 
00093   T* operator->() const
00094     {
00095     return this->Object;
00096     }
00098 
00100 
00104   T* GetPointer() const
00105     {
00106     return this->Object;
00107     }
00109 
00110 private:
00111   vtkNew(vtkNew<T> const&); // Not implemented.
00112   void operator=(vtkNew<T> const&);   // Not implemented.
00113   T* Object;
00114 };
00115 
00117 
00118 template <class T>
00119 inline ostream& operator << (ostream& os, const vtkNew<T>& p)
00120 {
00121   return os << p.GetPointer();
00122 }
00124 
00125 #endif