VTK  9.0.20210409
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:
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 
126  : vtkWeakPointerBase(r)
127  {
128  vtkWeakPointer::CheckTypes();
129  }
130 
131  template <typename U>
133  : vtkWeakPointerBase(r.Object)
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 
228 private:
229  class NullPointerOnly
230  {
231  };
232 
233 public:
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
242 protected:
243  vtkWeakPointer(T* r, const NoReference& n)
244  : vtkWeakPointerBase(r, n)
245  {
246  }
247 
248 private:
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 
294 namespace vtk
295 {
296 
299 template <typename T>
301 {
302  return vtkWeakPointer<T>(obj);
303 }
304 
305 } // end namespace vtk
306 
310 template <class T>
311 inline 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
vtkWeakPointer::operator=
vtkWeakPointer & operator=(const vtkWeakPointer &r)
Assign object to reference.
Definition: vtkWeakPointer.h:143
vtkWeakPointer::operator=
vtkWeakPointer & operator=(vtkWeakPointer< U > &&r) noexcept
Initialize smart pointer with the given smart pointer.
Definition: vtkWeakPointer.h:170
vtkX3D::value
@ value
Definition: vtkX3D.h:226
vtkWeakPointer::operator=
vtkWeakPointer & operator=(T *r)
Assign object to reference.
Definition: vtkWeakPointer.h:183
operator<<
ostream & operator<<(ostream &os, const vtkWeakPointer< T > &p)
Streaming operator to print smart pointer like regular pointers.
Definition: vtkWeakPointer.h:311
vtkWeakPointer::operator->
T * operator->() const noexcept
Provides normal pointer target member access using operator ->.
Definition: vtkWeakPointer.h:217
vtkMeta.h
vtkWeakPointer::operator=
vtkWeakPointer & operator=(const vtkNew< U > &r)
Initialize smart pointer with the given smart pointer.
Definition: vtkWeakPointer.h:191
vtk::detail::IsComplete
Definition: vtkMeta.h:77
vtkWeakPointer::operator=
vtkWeakPointer & operator=(vtkWeakPointer &&r) noexcept
Move r's object into this weak pointer, setting r to nullptr.
Definition: vtkWeakPointer.h:163
vtkWeakPointer::operator=
vtkWeakPointer & operator=(const vtkWeakPointer< U > &r)
Initialize smart pointer with the given smart pointer.
Definition: vtkWeakPointer.h:150
vtkWeakPointerBase.h
VTK_WEAK_POINTER_DEFINE_OPERATOR
#define VTK_WEAK_POINTER_DEFINE_OPERATOR(op)
Definition: vtkWeakPointer.h:255
vtkWeakPointer::vtkWeakPointer
vtkWeakPointer(vtkWeakPointer &&r) noexcept
Move r's object into the new weak pointer, setting r to nullptr.
Definition: vtkWeakPointer.h:108
vtkWeakPointer::vtkWeakPointer
vtkWeakPointer() noexcept
Initialize smart pointer to nullptr.
Definition: vtkWeakPointer.h:82
vtkWeakPointer::vtkWeakPointer
vtkWeakPointer(vtkWeakPointer< U > &&r) noexcept
Initialize smart pointer with the given smart pointer.
Definition: vtkWeakPointer.h:114
vtkWeakPointerBase
Non-templated superclass for vtkWeakPointer.
Definition: vtkWeakPointerBase.h:34
vtkWeakPointer::vtkWeakPointer
vtkWeakPointer(T *r)
Initialize smart pointer to given object.
Definition: vtkWeakPointer.h:125
vtkWeakPointer::vtkWeakPointer
vtkWeakPointer(T *r, const NoReference &n)
Initialize smart pointer with the given smart pointer.
Definition: vtkWeakPointer.h:243
vtkNew
Allocate and hold a VTK object.
Definition: vtkNew.h:56
vtkWeakPointer::GetPointer
T * GetPointer() const noexcept
Get the contained pointer.
Definition: vtkWeakPointer.h:204
vtkWeakPointer::vtkWeakPointer
vtkWeakPointer(const vtkWeakPointer &r)
Initialize smart pointer with the given smart pointer.
Definition: vtkWeakPointer.h:91
vtk::TakeWeakPointer
vtkWeakPointer< T > TakeWeakPointer(T *obj)
Construct a vtkWeakPointer<T> containing obj.
Definition: vtkWeakPointer.h:300
vtkNew.h
vtkWeakPointer::vtkWeakPointer
vtkWeakPointer(const vtkNew< U > &r)
Initialize smart pointer with the given smart pointer.
Definition: vtkWeakPointer.h:132
vtkWeakPointer::operator*
T & operator*() const noexcept
Dereference the pointer and return a reference to the contained object.
Definition: vtkWeakPointer.h:212
vtk
Specialization of tuple ranges and iterators for vtkAOSDataArrayTemplate.
Definition: vtkCollectionRange.h:29
vtkWeakPointer::vtkWeakPointer
vtkWeakPointer(const vtkWeakPointer< U > &r)
Initialize smart pointer with the given smart pointer.
Definition: vtkWeakPointer.h:97
vtkWeakPointerBase::operator=
vtkWeakPointerBase & operator=(vtkObjectBase *r)
Assign object to reference.
vtkWeakPointer
a weak reference to a vtkObject.
Definition: vtkWeakPointer.h:56
vtkWeakPointer::Get
T * Get() const noexcept
Initialize smart pointer with the given smart pointer.
Definition: vtkWeakPointer.h:205