VTK  9.1.0
vtkSmartPointer.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkSmartPointer.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=========================================================================*/
32#ifndef vtkSmartPointer_h
33#define vtkSmartPointer_h
34
35#include "vtkSmartPointerBase.h"
36
37#include "vtkMeta.h" // for IsComplete
38#include "vtkNew.h" // for vtkNew.h
39
40#include <type_traits> // for is_base_of
41#include <utility> // for std::move
42
43template <class T>
45{
46 // These static asserts only fire when the function calling CheckTypes is
47 // used. Thus, this smart pointer class may still be used as a member variable
48 // with a forward declared T, so long as T is defined by the time the calling
49 // function is used.
50 template <typename U = T>
51 static void CheckTypes() noexcept
52 {
54 "vtkSmartPointer<T>'s T type has not been defined. Missing "
55 "include?");
57 "Cannot store an object with undefined type in "
58 "vtkSmartPointer. Missing include?");
59 static_assert(std::is_base_of<T, U>::value,
60 "Argument type is not compatible with vtkSmartPointer<T>'s "
61 "T type.");
63 "vtkSmartPointer can only be used with subclasses of "
64 "vtkObjectBase.");
65 }
66
67public:
71 vtkSmartPointer() noexcept
73 {
74 }
75
81 // Need both overloads because the copy-constructor must be non-templated:
84 {
85 }
86
87 template <class U>
90 {
91 vtkSmartPointer::CheckTypes<U>();
92 }
93 /* @} **/
94
99 // Need both overloads because the move-constructor must be non-templated:
101 : vtkSmartPointerBase(std::move(r))
102 {
103 }
104
105 template <class U>
107 : vtkSmartPointerBase(std::move(r))
108 {
109 vtkSmartPointer::CheckTypes<U>();
110 }
119 {
120 vtkSmartPointer::CheckTypes();
121 }
122
123 template <typename U>
126 { // Create a new reference on copy
127 vtkSmartPointer::CheckTypes<U>();
128 }
130
135 template <typename U>
138 { // Steal the reference on move
139 vtkSmartPointer::CheckTypes<U>();
140
141 r.Object = nullptr;
142 }
143
145
149 // Need this since the compiler won't recognize template functions as
150 // assignment operators.
152 {
154 return *this;
155 }
156
157 template <class U>
159 {
160 vtkSmartPointer::CheckTypes<U>();
161
163 return *this;
164 }
166
171 template <typename U>
173 {
174 vtkSmartPointer::CheckTypes<U>();
175
176 this->vtkSmartPointerBase::operator=(r.Object);
177 return *this;
178 }
179
184 template <typename U>
186 {
187 vtkSmartPointer::CheckTypes<U>();
188
190 return *this;
191 }
192
194
197 T* GetPointer() const noexcept { return static_cast<T*>(this->Object); }
198 T* Get() const noexcept { return static_cast<T*>(this->Object); }
200
204 operator T*() const noexcept { return static_cast<T*>(this->Object); }
205
210 T& operator*() const noexcept { return *static_cast<T*>(this->Object); }
211
215 T* operator->() const noexcept { return static_cast<T*>(this->Object); }
216
229 void TakeReference(T* t) { *this = vtkSmartPointer<T>(t, NoReference()); }
230
234 static vtkSmartPointer<T> New() { return vtkSmartPointer<T>(T::New(), NoReference()); }
241 {
242 return vtkSmartPointer<T>(T::ExtendedNew(), NoReference());
243 }
244
249 {
250 return vtkSmartPointer<T>(t->NewInstance(), NoReference());
251 }
252
267
268 // Work-around for HP and IBM overload resolution bug. Since
269 // NullPointerOnly is a private type the only pointer value that can
270 // be passed by user code is a null pointer. This operator will be
271 // chosen by the compiler when comparing against null explicitly and
272 // avoid the bogus ambiguous overload error.
273#if defined(__HP_aCC) || defined(__IBMCPP__)
274#define VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(op) \
275 bool operator op(NullPointerOnly*) const { return ::operator op(*this, 0); }
276
277private:
278 class NullPointerOnly
279 {
280 };
281
282public:
283 VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(==)
284 VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(!=)
285 VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(<)
286 VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(<=)
287 VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(>)
288 VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(>=)
289#undef VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND
290#endif
291protected:
293 : vtkSmartPointerBase(r, n)
294 {
295 }
296
297private:
298 // These are purposely not implemented to prevent callers from
299 // trying to take references from other smart pointers.
300 void TakeReference(const vtkSmartPointerBase&) = delete;
301 static void Take(const vtkSmartPointerBase&) = delete;
302};
303
304#define VTK_SMART_POINTER_DEFINE_OPERATOR(op) \
305 template <class T, class U> \
306 inline bool operator op(const vtkSmartPointer<T>& l, const vtkSmartPointer<U>& r) \
307 { \
308 return (l.GetPointer() op r.GetPointer()); \
309 } \
310 template <class T, class U> \
311 inline bool operator op(T* l, const vtkSmartPointer<U>& r) \
312 { \
313 return (l op r.GetPointer()); \
314 } \
315 template <class T, class U> \
316 inline bool operator op(const vtkSmartPointer<T>& l, U* r) \
317 { \
318 return (l.GetPointer() op r); \
319 } \
320 template <class T, class U> \
321 inline bool operator op(const vtkNew<T>& l, const vtkSmartPointer<U>& r) \
322 { \
323 return (l.GetPointer() op r.GetPointer()); \
324 } \
325 template <class T, class U> \
326 inline bool operator op(const vtkSmartPointer<T>& l, const vtkNew<U>& r) \
327 { \
328 return (l.GetPointer() op r.GetPointer); \
329 }
330
340
341#undef VTK_SMART_POINTER_DEFINE_OPERATOR
342
343namespace vtk
344{
345
348template <typename T>
350{
351 return vtkSmartPointer<T>{ obj };
352}
353
356template <typename T>
358{
359 return vtkSmartPointer<T>::Take(obj);
360}
361
362} // end namespace vtk
363
367template <class T>
368inline ostream& operator<<(ostream& os, const vtkSmartPointer<T>& p)
369{
370 return os << static_cast<const vtkSmartPointerBase&>(p);
371}
372
373#endif
374// VTK-HeaderTest-Exclude: vtkSmartPointer.h
Allocate and hold a VTK object.
Definition: vtkNew.h:65
Non-templated superclass for vtkSmartPointer.
vtkSmartPointerBase & operator=(vtkObjectBase *r)
Assign object to reference.
vtkSmartPointerBase() noexcept
Initialize smart pointer to nullptr.
vtkObjectBase * Object
Hold a reference to a vtkObjectBase instance.
static vtkSmartPointer< T > NewInstance(T *t)
Create a new instance of the given VTK object.
vtkSmartPointer() noexcept
Initialize smart pointer to nullptr.
vtkSmartPointer(vtkNew< U > &&r) noexcept
Move the pointer from the vtkNew smart pointer to the new vtkSmartPointer, stealing its reference and...
vtkSmartPointer(const vtkSmartPointer &r)
Initialize smart pointer with a new reference to the same object referenced by given smart pointer.
vtkSmartPointer(T *r)
Initialize smart pointer to given object.
vtkSmartPointer & operator=(U *r)
Assign object to reference.
vtkSmartPointer(const vtkSmartPointer< U > &r)
Initialize smart pointer with a new reference to the same object referenced by given smart pointer.
void TakeReference(T *t)
Transfer ownership of one reference to the given VTK object to this smart pointer.
vtkSmartPointer(vtkSmartPointer &&r) noexcept
Move the contents of r into this.
vtkSmartPointer & operator=(const vtkSmartPointer &r)
Assign object to reference.
vtkSmartPointer & operator=(const vtkNew< U > &r)
Assign object to reference.
T * operator->() const noexcept
Provides normal pointer target member access using operator ->.
vtkSmartPointer(vtkSmartPointer< U > &&r) noexcept
Initialize smart pointer with a new reference to the same object referenced by given smart pointer.
static vtkSmartPointer< T > Take(T *t)
Transfer ownership of one reference to the given VTK object to a new smart pointer.
T * GetPointer() const noexcept
Get the contained pointer.
T & operator*() const noexcept
Dereference the pointer and return a reference to the contained object.
vtkSmartPointer & operator=(const vtkSmartPointer< U > &r)
Assign object to reference.
static vtkSmartPointer< T > ExtendedNew()
Create an instance of a VTK object in a memkind extended memory space.
vtkSmartPointer(T *r, const NoReference &n)
T * Get() const noexcept
Get the contained pointer.
vtkSmartPointer(const vtkNew< U > &r)
Initialize smart pointer to given object.
static vtkSmartPointer< T > New()
Create an instance of a VTK object.
@ value
Definition: vtkX3D.h:226
Specialization of tuple ranges and iterators for vtkAOSDataArrayTemplate.
vtkSmartPointer< T > TakeSmartPointer(T *obj)
Construct a vtkSmartPointer<T> containing obj.
vtkSmartPointer< T > MakeSmartPointer(T *obj)
Construct a vtkSmartPointer<T> containing obj.
This file contains a variety of metaprogramming constructs for working with vtk types.
ostream & operator<<(ostream &os, const vtkSmartPointer< T > &p)
Streaming operator to print smart pointer like regular pointers.
#define VTK_SMART_POINTER_DEFINE_OPERATOR(op)