VTK  9.2.20220626
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 =========================================================================*/
159 #ifndef vtkNew_h
160 #define vtkNew_h
161 
162 #include "vtkIOStream.h"
163 #include "vtkMeta.h" // for IsComplete
164 
165 #include <type_traits> // for is_base_of
166 
167 class vtkObjectBase;
168 
169 template <class T>
170 class vtkNew
171 {
172  // Allow other smart pointers friend access:
173  template <typename U>
174  friend class vtkNew;
175  template <typename U>
176  friend class vtkSmartPointer;
177  template <typename U>
178  friend class vtkWeakPointer;
179 
180  // These static asserts only fire when the function calling CheckTypes is
181  // used. Thus, this smart pointer class may still be used as a member variable
182  // with a forward declared T, so long as T is defined by the time the calling
183  // function is used.
184  template <typename U = T>
185  static void CheckTypes() noexcept
186  {
187  static_assert(vtk::detail::IsComplete<T>::value,
188  "vtkNew<T>'s T type has not been defined. Missing include?");
189  static_assert(vtk::detail::IsComplete<U>::value,
190  "Cannot store an object with undefined type in "
191  "vtkNew. Missing include?");
192  static_assert(std::is_base_of<T, U>::value,
193  "Argument type is not compatible with vtkNew<T>'s "
194  "T type.");
196  "vtkNew can only be used with subclasses of vtkObjectBase.");
197  }
198 
199 public:
204  : Object(T::New())
205  {
206  vtkNew::CheckTypes();
207  }
208 
214  vtkNew(vtkNew&& o) noexcept
215  : Object(o.Object)
216  {
217  o.Object = nullptr;
218  }
219 
220  template <typename U>
221  vtkNew(vtkNew<U>&& o) noexcept
222  : Object(o.Object)
223  {
224  vtkNew::CheckTypes<U>();
225 
226  o.Object = nullptr;
227  }
229 
231 
234  ~vtkNew() { this->Reset(); }
235 
236  void Reset()
237  {
238  T* obj = this->Object;
239  if (obj)
240  {
241  this->Object = nullptr;
242  obj->Delete();
243  }
244  }
246 
251  T* operator->() const noexcept { return this->Object; }
252 
254 
260  T* GetPointer() const noexcept { return this->Object; }
261  T* Get() const noexcept { return this->Object; }
262  operator T*() const noexcept { return static_cast<T*>(this->Object); }
264 
270  T& operator*() const noexcept { return *static_cast<T*>(this->Object); }
271 
275  vtkNew<T>& operator=(vtkNew<T>&& other) noexcept
276  {
277  this->Reset();
278  this->Object = other.Object;
279  other.Object = nullptr;
280  return *this;
281  }
282 
283 private:
284  vtkNew(vtkNew<T> const&) = delete;
285  void operator=(vtkNew<T> const&) = delete;
286  T* Object;
287 };
288 
289 #endif
290 // VTK-HeaderTest-Exclude: vtkNew.h
Allocate and hold a VTK object.
Definition: vtkNew.h:171
void Reset()
Deletes reference to instance of T.
Definition: vtkNew.h:236
T & operator*() const noexcept
Dereference the pointer and return a reference to the contained object.
Definition: vtkNew.h:270
friend class vtkNew
Definition: vtkNew.h:174
vtkNew()
Create a new T on construction.
Definition: vtkNew.h:203
T * Get() const noexcept
Get a raw pointer to the contained object.
Definition: vtkNew.h:261
vtkNew(vtkNew &&o) noexcept
Move the object into the constructed vtkNew wrapper, stealing its reference.
Definition: vtkNew.h:214
T * GetPointer() const noexcept
Get a raw pointer to the contained object.
Definition: vtkNew.h:260
~vtkNew()
Deletes reference to instance of T.
Definition: vtkNew.h:234
vtkNew(vtkNew< U > &&o) noexcept
Move the object into the constructed vtkNew wrapper, stealing its reference.
Definition: vtkNew.h:221
vtkNew< T > & operator=(vtkNew< T > &&other) noexcept
Move assignment operator.
Definition: vtkNew.h:275
T * operator->() const noexcept
Enable pointer-like dereference syntax.
Definition: vtkNew.h:251
abstract base class for most VTK objects
Definition: vtkObjectBase.h:74
virtual void Delete()
Delete a VTK object.
Hold a reference to a vtkObjectBase instance.
vtkObjectBase * Object
a weak reference to a vtkObject.
@ value
Definition: vtkX3D.h:226
This file contains a variety of metaprogramming constructs for working with vtk types.