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 =========================================================================*/
58 #ifndef vtkNew_h
59 #define vtkNew_h
60 
61 #include "vtkIOStream.h"
62 
63 class vtkObjectBase;
64 
65 template <class T>
66 class vtkNew
67 {
69 
70  void CheckObjectBase(vtkObjectBase*) {}
71 public:
73 
74 
75  vtkNew() : Object(T::New())
76  {
77  this->CheckObjectBase(this->Object);
78  }
80 
82 
84  {
85  T* obj = this->Object;
86  if (obj)
87  {
88  this->Object = 0;
89  obj->Delete();
90  }
91  }
93 
95 
97  T* operator->() const
98  {
99  return this->Object;
100  }
102 
104 
108  T* GetPointer() const
109  {
110  return this->Object;
111  }
112  T* Get() const
113  {
114  return this->Object;
115  }
117 
118 private:
119  vtkNew(vtkNew<T> const&); // Not implemented.
120  void operator=(vtkNew<T> const&); // Not implemented.
121  T* Object;
122 };
123 
125 
126 template <class T>
127 inline ostream& operator << (ostream& os, const vtkNew<T>& p)
128 {
129  return os << p.GetPointer();
130 }
132 
133 #endif
134 // VTK-HeaderTest-Exclude: vtkNew.h
~vtkNew()
Definition: vtkNew.h:83
T * Get() const
Definition: vtkNew.h:112
vtkNew()
Definition: vtkNew.h:75
T * operator->() const
Definition: vtkNew.h:97
abstract base class for most VTK objects
Definition: vtkObjectBase.h:59
T * GetPointer() const
Definition: vtkNew.h:108
Allocate and hold a VTK object.
Definition: vtkNew.h:66