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 =========================================================================*/
59 #ifndef vtkNew_h
60 #define vtkNew_h
61 
62 #include "vtkIOStream.h"
63 
64 class vtkObjectBase;
65 
66 template <class T>
67 class vtkNew
68 {
72  void CheckObjectBase(vtkObjectBase*) {}
73 public:
77  vtkNew() : Object(T::New())
78  {
79  this->CheckObjectBase(this->Object);
80  }
81 
83 
87  {
88  T* obj = this->Object;
89  if (obj)
90  {
91  this->Object = 0;
92  obj->Delete();
93  }
94  }
96 
101  T* operator->() const
102  {
103  return this->Object;
104  }
105 
107 
113  T* GetPointer() const
114  {
115  return this->Object;
116  }
117  T* Get() const
118  {
119  return this->Object;
120  }
122 
123 private:
124  vtkNew(vtkNew<T> const&) VTK_DELETE_FUNCTION;
125  void operator=(vtkNew<T> const&) VTK_DELETE_FUNCTION;
126  T* Object;
127 };
128 
132 template <class T>
133 inline ostream& operator << (ostream& os, const vtkNew<T>& p)
134 {
135  return os << p.GetPointer();
136 }
137 
138 #endif
139 // VTK-HeaderTest-Exclude: vtkNew.h
~vtkNew()
Deletes reference to instance of T on destruction.
Definition: vtkNew.h:86
T * Get() const
Get a raw pointer to the contained object.
Definition: vtkNew.h:117
vtkNew()
Create a new T on construction.
Definition: vtkNew.h:77
T * operator->() const
Enable pointer-like dereference syntax.
Definition: vtkNew.h:101
abstract base class for most VTK objects
Definition: vtkObjectBase.h:65
T * GetPointer() const
Get a raw pointer to the contained object.
Definition: vtkNew.h:113
Allocate and hold a VTK object.
Definition: vtkNew.h:67