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 =========================================================================*/
53 #ifndef vtkNew_h
54 #define vtkNew_h
55 
56 #include "vtkIOStream.h"
57 
58 class vtkObjectBase;
59 
60 template <class T>
61 class vtkNew
62 {
66  void CheckObjectBase(vtkObjectBase*) {}
67 public:
71  vtkNew() : Object(T::New())
72  {
73  this->CheckObjectBase(this->Object);
74  }
75 
77 
81  {
82  T* obj = this->Object;
83  if (obj)
84  {
85  this->Object = 0;
86  obj->Delete();
87  }
88  }
90 
95  T* operator->() const
96  {
97  return this->Object;
98  }
99 
101 
107  T* GetPointer() const
108  {
109  return this->Object;
110  }
111  T* Get() const
112  {
113  return this->Object;
114  }
116 
117 private:
118  vtkNew(vtkNew<T> const&) VTK_DELETE_FUNCTION;
119  void operator=(vtkNew<T> const&) VTK_DELETE_FUNCTION;
120  T* Object;
121 };
122 
126 template <class T>
127 inline ostream& operator << (ostream& os, const vtkNew<T>& p)
128 {
129  return os << p.GetPointer();
130 }
131 
132 #endif
133 // VTK-HeaderTest-Exclude: vtkNew.h
~vtkNew()
Deletes reference to instance of T on destruction.
Definition: vtkNew.h:80
T * Get() const
Get a raw pointer to the contained object.
Definition: vtkNew.h:111
vtkNew()
Create a new T on construction.
Definition: vtkNew.h:71
T * operator->() const
Enable pointer-like dereference syntax.
Definition: vtkNew.h:95
abstract base class for most VTK objects
Definition: vtkObjectBase.h:62
T * GetPointer() const
Get a raw pointer to the contained object.
Definition: vtkNew.h:107
Allocate and hold a VTK object.
Definition: vtkNew.h:61