VTK  9.2.6
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 =========================================================================*/
38 #ifndef vtkSmartPointer_h
39 #define vtkSmartPointer_h
40 
41 #include "vtkSmartPointerBase.h"
42 
43 #include "vtkMeta.h" // for IsComplete
44 #include "vtkNew.h" // for vtkNew.h
45 
46 #include <type_traits> // for is_base_of
47 #include <utility> // for std::move
48 
49 template <class T>
51 {
52  // These static asserts only fire when the function calling CheckTypes is
53  // used. Thus, this smart pointer class may still be used as a member variable
54  // with a forward declared T, so long as T is defined by the time the calling
55  // function is used.
56  template <typename U = T>
57  static void CheckTypes() noexcept
58  {
60  "vtkSmartPointer<T>'s T type has not been defined. Missing "
61  "include?");
63  "Cannot store an object with undefined type in "
64  "vtkSmartPointer. Missing include?");
65  static_assert(std::is_base_of<T, U>::value,
66  "Argument type is not compatible with vtkSmartPointer<T>'s "
67  "T type.");
69  "vtkSmartPointer can only be used with subclasses of "
70  "vtkObjectBase.");
71  }
72 
73 public:
77  vtkSmartPointer() noexcept
79  {
80  }
81 
87  // Need both overloads because the copy-constructor must be non-templated:
90  {
91  }
92 
93  template <class U>
96  {
97  vtkSmartPointer::CheckTypes<U>();
98  }
99  /* @} **/
100 
105  // Need both overloads because the move-constructor must be non-templated:
107  : vtkSmartPointerBase(std::move(r))
108  {
109  }
110 
111  template <class U>
113  : vtkSmartPointerBase(std::move(r))
114  {
115  vtkSmartPointer::CheckTypes<U>();
116  }
125  {
126  vtkSmartPointer::CheckTypes();
127  }
128 
129  template <typename U>
132  { // Create a new reference on copy
133  vtkSmartPointer::CheckTypes<U>();
134  }
136 
141  template <typename U>
144  { // Steal the reference on move
145  vtkSmartPointer::CheckTypes<U>();
146 
147  r.Object = nullptr;
148  }
149 
151 
155  // Need this since the compiler won't recognize template functions as
156  // assignment operators.
158  {
160  return *this;
161  }
162 
163  template <class U>
165  {
166  vtkSmartPointer::CheckTypes<U>();
167 
169  return *this;
170  }
172 
177  template <typename U>
179  {
180  vtkSmartPointer::CheckTypes<U>();
181 
182  this->vtkSmartPointerBase::operator=(r.Object);
183  return *this;
184  }
185 
190  template <typename U>
192  {
193  vtkSmartPointer::CheckTypes<U>();
194 
196  return *this;
197  }
198 
200 
203  T* GetPointer() const noexcept { return static_cast<T*>(this->Object); }
204  T* Get() const noexcept { return static_cast<T*>(this->Object); }
206 
210  operator T*() const noexcept { return static_cast<T*>(this->Object); }
211 
216  T& operator*() const noexcept { return *static_cast<T*>(this->Object); }
217 
221  T* operator->() const noexcept { return static_cast<T*>(this->Object); }
222 
235  void TakeReference(T* t) { *this = vtkSmartPointer<T>(t, NoReference()); }
236 
240  static vtkSmartPointer<T> New() { return vtkSmartPointer<T>(T::New(), NoReference()); }
247  {
248  return vtkSmartPointer<T>(T::ExtendedNew(), NoReference());
249  }
250 
255  {
256  return vtkSmartPointer<T>(t->NewInstance(), NoReference());
257  }
258 
272  static vtkSmartPointer<T> Take(T* t) { return vtkSmartPointer<T>(t, NoReference()); }
273 
274  // Work-around for HP and IBM overload resolution bug. Since
275  // NullPointerOnly is a private type the only pointer value that can
276  // be passed by user code is a null pointer. This operator will be
277  // chosen by the compiler when comparing against null explicitly and
278  // avoid the bogus ambiguous overload error.
279 #if defined(__HP_aCC) || defined(__IBMCPP__)
280 #define VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(op) \
281  bool operator op(NullPointerOnly*) const { return ::operator op(*this, 0); }
282 
283 private:
284  class NullPointerOnly
285  {
286  };
287 
288 public:
289  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(==)
290  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(!=)
291  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(<)
292  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(<=)
293  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(>)
294  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(>=)
295 #undef VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND
296 #endif
297 protected:
298  vtkSmartPointer(T* r, const NoReference& n)
299  : vtkSmartPointerBase(r, n)
300  {
301  }
302 
303 private:
304  // These are purposely not implemented to prevent callers from
305  // trying to take references from other smart pointers.
306  void TakeReference(const vtkSmartPointerBase&) = delete;
307  static void Take(const vtkSmartPointerBase&) = delete;
308 };
309 
310 #define VTK_SMART_POINTER_DEFINE_OPERATOR(op) \
311  template <class T, class U> \
312  inline bool operator op(const vtkSmartPointer<T>& l, const vtkSmartPointer<U>& r) \
313  { \
314  return (l.GetPointer() op r.GetPointer()); \
315  } \
316  template <class T, class U> \
317  inline bool operator op(T* l, const vtkSmartPointer<U>& r) \
318  { \
319  return (l op r.GetPointer()); \
320  } \
321  template <class T, class U> \
322  inline bool operator op(const vtkSmartPointer<T>& l, U* r) \
323  { \
324  return (l.GetPointer() op r); \
325  } \
326  template <class T, class U> \
327  inline bool operator op(const vtkNew<T>& l, const vtkSmartPointer<U>& r) \
328  { \
329  return (l.GetPointer() op r.GetPointer()); \
330  } \
331  template <class T, class U> \
332  inline bool operator op(const vtkSmartPointer<T>& l, const vtkNew<U>& r) \
333  { \
334  return (l.GetPointer() op r.GetPointer); \
335  }
336 
346 
347 #undef VTK_SMART_POINTER_DEFINE_OPERATOR
348 
349 namespace vtk
350 {
351 
354 template <typename T>
356 {
357  return vtkSmartPointer<T>{ obj };
358 }
359 
362 template <typename T>
364 {
365  return vtkSmartPointer<T>::Take(obj);
366 }
367 
368 } // end namespace vtk
369 
373 template <class T>
374 inline ostream& operator<<(ostream& os, const vtkSmartPointer<T>& p)
375 {
376  return os << static_cast<const vtkSmartPointerBase&>(p);
377 }
378 
379 #endif
380 // VTK-HeaderTest-Exclude: vtkSmartPointer.h
Allocate and hold a VTK object.
Definition: vtkNew.h:71
Non-templated superclass for vtkSmartPointer.
vtkSmartPointerBase() noexcept
Initialize smart pointer to nullptr.
vtkSmartPointerBase & operator=(vtkObjectBase *r)
Assign object to reference.
vtkObjectBase * Object
Hold a reference to a vtkObjectBase instance.
vtkSmartPointer() noexcept
Initialize smart pointer to nullptr.
T * Get() const noexcept
Get the contained pointer.
vtkSmartPointer(vtkNew< U > &&r) noexcept
Move the pointer from the vtkNew smart pointer to the new vtkSmartPointer, stealing its reference and...
static vtkSmartPointer< T > NewInstance(T *t)
Create a new instance of the given VTK object.
vtkSmartPointer(const vtkSmartPointer &r)
Initialize smart pointer with a new reference to the same object referenced by given smart pointer.
vtkSmartPointer(T *r)
Initialize smart pointer to given object.
vtkSmartPointer(const vtkSmartPointer< U > &r)
Initialize smart pointer with a new reference to the same object referenced by given smart pointer.
void TakeReference(T *t)
Transfer ownership of one reference to the given VTK object to this smart pointer.
vtkSmartPointer(vtkSmartPointer &&r) noexcept
Move the contents of r into this.
vtkSmartPointer & operator=(const vtkSmartPointer< U > &r)
Assign object to reference.
static vtkSmartPointer< T > New()
Create an instance of a VTK object.
vtkSmartPointer(vtkSmartPointer< U > &&r) noexcept
Initialize smart pointer with a new reference to the same object referenced by given smart pointer.
vtkSmartPointer & operator=(const vtkNew< U > &r)
Assign object to reference.
vtkSmartPointer & operator=(U *r)
Assign object to reference.
T & operator*() const noexcept
Dereference the pointer and return a reference to the contained object.
T * GetPointer() const noexcept
Get the contained pointer.
T * operator->() const noexcept
Provides normal pointer target member access using operator ->.
static vtkSmartPointer< T > ExtendedNew()
Create an instance of a VTK object in a memkind extended memory space.
vtkSmartPointer(T *r, const NoReference &n)
vtkSmartPointer & operator=(const vtkSmartPointer &r)
Assign object to reference.
static vtkSmartPointer< T > Take(T *t)
Transfer ownership of one reference to the given VTK object to a new smart pointer.
vtkSmartPointer(const vtkNew< U > &r)
Initialize smart pointer to given object.
@ value
Definition: vtkX3D.h:226
Specialization of tuple ranges and iterators for vtkAOSDataArrayTemplate.
vtkSmartPointer< T > TakeSmartPointer(T *obj)
Construct a vtkSmartPointer<T> containing obj.
vtkSmartPointer< T > MakeSmartPointer(T *obj)
Construct a vtkSmartPointer<T> containing obj.
This file contains a variety of metaprogramming constructs for working with vtk types.
#define VTK_SMART_POINTER_DEFINE_OPERATOR(op)
ostream & operator<<(ostream &os, const vtkSmartPointer< T > &p)
Streaming operator to print smart pointer like regular pointers.