VTK  9.3.0
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
38#ifndef vtkNew_h
39#define vtkNew_h
40
41#include "vtkIOStream.h"
42#include "vtkMeta.h" // for IsComplete
43
44#include <type_traits> // for is_base_of
45
46VTK_ABI_NAMESPACE_BEGIN
47class vtkObjectBase;
48
49template <class T>
50class vtkNew
51{
52 // Allow other smart pointers friend access:
53 template <typename U>
54 friend class vtkNew;
55 template <typename U>
56 friend class vtkSmartPointer;
57 template <typename U>
58 friend class vtkWeakPointer;
59
60 // These static asserts only fire when the function calling CheckTypes is
61 // used. Thus, this smart pointer class may still be used as a member variable
62 // with a forward declared T, so long as T is defined by the time the calling
63 // function is used.
64 template <typename U = T>
65 static void CheckTypes() noexcept
66 {
68 "vtkNew<T>'s T type has not been defined. Missing include?");
70 "Cannot store an object with undefined type in "
71 "vtkNew. Missing include?");
72 static_assert(std::is_base_of<T, U>::value,
73 "Argument type is not compatible with vtkNew<T>'s "
74 "T type.");
75 static_assert(std::is_base_of<vtkObjectBase, T>::value,
76 "vtkNew can only be used with subclasses of vtkObjectBase.");
77 }
78
79public:
84 : Object(T::New())
85 {
86 vtkNew::CheckTypes();
87 }
88
94 vtkNew(vtkNew&& o) noexcept
95 : Object(o.Object)
96 {
97 o.Object = nullptr;
98 }
99
100 template <typename U>
101 vtkNew(vtkNew<U>&& o) noexcept
102 : Object(o.Object)
103 {
104 vtkNew::CheckTypes<U>();
105
106 o.Object = nullptr;
107 }
109
111
114 ~vtkNew() { this->Reset(); }
115
116 void Reset()
117 {
118 T* obj = this->Object;
119 if (obj)
120 {
121 this->Object = nullptr;
122 obj->Delete();
123 }
124 }
126
131 T* operator->() const noexcept { return this->Object; }
132
134
140 T* GetPointer() const noexcept { return this->Object; }
141 T* Get() const noexcept { return this->Object; }
142 operator T*() const noexcept { return static_cast<T*>(this->Object); }
144
150 T& operator*() const noexcept { return *static_cast<T*>(this->Object); }
151
155 vtkNew<T>& operator=(vtkNew<T>&& other) noexcept
156 {
157 this->Reset();
158 this->Object = other.Object;
159 other.Object = nullptr;
160 return *this;
161 }
162
163private:
164 vtkNew(vtkNew<T> const&) = delete;
165 void operator=(vtkNew<T> const&) = delete;
166 T* Object;
167};
168
169VTK_ABI_NAMESPACE_END
170#endif
171// VTK-HeaderTest-Exclude: vtkNew.h
Allocate and hold a VTK object.
Definition vtkNew.h:51
void Reset()
Deletes reference to instance of T.
Definition vtkNew.h:116
T * Get() const noexcept
Get a raw pointer to the contained object.
Definition vtkNew.h:141
friend class vtkNew
Definition vtkNew.h:54
vtkNew()
Create a new T on construction.
Definition vtkNew.h:83
T * operator->() const noexcept
Enable pointer-like dereference syntax.
Definition vtkNew.h:131
vtkNew(vtkNew &&o) noexcept
Move the object into the constructed vtkNew wrapper, stealing its reference.
Definition vtkNew.h:94
~vtkNew()
Deletes reference to instance of T.
Definition vtkNew.h:114
vtkNew(vtkNew< U > &&o) noexcept
Move the object into the constructed vtkNew wrapper, stealing its reference.
Definition vtkNew.h:101
T * GetPointer() const noexcept
Get a raw pointer to the contained object.
Definition vtkNew.h:140
T & operator*() const noexcept
Dereference the pointer and return a reference to the contained object.
Definition vtkNew.h:150
vtkNew< T > & operator=(vtkNew< T > &&other) noexcept
Move assignment operator.
Definition vtkNew.h:155
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.