VTK  9.2.5
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
58class vtkObjectBase;
59
60template <class T>
61class vtkNew
62{
63 // Allow other smart pointers friend access:
64 template <typename U>
65 friend class vtkNew;
66 template <typename U>
67 friend class vtkSmartPointer;
68 template <typename U>
69 friend class vtkWeakPointer;
70
71 // These static asserts only fire when the function calling CheckTypes is
72 // used. Thus, this smart pointer class may still be used as a member variable
73 // with a forward declared T, so long as T is defined by the time the calling
74 // function is used.
75 template <typename U = T>
76 static void CheckTypes() noexcept
77 {
79 "vtkNew<T>'s T type has not been defined. Missing include?");
81 "Cannot store an object with undefined type in "
82 "vtkNew. Missing include?");
83 static_assert(std::is_base_of<T, U>::value,
84 "Argument type is not compatible with vtkNew<T>'s "
85 "T type.");
86 static_assert(std::is_base_of<vtkObjectBase, T>::value,
87 "vtkNew can only be used with subclasses of vtkObjectBase.");
88 }
89
90public:
95 : Object(T::New())
96 {
97 vtkNew::CheckTypes();
98 }
99
105 vtkNew(vtkNew&& o) noexcept
106 : Object(o.Object)
107 {
108 o.Object = nullptr;
109 }
110
111 template <typename U>
112 vtkNew(vtkNew<U>&& o) noexcept
113 : Object(o.Object)
114 {
115 vtkNew::CheckTypes<U>();
116
117 o.Object = nullptr;
118 }
120
122
125 ~vtkNew() { this->Reset(); }
126
127 void Reset()
128 {
129 T* obj = this->Object;
130 if (obj)
131 {
132 this->Object = nullptr;
133 obj->Delete();
134 }
135 }
137
142 T* operator->() const noexcept { return this->Object; }
143
145
151 T* GetPointer() const noexcept { return this->Object; }
152 T* Get() const noexcept { return this->Object; }
153 operator T*() const noexcept { return static_cast<T*>(this->Object); }
155
161 T& operator*() const noexcept { return *static_cast<T*>(this->Object); }
162
166 vtkNew<T>& operator=(vtkNew<T>&& other) noexcept
167 {
168 this->Reset();
169 this->Object = other.Object;
170 other.Object = nullptr;
171 return *this;
172 }
173
174private:
175 vtkNew(vtkNew<T> const&) = delete;
176 void operator=(vtkNew<T> const&) = delete;
177 T* Object;
178};
179
180#endif
181// VTK-HeaderTest-Exclude: vtkNew.h
Allocate and hold a VTK object.
Definition: vtkNew.h:62
void Reset()
Deletes reference to instance of T.
Definition: vtkNew.h:127
T * Get() const noexcept
Get a raw pointer to the contained object.
Definition: vtkNew.h:152
friend class vtkNew
Definition: vtkNew.h:65
vtkNew()
Create a new T on construction.
Definition: vtkNew.h:94
T * operator->() const noexcept
Enable pointer-like dereference syntax.
Definition: vtkNew.h:142
vtkNew(vtkNew &&o) noexcept
Move the object into the constructed vtkNew wrapper, stealing its reference.
Definition: vtkNew.h:105
~vtkNew()
Deletes reference to instance of T.
Definition: vtkNew.h:125
vtkNew(vtkNew< U > &&o) noexcept
Move the object into the constructed vtkNew wrapper, stealing its reference.
Definition: vtkNew.h:112
T * GetPointer() const noexcept
Get a raw pointer to the contained object.
Definition: vtkNew.h:151
T & operator*() const noexcept
Dereference the pointer and return a reference to the contained object.
Definition: vtkNew.h:161
vtkNew< T > & operator=(vtkNew< T > &&other) noexcept
Move assignment operator.
Definition: vtkNew.h:166
abstract base class for most VTK objects
Definition: vtkObjectBase.h:74
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.