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 =========================================================================*/
50 #ifndef vtkNew_h
51 #define vtkNew_h
52 
53 #include "vtkIOStream.h"
54 #include "vtkMeta.h" // for IsComplete
55 
56 #include <type_traits> // for is_base_of
57 
58 class vtkObjectBase;
59 
60 template <class T>
61 class vtkNew
62 {
63  // Allow other smart pointers friend access:
64  template <typename U> friend class vtkNew;
65  template <typename U> friend class vtkSmartPointer;
66  template <typename U> friend class vtkWeakPointer;
67 
68  // These static asserts only fire when the function calling CheckTypes is
69  // used. Thus, this smart pointer class may still be used as a member variable
70  // with a forward declared T, so long as T is defined by the time the calling
71  // function is used.
72  template <typename U = T>
73  static void CheckTypes() noexcept
74  {
76  "vtkNew<T>'s T type has not been defined. Missing include?");
78  "Cannot store an object with undefined type in "
79  "vtkNew. Missing include?");
80  static_assert(std::is_base_of<T, U>::value,
81  "Argument type is not compatible with vtkNew<T>'s "
82  "T type.");
84  "vtkNew can only be used with subclasses of vtkObjectBase.");
85  }
86 
87 public:
91  vtkNew() : Object(T::New())
92  {
93  vtkNew::CheckTypes();
94  }
95 
101  vtkNew(vtkNew &&o) noexcept
102  : Object(o.Object)
103  {
104  o.Object = nullptr;
105  }
106 
107  template <typename U>
108  vtkNew(vtkNew<U> &&o) noexcept
109  : Object(o.Object)
110  {
111  vtkNew::CheckTypes<U>();
112 
113  o.Object = nullptr;
114  }
116 
118 
122  {
123  this->Reset();
124  }
125 
126  void Reset()
127  {
128  T* obj = this->Object;
129  if (obj)
130  {
131  this->Object = nullptr;
132  obj->Delete();
133  }
134  }
136 
141  T* operator->() const noexcept
142  {
143  return this->Object;
144  }
145 
147 
153  T* GetPointer() const noexcept
154  {
155  return this->Object;
156  }
157  T* Get() const noexcept
158  {
159  return this->Object;
160  }
161  operator T* () const noexcept
162  {
163  return static_cast<T*>(this->Object);
164  }
166 
172  T& operator*() const noexcept
173  {
174  return *static_cast<T*>(this->Object);
175  }
176 
177 private:
178  vtkNew(vtkNew<T> const&) = delete;
179  void operator=(vtkNew<T> const&) = delete;
180  T* Object;
181 };
182 
183 #endif
184 // VTK-HeaderTest-Exclude: vtkNew.h
vtkWeakPointerBase::Object
vtkObjectBase * Object
Definition: vtkWeakPointerBase.h:91
vtkWeakPointer::operator=
vtkWeakPointer & operator=(const vtkWeakPointer &r)
Assign object to reference.
Definition: vtkWeakPointer.h:146
vtkNew::vtkNew
vtkNew(vtkNew &&o) noexcept
Move the object into the constructed vtkNew wrapper, stealing its reference.
Definition: vtkNew.h:101
vtkX3D::value
Definition: vtkX3D.h:220
vtkMeta.h
vtkSmartPointer
Hold a reference to a vtkObjectBase instance.
Definition: vtkMeta.h:30
vtkNew::Get
T * Get() const noexcept
Move the object into the constructed vtkNew wrapper, stealing its reference.
Definition: vtkNew.h:157
vtk::detail::IsComplete
Definition: vtkMeta.h:73
vtkNew::GetPointer
T * GetPointer() const noexcept
Get a raw pointer to the contained object.
Definition: vtkNew.h:153
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:172
vtkObjectBase
abstract base class for most VTK objects
Definition: vtkObjectBase.h:66
vtkNew::~vtkNew
~vtkNew()
Deletes reference to instance of T.
Definition: vtkNew.h:121
vtkNew::operator->
T * operator->() const noexcept
Enable pointer-like dereference syntax.
Definition: vtkNew.h:141
vtkNew::vtkNew
vtkNew(vtkNew< U > &&o) noexcept
Move the object into the constructed vtkNew wrapper, stealing its reference.
Definition: vtkNew.h:108
vtkNew
Allocate and hold a VTK object.
Definition: vtkMeta.h:29
vtkNew::vtkNew
vtkNew()
Create a new T on construction.
Definition: vtkNew.h:91
vtkIOStream.h
vtkNew::Reset
void Reset()
Move the object into the constructed vtkNew wrapper, stealing its reference.
Definition: vtkNew.h:126
vtkWeakPointer
a weak reference to a vtkObject.
Definition: vtkMeta.h:31