VTK  9.0.2
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 
54 template <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.");
74  "vtkWeakPointer can only be used with subclasses of "
75  "vtkObjectBase.");
76  }
77 
78 public:
83 
90  {
91  }
92 
93  template <class U>
96  {
97  vtkWeakPointer::CheckTypes<U>();
98  }
99  /* @} **/
100 
105  vtkWeakPointer(vtkWeakPointer&& r) noexcept : vtkWeakPointerBase(std::move(r)) {}
106 
107  template <class U>
109  {
110  vtkWeakPointer::CheckTypes<U>();
111  }
112  /* @} **/
113 
119  : vtkWeakPointerBase(r)
120  {
121  vtkWeakPointer::CheckTypes();
122  }
123 
124  template <typename U>
127  { // Create a new reference on copy
128  vtkWeakPointer::CheckTypes<U>();
129  }
131 
133 
137  {
139  return *this;
140  }
141 
142  template <class U>
144  {
145  vtkWeakPointer::CheckTypes<U>();
146 
148  return *this;
149  }
151 
153 
157  {
158  this->vtkWeakPointerBase::operator=(std::move(r));
159  return *this;
160  }
161 
162  template <class U>
164  {
165  vtkWeakPointer::CheckTypes<U>();
166 
167  this->vtkWeakPointerBase::operator=(std::move(r));
168  return *this;
169  }
171 
173 
177  {
178  vtkWeakPointer::CheckTypes();
180  return *this;
181  }
182 
183  template <typename U>
185  {
186  vtkWeakPointer::CheckTypes<U>();
187 
188  this->vtkWeakPointerBase::operator=(r.Object);
189  return *this;
190  }
192 
194 
197  T* GetPointer() const noexcept { return static_cast<T*>(this->Object); }
198  T* Get() const noexcept { return static_cast<T*>(this->Object); }
199  operator T*() const noexcept { return static_cast<T*>(this->Object); }
200 
205  T& operator*() const noexcept { return *static_cast<T*>(this->Object); }
206 
210  T* operator->() const noexcept { return static_cast<T*>(this->Object); }
211 
212  // Work-around for HP and IBM overload resolution bug. Since
213  // NullPointerOnly is a private type the only pointer value that can
214  // be passed by user code is a null pointer. This operator will be
215  // chosen by the compiler when comparing against null explicitly and
216  // avoid the bogus ambiguous overload error.
217 #if defined(__HP_aCC) || defined(__IBMCPP__)
218 #define VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(op) \
219  bool operator op(NullPointerOnly*) const { return ::operator op(*this, 0); }
220 
221 private:
222  class NullPointerOnly
223  {
224  };
225 
226 public:
227  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(==)
228  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(!=)
229  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(<)
230  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(<=)
231  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(>)
232  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(>=)
233 #undef VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND
234 #endif
235 protected:
236  vtkWeakPointer(T* r, const NoReference& n)
237  : vtkWeakPointerBase(r, n)
238  {
239  }
240 
241 private:
242  // These are purposely not implemented to prevent callers from
243  // trying to take references from other smart pointers.
244  void TakeReference(const vtkWeakPointerBase&) = delete;
245  static void Take(const vtkWeakPointerBase&) = delete;
246 };
247 
248 #define VTK_WEAK_POINTER_DEFINE_OPERATOR(op) \
249  template <class T, class U> \
250  inline bool operator op(const vtkWeakPointer<T>& l, const vtkWeakPointer<U>& r) \
251  { \
252  return (l.GetPointer() op r.GetPointer()); \
253  } \
254  template <class T, class U> \
255  inline bool operator op(T* l, const vtkWeakPointer<U>& r) \
256  { \
257  return (l op r.GetPointer()); \
258  } \
259  template <class T, class U> \
260  inline bool operator op(const vtkWeakPointer<T>& l, U* r) \
261  { \
262  return (l.GetPointer() op r); \
263  } \
264  template <class T, class U> \
265  inline bool operator op(const vtkNew<T>& l, const vtkWeakPointer<U>& r) \
266  { \
267  return (l.GetPointer() op r.GetPointer()); \
268  } \
269  template <class T, class U> \
270  inline bool operator op(const vtkWeakPointer<T>& l, const vtkNew<U>& r) \
271  { \
272  return (l.GetPointer() op r.GetPointer); \
273  }
274 
284 
285 #undef VTK_WEAK_POINTER_DEFINE_OPERATOR
286 
287 namespace vtk
288 {
289 
292 template <typename T>
294 {
295  return vtkWeakPointer<T>(obj);
296 }
297 
298 } // end namespace vtk
299 
303 template <class T>
304 inline ostream& operator<<(ostream& os, const vtkWeakPointer<T>& p)
305 {
306  return os << static_cast<const vtkWeakPointerBase&>(p);
307 }
308 
309 #endif
310 
311 // VTK-HeaderTest-Exclude: vtkWeakPointer.h
Allocate and hold a VTK object.
Definition: vtkNew.h:56
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
Initialize smart pointer with the given smart pointer.
T * Get() const noexcept
Initialize smart pointer with the given smart pointer.
vtkWeakPointer & operator=(const vtkWeakPointer< U > &r)
Initialize smart pointer with the given smart pointer.
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)
Initialize smart pointer with the given smart pointer.
vtkWeakPointer(T *r, const NoReference &n)
Initialize smart pointer with the given smart 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.
#define VTK_WEAK_POINTER_DEFINE_OPERATOR(op)
ostream & operator<<(ostream &os, const vtkWeakPointer< T > &p)
Streaming operator to print smart pointer like regular pointers.