VTK  9.0.20201024
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 #include "vtkMeta.h" // for IsComplete
49 
50 #include <type_traits> // for is_base_of
51 
52 class vtkObjectBase;
53 
54 template <class T>
55 class vtkNew
56 {
57  // Allow other smart pointers friend access:
58  template <typename U>
59  friend class vtkNew;
60  template <typename U>
61  friend class vtkSmartPointer;
62  template <typename U>
63  friend class vtkWeakPointer;
64 
65  // These static asserts only fire when the function calling CheckTypes is
66  // used. Thus, this smart pointer class may still be used as a member variable
67  // with a forward declared T, so long as T is defined by the time the calling
68  // function is used.
69  template <typename U = T>
70  static void CheckTypes() noexcept
71  {
73  "vtkNew<T>'s T type has not been defined. Missing include?");
75  "Cannot store an object with undefined type in "
76  "vtkNew. Missing include?");
77  static_assert(std::is_base_of<T, U>::value,
78  "Argument type is not compatible with vtkNew<T>'s "
79  "T type.");
81  "vtkNew can only be used with subclasses of vtkObjectBase.");
82  }
83 
84 public:
89  : Object(T::New())
90  {
91  vtkNew::CheckTypes();
92  }
93 
99  vtkNew(vtkNew&& o) noexcept
100  : Object(o.Object)
101  {
102  o.Object = nullptr;
103  }
104 
105  template <typename U>
106  vtkNew(vtkNew<U>&& o) noexcept
107  : Object(o.Object)
108  {
109  vtkNew::CheckTypes<U>();
110 
111  o.Object = nullptr;
112  }
114 
116 
119  ~vtkNew() { this->Reset(); }
120 
121  void Reset()
122  {
123  T* obj = this->Object;
124  if (obj)
125  {
126  this->Object = nullptr;
127  obj->Delete();
128  }
129  }
131 
136  T* operator->() const noexcept { return this->Object; }
137 
139 
145  T* GetPointer() const noexcept { return this->Object; }
146  T* Get() const noexcept { return this->Object; }
147  operator T*() const noexcept { return static_cast<T*>(this->Object); }
149 
155  T& operator*() const noexcept { return *static_cast<T*>(this->Object); }
156 
157 private:
158  vtkNew(vtkNew<T> const&) = delete;
159  void operator=(vtkNew<T> const&) = delete;
160  T* Object;
161 };
162 
163 #endif
164 // VTK-HeaderTest-Exclude: vtkNew.h
vtkWeakPointerBase::Object
vtkObjectBase * Object
Definition: vtkWeakPointerBase.h:95
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:99
vtkX3D::value
Definition: vtkX3D.h:226
vtkMeta.h
vtkSmartPointer
Hold a reference to a vtkObjectBase instance.
Definition: vtkMeta.h:32
vtkNew::Get
T * Get() const noexcept
Move the object into the constructed vtkNew wrapper, stealing its reference.
Definition: vtkNew.h:146
vtk::detail::IsComplete
Definition: vtkMeta.h:76
vtkNew::GetPointer
T * GetPointer() const noexcept
Get a raw pointer to the contained object.
Definition: vtkNew.h:145
vtkObjectBase::Delete
virtual void Delete()
Delete a VTK object.
vtkNew::operator*
T & operator*() const noexcept
Dereference the pointer and return a reference to the contained object.
Definition: vtkNew.h:155
vtkObjectBase
abstract base class for most VTK objects
Definition: vtkObjectBase.h:69
vtkNew::~vtkNew
~vtkNew()
Deletes reference to instance of T.
Definition: vtkNew.h:119
vtkNew::operator->
T * operator->() const noexcept
Enable pointer-like dereference syntax.
Definition: vtkNew.h:136
vtkNew::vtkNew
vtkNew(vtkNew< U > &&o) noexcept
Move the object into the constructed vtkNew wrapper, stealing its reference.
Definition: vtkNew.h:106
vtkNew
Allocate and hold a VTK object.
Definition: vtkMeta.h:30
vtkNew::vtkNew
vtkNew()
Create a new T on construction.
Definition: vtkNew.h:88
vtkIOStream.h
vtkNew::Reset
void Reset()
Move the object into the constructed vtkNew wrapper, stealing its reference.
Definition: vtkNew.h:121
vtkWeakPointer
a weak reference to a vtkObject.
Definition: vtkMeta.h:34