VTK
vtkNew.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkNew.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
55 #ifndef __vtkNew_h
56 #define __vtkNew_h
57 
58 #include "vtkIOStream.h"
59 
60 class vtkObjectBase;
61 
62 template <class T>
63 class vtkNew
64 {
66 
67  void CheckObjectBase(vtkObjectBase*) {}
68 public:
69  // Description:
70  // Create a new T on construction.
71  vtkNew() : Object(T::New())
72  {
73  this->CheckObjectBase(this->Object);
74  }
76 
78 
80  {
81  T* obj = this->Object;
82  if (obj)
83  {
84  this->Object = 0;
85  obj->Delete();
86  }
87  }
89 
91 
93  T* operator->() const
94  {
95  return this->Object;
96  }
98 
100 
104  T* GetPointer() const
105  {
106  return this->Object;
107  }
109 
110 private:
111  vtkNew(vtkNew<T> const&); // Not implemented.
112  void operator=(vtkNew<T> const&); // Not implemented.
113  T* Object;
114 };
115 
117 
118 template <class T>
119 inline ostream& operator << (ostream& os, const vtkNew<T>& p)
120 {
121  return os << p.GetPointer();
122 }
124 
125 #endif
~vtkNew()
Definition: vtkNew.h:79
vtkNew()
Definition: vtkNew.h:71
T * operator->() const
Definition: vtkNew.h:93
abstract base class for most VTK objects
Definition: vtkObjectBase.h:59
T * GetPointer() const
Definition: vtkNew.h:104
Allocate and hold a VTK object.
Definition: vtkNew.h:63