VTK  9.1.0
vtkWeakPointer.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkWeakPointer.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=========================================================================*/
43#ifndef vtkWeakPointer_h
44#define vtkWeakPointer_h
45
46#include "vtkWeakPointerBase.h"
47
48#include "vtkMeta.h" // for IsComplete
49#include "vtkNew.h" // for vtkNew
50
51#include <type_traits> // for is_base_of
52#include <utility> // for std::move
53
54template <class T>
56{
57 // These static asserts only fire when the function calling CheckTypes is
58 // used. Thus, this smart pointer class may still be used as a member variable
59 // with a forward declared T, so long as T is defined by the time the calling
60 // function is used.
61 template <typename U = T>
62 static void CheckTypes() noexcept
63 {
65 "vtkWeakPointer<T>'s T type has not been defined. Missing "
66 "include?");
68 "Cannot store an object with undefined type in "
69 "vtkWeakPointer. Missing include?");
70 static_assert(std::is_base_of<T, U>::value,
71 "Argument type is not compatible with vtkWeakPointer<T>'s "
72 "T type.");
73 static_assert(std::is_base_of<vtkObjectBase, T>::value,
74 "vtkWeakPointer can only be used with subclasses of "
75 "vtkObjectBase.");
76 }
77
78public:
82 vtkWeakPointer() noexcept
84 {
85 }
86
93 {
94 }
95
96 template <class U>
99 {
100 vtkWeakPointer::CheckTypes<U>();
101 }
102 /* @} **/
103
109 : vtkWeakPointerBase(std::move(r))
110 {
111 }
112
113 template <class U>
115 : vtkWeakPointerBase(std::move(r))
116 {
117 vtkWeakPointer::CheckTypes<U>();
118 }
119 /* @} **/
120
127 {
128 vtkWeakPointer::CheckTypes();
129 }
130
131 template <typename U>
134 { // Create a new reference on copy
135 vtkWeakPointer::CheckTypes<U>();
136 }
138
140
144 {
146 return *this;
147 }
148
149 template <class U>
151 {
152 vtkWeakPointer::CheckTypes<U>();
153
155 return *this;
156 }
158
160
164 {
165 this->vtkWeakPointerBase::operator=(std::move(r));
166 return *this;
167 }
168
169 template <class U>
171 {
172 vtkWeakPointer::CheckTypes<U>();
173
174 this->vtkWeakPointerBase::operator=(std::move(r));
175 return *this;
176 }
178
180
184 {
185 vtkWeakPointer::CheckTypes();
187 return *this;
188 }
189
190 template <typename U>
192 {
193 vtkWeakPointer::CheckTypes<U>();
194
195 this->vtkWeakPointerBase::operator=(r.Object);
196 return *this;
197 }
199
201
204 T* GetPointer() const noexcept { return static_cast<T*>(this->Object); }
205 T* Get() const noexcept { return static_cast<T*>(this->Object); }
206 operator T*() const noexcept { return static_cast<T*>(this->Object); }
207
212 T& operator*() const noexcept { return *static_cast<T*>(this->Object); }
213
217 T* operator->() const noexcept { return static_cast<T*>(this->Object); }
218
219 // Work-around for HP and IBM overload resolution bug. Since
220 // NullPointerOnly is a private type the only pointer value that can
221 // be passed by user code is a null pointer. This operator will be
222 // chosen by the compiler when comparing against null explicitly and
223 // avoid the bogus ambiguous overload error.
224#if defined(__HP_aCC) || defined(__IBMCPP__)
225#define VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(op) \
226 bool operator op(NullPointerOnly*) const { return ::operator op(*this, 0); }
227
228private:
229 class NullPointerOnly
230 {
231 };
232
233public:
234 VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(==)
235 VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(!=)
236 VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(<)
237 VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(<=)
238 VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(>)
239 VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(>=)
240#undef VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND
241#endif
242protected:
244 : vtkWeakPointerBase(r, n)
245 {
246 }
247
248private:
249 // These are purposely not implemented to prevent callers from
250 // trying to take references from other smart pointers.
251 void TakeReference(const vtkWeakPointerBase&) = delete;
252 static void Take(const vtkWeakPointerBase&) = delete;
253};
254
255#define VTK_WEAK_POINTER_DEFINE_OPERATOR(op) \
256 template <class T, class U> \
257 inline bool operator op(const vtkWeakPointer<T>& l, const vtkWeakPointer<U>& r) \
258 { \
259 return (l.GetPointer() op r.GetPointer()); \
260 } \
261 template <class T, class U> \
262 inline bool operator op(T* l, const vtkWeakPointer<U>& r) \
263 { \
264 return (l op r.GetPointer()); \
265 } \
266 template <class T, class U> \
267 inline bool operator op(const vtkWeakPointer<T>& l, U* r) \
268 { \
269 return (l.GetPointer() op r); \
270 } \
271 template <class T, class U> \
272 inline bool operator op(const vtkNew<T>& l, const vtkWeakPointer<U>& r) \
273 { \
274 return (l.GetPointer() op r.GetPointer()); \
275 } \
276 template <class T, class U> \
277 inline bool operator op(const vtkWeakPointer<T>& l, const vtkNew<U>& r) \
278 { \
279 return (l.GetPointer() op r.GetPointer); \
280 }
281
291
292#undef VTK_WEAK_POINTER_DEFINE_OPERATOR
293
294namespace vtk
295{
296
299template <typename T>
301{
302 return vtkWeakPointer<T>(obj);
303}
304
305} // end namespace vtk
306
310template <class T>
311inline ostream& operator<<(ostream& os, const vtkWeakPointer<T>& p)
312{
313 return os << static_cast<const vtkWeakPointerBase&>(p);
314}
315
316#endif
317
318// VTK-HeaderTest-Exclude: vtkWeakPointer.h
Allocate and hold a VTK object.
Definition: vtkNew.h:165
Non-templated superclass for vtkWeakPointer.
vtkObjectBase * Object
vtkWeakPointerBase & operator=(vtkObjectBase *r)
Assign object to reference.
vtkWeakPointerBase() noexcept
Initialize smart pointer to nullptr.
a weak reference to a vtkObject.
T * GetPointer() const noexcept
Get the contained pointer.
vtkWeakPointer & operator=(vtkWeakPointer< U > &&r) noexcept
Move r's object into this weak pointer, setting r to nullptr.
vtkWeakPointer & operator=(const vtkNew< U > &r)
Assign object to reference.
vtkWeakPointer(const vtkWeakPointer< U > &r)
Initialize smart pointer with the given smart pointer.
vtkWeakPointer() noexcept
Initialize smart pointer to nullptr.
vtkWeakPointer(vtkWeakPointer< U > &&r) noexcept
Initialize smart pointer with the given smart pointer.
vtkWeakPointer(vtkWeakPointer &&r) noexcept
Move r's object into the new weak pointer, setting r to nullptr.
vtkWeakPointer & operator=(const vtkWeakPointer &r)
Assign object to reference.
vtkWeakPointer & operator=(vtkWeakPointer &&r) noexcept
Move r's object into this weak pointer, setting r to nullptr.
T & operator*() const noexcept
Dereference the pointer and return a reference to the contained object.
vtkWeakPointer & operator=(const vtkWeakPointer< U > &r)
Assign object to reference.
vtkWeakPointer(T *r, const NoReference &n)
Get the contained pointer.
vtkWeakPointer & operator=(T *r)
Assign object to reference.
vtkWeakPointer(const vtkWeakPointer &r)
Initialize smart pointer with the given smart pointer.
T * Get() const noexcept
Get the contained pointer.
vtkWeakPointer(T *r)
Initialize smart pointer to given object.
T * operator->() const noexcept
Provides normal pointer target member access using operator ->.
vtkWeakPointer(const vtkNew< U > &r)
Initialize smart pointer with the given smart pointer.
Specialization of tuple ranges and iterators for vtkAOSDataArrayTemplate.
vtkWeakPointer< T > TakeWeakPointer(T *obj)
Construct a vtkWeakPointer<T> containing obj.
This file contains a variety of metaprogramming constructs for working with vtk types.
ostream & operator<<(ostream &os, const vtkWeakPointer< T > &p)
Streaming operator to print smart pointer like regular pointers.
#define VTK_WEAK_POINTER_DEFINE_OPERATOR(op)