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
64class vtkObjectBase;
65
66template <class T>
67class vtkNew
68{
72 void CheckObjectBase(vtkObjectBase*) {}
73public:
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
123private:
124 vtkNew(vtkNew<T> const&) VTK_DELETE_FUNCTION;
125 void operator=(vtkNew<T> const&) VTK_DELETE_FUNCTION;
126 T* Object;
127};
128
132template <class T>
133inline ostream& operator << (ostream& os, const vtkNew<T>& p)
134{
135 return os << p.GetPointer();
136}
137
138#endif
139// VTK-HeaderTest-Exclude: vtkNew.h
Allocate and hold a VTK object.
Definition: vtkNew.h:68
T * GetPointer() const
Get a raw pointer to the contained object.
Definition: vtkNew.h:113
vtkNew()
Create a new T on construction.
Definition: vtkNew.h:77
T * operator->() const
Enable pointer-like dereference syntax.
Definition: vtkNew.h:101
~vtkNew()
Deletes reference to instance of T on destruction.
Definition: vtkNew.h:86
T * Get() const
Definition: vtkNew.h:117
abstract base class for most VTK objects
Definition: vtkObjectBase.h:66
ostream & operator<<(ostream &os, const vtkNew< T > &p)
Streaming operator to print vtkNew like regular pointers.
Definition: vtkNew.h:133