VTK  9.3.20240721
vtkNew.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
147#ifndef vtkNew_h
148#define vtkNew_h
149
150#include "vtkIOStream.h"
151#include "vtkMeta.h" // for IsComplete
152
153#include <type_traits> // for is_base_of
154
155VTK_ABI_NAMESPACE_BEGIN
156class vtkObjectBase;
157
158template <class T>
160{
161 // Allow other smart pointers friend access:
162 template <typename U>
163 friend class vtkNew;
164 template <typename U>
165 friend class vtkSmartPointer;
166 template <typename U>
167 friend class vtkWeakPointer;
168
169 // These static asserts only fire when the function calling CheckTypes is
170 // used. Thus, this smart pointer class may still be used as a member variable
171 // with a forward declared T, so long as T is defined by the time the calling
172 // function is used.
173 template <typename U = T>
174 static void CheckTypes() noexcept
175 {
177 "vtkNew<T>'s T type has not been defined. Missing include?");
179 "Cannot store an object with undefined type in "
180 "vtkNew. Missing include?");
181 static_assert(std::is_base_of<T, U>::value,
182 "Argument type is not compatible with vtkNew<T>'s "
183 "T type.");
184 static_assert(std::is_base_of<vtkObjectBase, T>::value,
185 "vtkNew can only be used with subclasses of vtkObjectBase.");
186 }
187
188public:
193 : Object(T::New())
194 {
195 vtkNew::CheckTypes();
196 }
197
203 vtkNew(vtkNew&& o) noexcept
204 : Object(o.Object)
205 {
206 o.Object = nullptr;
207 }
208
209 template <typename U>
210 vtkNew(vtkNew<U>&& o) noexcept
211 : Object(o.Object)
212 {
213 vtkNew::CheckTypes<U>();
214
215 o.Object = nullptr;
216 }
218
220
223 ~vtkNew() { this->Reset(); }
224
225 void Reset()
226 {
227 T* obj = this->Object;
228 if (obj)
229 {
230 this->Object = nullptr;
231 obj->Delete();
232 }
233 }
235
240 T* operator->() const noexcept { return this->Object; }
241
243
249 T* GetPointer() const noexcept { return this->Object; }
250 T* Get() const noexcept { return this->Object; }
251 operator T*() const noexcept { return static_cast<T*>(this->Object); }
253
259 T& operator*() const noexcept { return *static_cast<T*>(this->Object); }
260
264 vtkNew<T>& operator=(vtkNew<T>&& other) noexcept
265 {
266 this->Reset();
267 this->Object = other.Object;
268 other.Object = nullptr;
269 return *this;
270 }
271
272private:
273 vtkNew(vtkNew<T> const&) = delete;
274 void operator=(vtkNew<T> const&) = delete;
275 T* Object;
276};
277
278VTK_ABI_NAMESPACE_END
279#endif
280// VTK-HeaderTest-Exclude: vtkNew.h
Allocate and hold a VTK object.
Definition vtkNew.h:160
void Reset()
Deletes reference to instance of T.
Definition vtkNew.h:225
T * Get() const noexcept
Get a raw pointer to the contained object.
Definition vtkNew.h:250
friend class vtkNew
Definition vtkNew.h:163
vtkNew()
Create a new T on construction.
Definition vtkNew.h:192
T * operator->() const noexcept
Enable pointer-like dereference syntax.
Definition vtkNew.h:240
vtkNew(vtkNew &&o) noexcept
Move the object into the constructed vtkNew wrapper, stealing its reference.
Definition vtkNew.h:203
~vtkNew()
Deletes reference to instance of T.
Definition vtkNew.h:223
vtkNew(vtkNew< U > &&o) noexcept
Move the object into the constructed vtkNew wrapper, stealing its reference.
Definition vtkNew.h:210
T * GetPointer() const noexcept
Get a raw pointer to the contained object.
Definition vtkNew.h:249
T & operator*() const noexcept
Dereference the pointer and return a reference to the contained object.
Definition vtkNew.h:259
vtkNew< T > & operator=(vtkNew< T > &&other) noexcept
Move assignment operator.
Definition vtkNew.h:264
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.