VTK  9.2.6
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=========================================================================*/
59#ifndef vtkNew_h
60#define vtkNew_h
61
62#include "vtkIOStream.h"
63#include "vtkMeta.h" // for IsComplete
64
65#include <type_traits> // for is_base_of
66
67class vtkObjectBase;
68
69template <class T>
70class vtkNew
71{
72 // Allow other smart pointers friend access:
73 template <typename U>
74 friend class vtkNew;
75 template <typename U>
76 friend class vtkSmartPointer;
77 template <typename U>
78 friend class vtkWeakPointer;
79
80 // These static asserts only fire when the function calling CheckTypes is
81 // used. Thus, this smart pointer class may still be used as a member variable
82 // with a forward declared T, so long as T is defined by the time the calling
83 // function is used.
84 template <typename U = T>
85 static void CheckTypes() noexcept
86 {
88 "vtkNew<T>'s T type has not been defined. Missing include?");
90 "Cannot store an object with undefined type in "
91 "vtkNew. Missing include?");
92 static_assert(std::is_base_of<T, U>::value,
93 "Argument type is not compatible with vtkNew<T>'s "
94 "T type.");
95 static_assert(std::is_base_of<vtkObjectBase, T>::value,
96 "vtkNew can only be used with subclasses of vtkObjectBase.");
97 }
98
99public:
104 : Object(T::New())
105 {
106 vtkNew::CheckTypes();
107 }
108
114 vtkNew(vtkNew&& o) noexcept
115 : Object(o.Object)
116 {
117 o.Object = nullptr;
118 }
119
120 template <typename U>
121 vtkNew(vtkNew<U>&& o) noexcept
122 : Object(o.Object)
123 {
124 vtkNew::CheckTypes<U>();
125
126 o.Object = nullptr;
127 }
129
131
134 ~vtkNew() { this->Reset(); }
135
136 void Reset()
137 {
138 T* obj = this->Object;
139 if (obj)
140 {
141 this->Object = nullptr;
142 obj->Delete();
143 }
144 }
146
151 T* operator->() const noexcept { return this->Object; }
152
154
160 T* GetPointer() const noexcept { return this->Object; }
161 T* Get() const noexcept { return this->Object; }
162 operator T*() const noexcept { return static_cast<T*>(this->Object); }
164
170 T& operator*() const noexcept { return *static_cast<T*>(this->Object); }
171
175 vtkNew<T>& operator=(vtkNew<T>&& other) noexcept
176 {
177 this->Reset();
178 this->Object = other.Object;
179 other.Object = nullptr;
180 return *this;
181 }
182
183private:
184 vtkNew(vtkNew<T> const&) = delete;
185 void operator=(vtkNew<T> const&) = delete;
186 T* Object;
187};
188
189#endif
190// VTK-HeaderTest-Exclude: vtkNew.h
Allocate and hold a VTK object.
Definition vtkNew.h:71
void Reset()
Deletes reference to instance of T.
Definition vtkNew.h:136
T * Get() const noexcept
Get a raw pointer to the contained object.
Definition vtkNew.h:161
friend class vtkNew
Definition vtkNew.h:74
vtkNew()
Create a new T on construction.
Definition vtkNew.h:103
T * operator->() const noexcept
Enable pointer-like dereference syntax.
Definition vtkNew.h:151
vtkNew(vtkNew &&o) noexcept
Move the object into the constructed vtkNew wrapper, stealing its reference.
Definition vtkNew.h:114
~vtkNew()
Deletes reference to instance of T.
Definition vtkNew.h:134
vtkNew(vtkNew< U > &&o) noexcept
Move the object into the constructed vtkNew wrapper, stealing its reference.
Definition vtkNew.h:121
T * GetPointer() const noexcept
Get a raw pointer to the contained object.
Definition vtkNew.h:160
T & operator*() const noexcept
Dereference the pointer and return a reference to the contained object.
Definition vtkNew.h:170
vtkNew< T > & operator=(vtkNew< T > &&other) noexcept
Move assignment operator.
Definition vtkNew.h:175
abstract base class for most VTK objects
Hold a reference to a vtkObjectBase instance.
a weak reference to a vtkObject.
This file contains a variety of metaprogramming constructs for working with vtk types.