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 =========================================================================*/
44 #ifndef vtkNew_h
45 #define vtkNew_h
46 
47 #include "vtkIOStream.h"
48 
49 class vtkObjectBase;
50 
51 template <class T>
52 class vtkNew
53 {
57  void CheckObjectBase(vtkObjectBase*) {}
58 public:
62  vtkNew() : Object(T::New())
63  {
64  this->CheckObjectBase(this->Object);
65  }
66 
68 
72  {
73  T* obj = this->Object;
74  if (obj)
75  {
76  this->Object = nullptr;
77  obj->Delete();
78  }
79  }
81 
86  T* operator->() const
87  {
88  return this->Object;
89  }
90 
92 
98  T* GetPointer() const
99  {
100  return this->Object;
101  }
102  T* Get() const
103  {
104  return this->Object;
105  }
106  operator T* () const
107  {
108  return static_cast<T*>(this->Object);
109  }
111 
117  T& operator*() const
118  {
119  return *static_cast<T*>(this->Object);
120  }
121 
122 private:
123  vtkNew(vtkNew<T> const&) = delete;
124  void operator=(vtkNew<T> const&) = delete;
125  T* Object;
126 };
127 
128 #endif
129 // VTK-HeaderTest-Exclude: vtkNew.h
vtkNew::operator->
T * operator->() const
Enable pointer-like dereference syntax.
Definition: vtkNew.h:86
vtkNew::operator*
T & operator*() const
Dereference the pointer and return a reference to the contained object.
Definition: vtkNew.h:117
vtkObjectBase
abstract base class for most VTK objects
Definition: vtkObjectBase.h:62
vtkNew::~vtkNew
~vtkNew()
Deletes reference to instance of T on destruction.
Definition: vtkNew.h:71
vtkNew
Allocate and hold a VTK object.
Definition: vtkNew.h:52
vtkNew::GetPointer
T * GetPointer() const
Get a raw pointer to the contained object.
Definition: vtkNew.h:98
vtkNew::vtkNew
vtkNew()
Create a new T on construction.
Definition: vtkNew.h:62
vtkIOStream.h
vtkNew::Get
T * Get() const
Definition: vtkNew.h:102