VTK  9.0.20210927
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 =========================================================================*/
153 #ifndef vtkNew_h
154 #define vtkNew_h
155 
156 #include "vtkIOStream.h"
157 #include "vtkMeta.h" // for IsComplete
158 
159 #include <type_traits> // for is_base_of
160 
161 class vtkObjectBase;
162 
163 template <class T>
164 class vtkNew
165 {
166  // Allow other smart pointers friend access:
167  template <typename U>
168  friend class vtkNew;
169  template <typename U>
170  friend class vtkSmartPointer;
171  template <typename U>
172  friend class vtkWeakPointer;
173 
174  // These static asserts only fire when the function calling CheckTypes is
175  // used. Thus, this smart pointer class may still be used as a member variable
176  // with a forward declared T, so long as T is defined by the time the calling
177  // function is used.
178  template <typename U = T>
179  static void CheckTypes() noexcept
180  {
181  static_assert(vtk::detail::IsComplete<T>::value,
182  "vtkNew<T>'s T type has not been defined. Missing include?");
183  static_assert(vtk::detail::IsComplete<U>::value,
184  "Cannot store an object with undefined type in "
185  "vtkNew. Missing include?");
186  static_assert(std::is_base_of<T, U>::value,
187  "Argument type is not compatible with vtkNew<T>'s "
188  "T type.");
190  "vtkNew can only be used with subclasses of vtkObjectBase.");
191  }
192 
193 public:
198  : Object(T::New())
199  {
200  vtkNew::CheckTypes();
201  }
202 
208  vtkNew(vtkNew&& o) noexcept
209  : Object(o.Object)
210  {
211  o.Object = nullptr;
212  }
213 
214  template <typename U>
215  vtkNew(vtkNew<U>&& o) noexcept
216  : Object(o.Object)
217  {
218  vtkNew::CheckTypes<U>();
219 
220  o.Object = nullptr;
221  }
223 
225 
228  ~vtkNew() { this->Reset(); }
229 
230  void Reset()
231  {
232  T* obj = this->Object;
233  if (obj)
234  {
235  this->Object = nullptr;
236  obj->Delete();
237  }
238  }
240 
245  T* operator->() const noexcept { return this->Object; }
246 
248 
254  T* GetPointer() const noexcept { return this->Object; }
255  T* Get() const noexcept { return this->Object; }
256  operator T*() const noexcept { return static_cast<T*>(this->Object); }
258 
264  T& operator*() const noexcept { return *static_cast<T*>(this->Object); }
265 
266 private:
267  vtkNew(vtkNew<T> const&) = delete;
268  void operator=(vtkNew<T> const&) = delete;
269  T* Object;
270 };
271 
272 #endif
273 // VTK-HeaderTest-Exclude: vtkNew.h
vtkWeakPointer::operator=
vtkWeakPointer & operator=(const vtkWeakPointer &r)
Assign object to reference.
Definition: vtkWeakPointer.h:143
vtkNew::vtkNew
vtkNew(vtkNew &&o) noexcept
Move the object into the constructed vtkNew wrapper, stealing its reference.
Definition: vtkNew.h:208
vtkX3D::value
@ value
Definition: vtkX3D.h:226
vtkMeta.h
This file contains a variety of metaprogramming constructs for working with vtk types.
vtkSmartPointer
Hold a reference to a vtkObjectBase instance.
Definition: vtkSmartPointer.h:145
vtkNew::Get
T * Get() const noexcept
Get a raw pointer to the contained object.
Definition: vtkNew.h:255
vtk::detail::IsComplete
Definition: vtkMeta.h:77
vtkNew::vtkNew
friend class vtkNew
Definition: vtkNew.h:168
vtkNew::GetPointer
T * GetPointer() const noexcept
Get a raw pointer to the contained object.
Definition: vtkNew.h:254
vtkNew::operator*
T & operator*() const noexcept
Dereference the pointer and return a reference to the contained object.
Definition: vtkNew.h:264
vtkObjectBase
abstract base class for most VTK objects
Definition: vtkObjectBase.h:70
vtkNew::~vtkNew
~vtkNew()
Deletes reference to instance of T.
Definition: vtkNew.h:228
vtkNew::operator->
T * operator->() const noexcept
Enable pointer-like dereference syntax.
Definition: vtkNew.h:245
vtkNew::vtkNew
vtkNew(vtkNew< U > &&o) noexcept
Move the object into the constructed vtkNew wrapper, stealing its reference.
Definition: vtkNew.h:215
vtkNew
Allocate and hold a VTK object.
Definition: vtkNew.h:165
vtkNew::vtkNew
vtkNew()
Create a new T on construction.
Definition: vtkNew.h:197
vtkIOStream.h
vtkNew::Reset
void Reset()
Deletes reference to instance of T.
Definition: vtkNew.h:230
vtkWeakPointer
a weak reference to a vtkObject.
Definition: vtkWeakPointer.h:56