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