VTK  9.2.20220630
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 =========================================================================*/
46 #ifndef vtkWeakPointer_h
47 #define vtkWeakPointer_h
48 
49 #include "vtkWeakPointerBase.h"
50 
51 #include "vtkMeta.h" // for IsComplete
52 #include "vtkNew.h" // for vtkNew
53 
54 #include <type_traits> // for is_base_of
55 #include <utility> // for std::move
56 
57 template <class T>
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  "vtkWeakPointer<T>'s T type has not been defined. Missing "
69  "include?");
71  "Cannot store an object with undefined type in "
72  "vtkWeakPointer. Missing include?");
73  static_assert(std::is_base_of<T, U>::value,
74  "Argument type is not compatible with vtkWeakPointer<T>'s "
75  "T type.");
77  "vtkWeakPointer can only be used with subclasses of "
78  "vtkObjectBase.");
79  }
80 
81 public:
85  vtkWeakPointer() noexcept
87  {
88  }
89 
96  {
97  }
98 
99  template <class U>
101  : vtkWeakPointerBase(r)
102  {
103  vtkWeakPointer::CheckTypes<U>();
104  }
105  /* @} **/
106 
112  : vtkWeakPointerBase(std::move(r))
113  {
114  }
115 
116  template <class U>
118  : vtkWeakPointerBase(std::move(r))
119  {
120  vtkWeakPointer::CheckTypes<U>();
121  }
122  /* @} **/
123 
129  : vtkWeakPointerBase(r)
130  {
131  vtkWeakPointer::CheckTypes();
132  }
133 
134  template <typename U>
137  { // Create a new reference on copy
138  vtkWeakPointer::CheckTypes<U>();
139  }
141 
143 
147  {
149  return *this;
150  }
151 
152  template <class U>
154  {
155  vtkWeakPointer::CheckTypes<U>();
156 
158  return *this;
159  }
161 
163 
167  {
168  this->vtkWeakPointerBase::operator=(std::move(r));
169  return *this;
170  }
171 
172  template <class U>
174  {
175  vtkWeakPointer::CheckTypes<U>();
176 
177  this->vtkWeakPointerBase::operator=(std::move(r));
178  return *this;
179  }
181 
183 
187  {
188  vtkWeakPointer::CheckTypes();
190  return *this;
191  }
192 
193  template <typename U>
195  {
196  vtkWeakPointer::CheckTypes<U>();
197 
198  this->vtkWeakPointerBase::operator=(r.Object);
199  return *this;
200  }
202 
204 
207  T* GetPointer() const noexcept { return static_cast<T*>(this->Object); }
208  T* Get() const noexcept { return static_cast<T*>(this->Object); }
209  operator T*() const noexcept { return static_cast<T*>(this->Object); }
210 
215  T& operator*() const noexcept { return *static_cast<T*>(this->Object); }
216 
220  T* operator->() const noexcept { return static_cast<T*>(this->Object); }
221 
222  // Work-around for HP and IBM overload resolution bug. Since
223  // NullPointerOnly is a private type the only pointer value that can
224  // be passed by user code is a null pointer. This operator will be
225  // chosen by the compiler when comparing against null explicitly and
226  // avoid the bogus ambiguous overload error.
227 #if defined(__HP_aCC) || defined(__IBMCPP__)
228 #define VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(op) \
229  bool operator op(NullPointerOnly*) const { return ::operator op(*this, 0); }
230 
231 private:
232  class NullPointerOnly
233  {
234  };
235 
236 public:
237  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(==)
238  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(!=)
239  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(<)
240  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(<=)
241  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(>)
242  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(>=)
243 #undef VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND
244 #endif
245 protected:
246  vtkWeakPointer(T* r, const NoReference& n)
247  : vtkWeakPointerBase(r, n)
248  {
249  }
250 
251 private:
252  // These are purposely not implemented to prevent callers from
253  // trying to take references from other smart pointers.
254  void TakeReference(const vtkWeakPointerBase&) = delete;
255  static void Take(const vtkWeakPointerBase&) = delete;
256 };
257 
258 #define VTK_WEAK_POINTER_DEFINE_OPERATOR(op) \
259  template <class T, class U> \
260  inline bool operator op(const vtkWeakPointer<T>& l, const vtkWeakPointer<U>& r) \
261  { \
262  return (l.GetPointer() op r.GetPointer()); \
263  } \
264  template <class T, class U> \
265  inline bool operator op(T* l, const vtkWeakPointer<U>& r) \
266  { \
267  return (l op r.GetPointer()); \
268  } \
269  template <class T, class U> \
270  inline bool operator op(const vtkWeakPointer<T>& l, U* r) \
271  { \
272  return (l.GetPointer() op r); \
273  } \
274  template <class T, class U> \
275  inline bool operator op(const vtkNew<T>& l, const vtkWeakPointer<U>& r) \
276  { \
277  return (l.GetPointer() op r.GetPointer()); \
278  } \
279  template <class T, class U> \
280  inline bool operator op(const vtkWeakPointer<T>& l, const vtkNew<U>& r) \
281  { \
282  return (l.GetPointer() op r.GetPointer); \
283  }
284 
294 
295 #undef VTK_WEAK_POINTER_DEFINE_OPERATOR
296 
297 namespace vtk
298 {
299 
302 template <typename T>
304 {
305  return vtkWeakPointer<T>(obj);
306 }
307 
308 } // end namespace vtk
309 
313 template <class T>
314 inline ostream& operator<<(ostream& os, const vtkWeakPointer<T>& p)
315 {
316  return os << static_cast<const vtkWeakPointerBase&>(p);
317 }
318 
319 #endif
320 
321 // VTK-HeaderTest-Exclude: vtkWeakPointer.h
Allocate and hold a VTK object.
Definition: vtkNew.h:171
Non-templated superclass for vtkWeakPointer.
vtkWeakPointerBase & operator=(vtkObjectBase *r)
Assign object to reference.
vtkObjectBase * Object
vtkWeakPointerBase() noexcept
Initialize smart pointer to nullptr.
a weak reference to a vtkObject.
T & operator*() const noexcept
Dereference the pointer and return a reference to the contained object.
vtkWeakPointer & operator=(vtkWeakPointer< U > &&r) noexcept
Move r's object into this weak pointer, setting r to nullptr.
T * Get() const noexcept
Get the contained pointer.
vtkWeakPointer & operator=(const vtkWeakPointer< U > &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
Provides normal pointer target member access using operator ->.
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 & operator=(const vtkWeakPointer &r)
Assign object to reference.
vtkWeakPointer & operator=(T *r)
Assign object to reference.
vtkWeakPointer(vtkWeakPointer &&r) noexcept
Move r's object into the new weak pointer, setting r to nullptr.
vtkWeakPointer & operator=(const vtkNew< U > &r)
Assign object to reference.
vtkWeakPointer(T *r, const NoReference &n)
Get the contained pointer.
vtkWeakPointer(const vtkWeakPointer &r)
Initialize smart pointer with the given smart pointer.
vtkWeakPointer(T *r)
Initialize smart pointer to given object.
T * GetPointer() const noexcept
Get the contained pointer.
vtkWeakPointer(const vtkNew< U > &r)
Initialize smart pointer with the given smart pointer.
@ value
Definition: vtkX3D.h:226
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.
#define VTK_WEAK_POINTER_DEFINE_OPERATOR(op)
ostream & operator<<(ostream &os, const vtkWeakPointer< T > &p)
Streaming operator to print smart pointer like regular pointers.