VTK  9.4.20241221
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;
158
159template <class T>
160class vtkNew;
161
162template <class T>
163void vtkGarbageCollectorReport(vtkGarbageCollector* collector, vtkNew<T>& ptr, const char* desc);
164
165template <class T>
167{
168 // Allow other smart pointers friend access:
169 template <typename U>
170 friend class vtkNew;
171 template <typename U>
172 friend class vtkSmartPointer;
173 template <typename U>
174 friend class vtkWeakPointer;
175
176 // These static asserts only fire when the function calling CheckTypes is
177 // used. Thus, this smart pointer class may still be used as a member variable
178 // with a forward declared T, so long as T is defined by the time the calling
179 // function is used.
180 template <typename U = T>
181 static void CheckTypes() noexcept
182 {
184 "vtkNew<T>'s T type has not been defined. Missing include?");
186 "Cannot store an object with undefined type in "
187 "vtkNew. Missing include?");
188 static_assert(std::is_base_of<T, U>::value,
189 "Argument type is not compatible with vtkNew<T>'s "
190 "T type.");
191 static_assert(std::is_base_of<vtkObjectBase, T>::value,
192 "vtkNew can only be used with subclasses of vtkObjectBase.");
193 }
194
195public:
200 : Object(T::New())
201 {
202 vtkNew::CheckTypes();
203 }
204
210 vtkNew(vtkNew&& o) noexcept
211 : Object(o.Object)
212 {
213 o.Object = nullptr;
214 }
215
216 template <typename U>
217 vtkNew(vtkNew<U>&& o) noexcept
218 : Object(o.Object)
219 {
220 vtkNew::CheckTypes<U>();
221
222 o.Object = nullptr;
223 }
225
227
230 ~vtkNew() { this->Reset(); }
231
232 void Reset()
233 {
234 T* obj = this->Object;
235 if (obj)
236 {
237 this->Object = nullptr;
238 obj->Delete();
239 }
240 }
242
247 T* operator->() const noexcept { return this->Object; }
248
250
256 T* GetPointer() const noexcept { return this->Object; }
257 T* Get() const noexcept { return this->Object; }
258 operator T*() const noexcept { return static_cast<T*>(this->Object); }
260
266 T& operator*() const noexcept { return *static_cast<T*>(this->Object); }
267
271 vtkNew<T>& operator=(vtkNew<T>&& other) noexcept
272 {
273 this->Reset();
274 this->Object = other.Object;
275 other.Object = nullptr;
276 return *this;
277 }
278
279private:
280 vtkNew(vtkNew<T> const&) = delete;
281 void operator=(vtkNew<T> const&) = delete;
282 friend void vtkGarbageCollectorReport<T>(
283 vtkGarbageCollector* collector, vtkNew<T>& ptr, const char* desc);
284 T* Object;
285};
286
287VTK_ABI_NAMESPACE_END
288#endif
289// VTK-HeaderTest-Exclude: vtkNew.h
Detect and break reference loops.
Allocate and hold a VTK object.
Definition vtkNew.h:167
void Reset()
Deletes reference to instance of T.
Definition vtkNew.h:232
T * Get() const noexcept
Get a raw pointer to the contained object.
Definition vtkNew.h:257
friend class vtkNew
Definition vtkNew.h:170
vtkNew()
Create a new T on construction.
Definition vtkNew.h:199
T * operator->() const noexcept
Enable pointer-like dereference syntax.
Definition vtkNew.h:247
vtkNew(vtkNew &&o) noexcept
Move the object into the constructed vtkNew wrapper, stealing its reference.
Definition vtkNew.h:210
~vtkNew()
Deletes reference to instance of T.
Definition vtkNew.h:230
vtkNew(vtkNew< U > &&o) noexcept
Move the object into the constructed vtkNew wrapper, stealing its reference.
Definition vtkNew.h:217
T * GetPointer() const noexcept
Get a raw pointer to the contained object.
Definition vtkNew.h:256
T & operator*() const noexcept
Dereference the pointer and return a reference to the contained object.
Definition vtkNew.h:266
vtkNew< T > & operator=(vtkNew< T > &&other) noexcept
Move assignment operator.
Definition vtkNew.h:271
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.
void vtkGarbageCollectorReport(vtkGarbageCollector *collector, vtkNew< T > &ptr, const char *desc)
Function to report a reference held by a vtkNew to a collector.