VTK
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 =========================================================================*/
23 #ifndef vtkSmartPointer_h
24 #define vtkSmartPointer_h
25 
26 #include "vtkSmartPointerBase.h"
27 
28 #include "vtkMeta.h" // for IsComplete
29 #include "vtkNew.h" // for vtkNew.h
30 
31 #include <type_traits> // for is_base_of
32 #include <utility> // for std::move
33 
34 template <class T>
36 {
37  // These static asserts only fire when the function calling CheckTypes is
38  // used. Thus, this smart pointer class may still be used as a member variable
39  // with a forward declared T, so long as T is defined by the time the calling
40  // function is used.
41  template <typename U = T>
42  static void CheckTypes() noexcept
43  {
45  "vtkSmartPointer<T>'s T type has not been defined. Missing "
46  "include?");
48  "Cannot store an object with undefined type in "
49  "vtkSmartPointer. Missing include?");
50  static_assert(std::is_base_of<T, U>::value,
51  "Argument type is not compatible with vtkSmartPointer<T>'s "
52  "T type.");
54  "vtkSmartPointer can only be used with subclasses of "
55  "vtkObjectBase.");
56  }
57 
58 public:
63 
69  // Need both overloads because the copy-constructor must be non-templated:
72  {
73  }
74 
75  template <class U>
78  {
79  vtkSmartPointer::CheckTypes<U>();
80  }
81  /* @} **/
82 
87  // Need both overloads because the move-constructor must be non-templated:
88  vtkSmartPointer(vtkSmartPointer&& r) noexcept : vtkSmartPointerBase(std::move(r)) {}
89 
90  template <class U>
92  {
93  vtkSmartPointer::CheckTypes<U>();
94  }
103  {
104  vtkSmartPointer::CheckTypes();
105  }
106 
107  template <typename U>
110  { // Create a new reference on copy
111  vtkSmartPointer::CheckTypes<U>();
112  }
114 
119  template <typename U>
122  { // Steal the reference on move
123  vtkSmartPointer::CheckTypes<U>();
124 
125  r.Object = nullptr;
126  }
127 
129 
133  // Need this since the compiler won't recognize template functions as
134  // assignment operators.
136  {
138  return *this;
139  }
140 
141  template <class U>
143  {
144  vtkSmartPointer::CheckTypes<U>();
145 
147  return *this;
148  }
150 
155  template <typename U>
157  {
158  vtkSmartPointer::CheckTypes<U>();
159 
160  this->vtkSmartPointerBase::operator=(r.Object);
161  return *this;
162  }
163 
168  template <typename U>
170  {
171  vtkSmartPointer::CheckTypes<U>();
172 
174  return *this;
175  }
176 
178 
181  T* GetPointer() const noexcept { return static_cast<T*>(this->Object); }
182  T* Get() const noexcept { return static_cast<T*>(this->Object); }
184 
188  operator T*() const noexcept { return static_cast<T*>(this->Object); }
189 
194  T& operator*() const noexcept { return *static_cast<T*>(this->Object); }
195 
199  T* operator->() const noexcept { return static_cast<T*>(this->Object); }
200 
213  void TakeReference(T* t) { *this = vtkSmartPointer<T>(t, NoReference()); }
214 
218  static vtkSmartPointer<T> New() { return vtkSmartPointer<T>(T::New(), NoReference()); }
219 
224  {
225  return vtkSmartPointer<T>(t->NewInstance(), NoReference());
226  }
227 
241  static vtkSmartPointer<T> Take(T* t) { return vtkSmartPointer<T>(t, NoReference()); }
242 
243  // Work-around for HP and IBM overload resolution bug. Since
244  // NullPointerOnly is a private type the only pointer value that can
245  // be passed by user code is a null pointer. This operator will be
246  // chosen by the compiler when comparing against null explicitly and
247  // avoid the bogus ambiguous overload error.
248 #if defined(__HP_aCC) || defined(__IBMCPP__)
249 #define VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(op) \
250  bool operator op(NullPointerOnly*) const { return ::operator op(*this, 0); }
251 
252 private:
253  class NullPointerOnly
254  {
255  };
256 
257 public:
258  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(==)
259  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(!=)
260  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(<)
261  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(<=)
262  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(>)
263  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(>=)
264 #undef VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND
265 #endif
266 protected:
267  vtkSmartPointer(T* r, const NoReference& n)
268  : vtkSmartPointerBase(r, n)
269  {
270  }
271 
272 private:
273  // These are purposely not implemented to prevent callers from
274  // trying to take references from other smart pointers.
275  void TakeReference(const vtkSmartPointerBase&) = delete;
276  static void Take(const vtkSmartPointerBase&) = delete;
277 };
278 
279 #define VTK_SMART_POINTER_DEFINE_OPERATOR(op) \
280  template <class T, class U> \
281  inline bool operator op(const vtkSmartPointer<T>& l, const vtkSmartPointer<U>& r) \
282  { \
283  return (l.GetPointer() op r.GetPointer()); \
284  } \
285  template <class T, class U> \
286  inline bool operator op(T* l, const vtkSmartPointer<U>& r) \
287  { \
288  return (l op r.GetPointer()); \
289  } \
290  template <class T, class U> \
291  inline bool operator op(const vtkSmartPointer<T>& l, U* r) \
292  { \
293  return (l.GetPointer() op r); \
294  } \
295  template <class T, class U> \
296  inline bool operator op(const vtkNew<T>& l, const vtkSmartPointer<U>& r) \
297  { \
298  return (l.GetPointer() op r.GetPointer()); \
299  } \
300  template <class T, class U> \
301  inline bool operator op(const vtkSmartPointer<T>& l, const vtkNew<U>& r) \
302  { \
303  return (l.GetPointer() op r.GetPointer); \
304  }
305 
315 
316 #undef VTK_SMART_POINTER_DEFINE_OPERATOR
317 
318 namespace vtk
319 {
320 
323 template <typename T>
325 {
326  return vtkSmartPointer<T>{ obj };
327 }
328 
331 template <typename T>
333 {
334  return vtkSmartPointer<T>::Take(obj);
335 }
336 
337 } // end namespace vtk
338 
342 template <class T>
343 inline ostream& operator<<(ostream& os, const vtkSmartPointer<T>& p)
344 {
345  return os << static_cast<const vtkSmartPointerBase&>(p);
346 }
347 
348 #endif
349 // VTK-HeaderTest-Exclude: vtkSmartPointer.h
vtkSmartPointerBase::operator=
vtkSmartPointerBase & operator=(vtkObjectBase *r)
Assign object to reference.
vtkX3D::value
Definition: vtkX3D.h:226
vtkSmartPointerBase
Non-templated superclass for vtkSmartPointer.
Definition: vtkSmartPointerBase.h:32
vtkMeta.h
vtkSmartPointer::operator=
vtkSmartPointer & operator=(const vtkSmartPointer< U > &r)
Initialize smart pointer to given object.
Definition: vtkSmartPointer.h:142
vtkSmartPointer::GetPointer
T * GetPointer() const noexcept
Get the contained pointer.
Definition: vtkSmartPointer.h:181
vtkSmartPointer::Take
static vtkSmartPointer< T > Take(T *t)
Transfer ownership of one reference to the given VTK object to a new smart pointer.
Definition: vtkSmartPointer.h:241
vtkSmartPointer
Hold a reference to a vtkObjectBase instance.
Definition: vtkMeta.h:32
vtkSmartPointerBase::vtkSmartPointerBase
vtkSmartPointerBase() noexcept
Initialize smart pointer to nullptr.
vtk::detail::IsComplete
Definition: vtkMeta.h:76
vtkSmartPointer::operator=
vtkSmartPointer & operator=(const vtkSmartPointer &r)
Assign object to reference.
Definition: vtkSmartPointer.h:135
vtkSmartPointer::operator=
vtkSmartPointer & operator=(U *r)
Assign object to reference.
Definition: vtkSmartPointer.h:169
vtkSmartPointer::vtkSmartPointer
vtkSmartPointer(const vtkSmartPointer< U > &r)
Initialize smart pointer with a new reference to the same object referenced by given smart pointer.
Definition: vtkSmartPointer.h:76
vtkSmartPointer::vtkSmartPointer
vtkSmartPointer(T *r)
Initialize smart pointer to given object.
Definition: vtkSmartPointer.h:101
vtkSmartPointer::vtkSmartPointer
vtkSmartPointer(vtkNew< U > &&r) noexcept
Move the pointer from the vtkNew smart pointer to the new vtkSmartPointer, stealing its reference and...
Definition: vtkSmartPointer.h:120
vtkSmartPointer::vtkSmartPointer
vtkSmartPointer() noexcept
Initialize smart pointer to nullptr.
Definition: vtkSmartPointer.h:62
vtkSmartPointer::vtkSmartPointer
vtkSmartPointer(vtkSmartPointer &&r) noexcept
Move the contents of r into this.
Definition: vtkSmartPointer.h:88
operator<<
ostream & operator<<(ostream &os, const vtkSmartPointer< T > &p)
Streaming operator to print smart pointer like regular pointers.
Definition: vtkSmartPointer.h:343
vtkSmartPointer::vtkSmartPointer
vtkSmartPointer(const vtkSmartPointer &r)
Initialize smart pointer with a new reference to the same object referenced by given smart pointer.
Definition: vtkSmartPointer.h:70
vtkSmartPointer::Get
T * Get() const noexcept
Initialize smart pointer to given object.
Definition: vtkSmartPointer.h:182
vtkSmartPointer::New
static vtkSmartPointer< T > New()
Create an instance of a VTK object.
Definition: vtkSmartPointer.h:218
VTK_SMART_POINTER_DEFINE_OPERATOR
#define VTK_SMART_POINTER_DEFINE_OPERATOR(op)
Definition: vtkSmartPointer.h:279
vtkNew
Allocate and hold a VTK object.
Definition: vtkMeta.h:30
vtkSmartPointer::operator->
T * operator->() const noexcept
Provides normal pointer target member access using operator ->.
Definition: vtkSmartPointer.h:199
vtkSmartPointer::operator*
T & operator*() const noexcept
Dereference the pointer and return a reference to the contained object.
Definition: vtkSmartPointer.h:194
vtkNew.h
vtk::MakeSmartPointer
vtkSmartPointer< T > MakeSmartPointer(T *obj)
Construct a vtkSmartPointer<T> containing obj.
Definition: vtkSmartPointer.h:324
vtk
Specialization of tuple ranges and iterators for vtkAOSDataArrayTemplate.
Definition: vtkAtomicTypeConcepts.h:21
vtk::TakeSmartPointer
vtkSmartPointer< T > TakeSmartPointer(T *obj)
Construct a vtkSmartPointer<T> containing obj.
Definition: vtkSmartPointer.h:332
vtkSmartPointerBase.h
vtkSmartPointer::vtkSmartPointer
vtkSmartPointer(vtkSmartPointer< U > &&r) noexcept
Initialize smart pointer with a new reference to the same object referenced by given smart pointer.
Definition: vtkSmartPointer.h:91
vtkSmartPointer::NewInstance
static vtkSmartPointer< T > NewInstance(T *t)
Create a new instance of the given VTK object.
Definition: vtkSmartPointer.h:223
vtkSmartPointer::vtkSmartPointer
vtkSmartPointer(T *r, const NoReference &n)
Initialize smart pointer to given object.
Definition: vtkSmartPointer.h:267
vtkSmartPointerBase::Object
vtkObjectBase * Object
Definition: vtkSmartPointerBase.h:95
vtkSmartPointer::vtkSmartPointer
vtkSmartPointer(const vtkNew< U > &r)
Initialize smart pointer to given object.
Definition: vtkSmartPointer.h:108
vtkSmartPointer::TakeReference
void TakeReference(T *t)
Transfer ownership of one reference to the given VTK object to this smart pointer.
Definition: vtkSmartPointer.h:213
vtkSmartPointer::operator=
vtkSmartPointer & operator=(const vtkNew< U > &r)
Assign object to reference.
Definition: vtkSmartPointer.h:156
vtkSmartPointerBase::NoReference
Definition: vtkSmartPointerBase.h:89