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 =========================================================================*/
29 #ifndef vtkSmartPointer_h
30 #define vtkSmartPointer_h
31 
32 #include "vtkSmartPointerBase.h"
33 #include "vtkNew.h" // For converting New pointers to Smart pointers
34 
35 template <class T>
37 {
38  static T* CheckType(T* t) { return t; }
39 public:
44 
49 
54 
59  template <class U>
61  vtkSmartPointerBase(CheckType(r.GetPointer())) {}
62 
64 
69  {
71  return *this;
72  }
74 
76 
81  {
83  return *this;
84  }
86 
88 
92  template <class U>
94  {
95  this->vtkSmartPointerBase::operator=(CheckType(r.GetPointer()));
96  return *this;
97  }
99 
101 
104  T* GetPointer() const
105  {
106  return static_cast<T*>(this->Object);
107  }
108  T* Get() const
109  {
110  return static_cast<T*>(this->Object);
111  }
113 
117  operator T* () const
118  {
119  return static_cast<T*>(this->Object);
120  }
121 
126  T& operator*() const
127  {
128  return *static_cast<T*>(this->Object);
129  }
130 
134  T* operator->() const
135  {
136  return static_cast<T*>(this->Object);
137  }
138 
151  void TakeReference(T* t)
152  {
153  *this = vtkSmartPointer<T>(t, NoReference());
154  }
155 
160  {
161  return vtkSmartPointer<T>(T::New(), NoReference());
162  }
163 
168  {
169  return vtkSmartPointer<T>(t->NewInstance(), NoReference());
170  }
171 
186  {
187  return vtkSmartPointer<T>(t, NoReference());
188  }
189 
190  // Work-around for HP and IBM overload resolution bug. Since
191  // NullPointerOnly is a private type the only pointer value that can
192  // be passed by user code is a null pointer. This operator will be
193  // chosen by the compiler when comparing against null explicitly and
194  // avoid the bogus ambiguous overload error.
195 #if defined(__HP_aCC) || defined(__IBMCPP__)
196 # define VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(op) \
197  bool operator op (NullPointerOnly*) const \
198  { \
199  return ::operator op (*this, 0); \
200  }
201 private:
202  class NullPointerOnly {};
203 public:
204  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(==)
205  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(!=)
206  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(<)
207  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(<=)
208  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(>)
209  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(>=)
210 # undef VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND
211 #endif
212 protected:
214 private:
215  // These are purposely not implemented to prevent callers from
216  // trying to take references from other smart pointers.
217  void TakeReference(const vtkSmartPointerBase&) = delete;
218  static void Take(const vtkSmartPointerBase&) = delete;
219 };
220 
221 #define VTK_SMART_POINTER_DEFINE_OPERATOR(op) \
222  template <class T> \
223  inline bool \
224  operator op (const vtkSmartPointer<T>& l, const vtkSmartPointer<T>& r) \
225  { \
226  return (l.GetPointer() op r.GetPointer()); \
227  } \
228  template <class T> \
229  inline bool operator op (T* l, const vtkSmartPointer<T>& r) \
230  { \
231  return (l op r.GetPointer()); \
232  } \
233  template <class T> \
234  inline bool operator op (const vtkSmartPointer<T>& l, T* r) \
235  { \
236  return (l.GetPointer() op r); \
237  }
238 
247 
248 #undef VTK_SMART_POINTER_DEFINE_OPERATOR
249 
253 template <class T>
254 inline ostream& operator << (ostream& os, const vtkSmartPointer<T>& p)
255 {
256  return os << static_cast<const vtkSmartPointerBase&>(p);
257 }
258 
259 #endif
260 // VTK-HeaderTest-Exclude: vtkSmartPointer.h
vtkSmartPointer()
Initialize smart pointer to nullptr.
static vtkSmartPointer< T > Take(T *t)
Transfer ownership of one reference to the given VTK object to a new smart pointer.
static vtkSmartPointer< T > NewInstance(T *t)
Create a new instance of the given VTK object.
vtkSmartPointerBase & operator=(vtkObjectBase *r)
Assign object to reference.
vtkSmartPointer(T *r, const NoReference &n)
vtkSmartPointer & operator=(T *r)
Assign object to reference.
Hold a reference to a vtkObjectBase instance.
static vtkSmartPointer< T > New()
Create an instance of a VTK object.
vtkSmartPointer & operator=(const vtkSmartPointer< U > &r)
Assign object to reference.
vtkSmartPointer(T *r)
Initialize smart pointer to given object.
vtkSmartPointer(vtkNew< T > &r)
Initialize smart pointer to given object.
vtkSmartPointer & operator=(vtkNew< T > &r)
Assign object to reference.
Non-templated superclass for vtkSmartPointer.
vtkObjectBase * Object
void TakeReference(T *t)
Transfer ownership of one reference to the given VTK object to this smart pointer.
T * operator->() const
Provides normal pointer target member access using operator ->.
Allocate and hold a VTK object.
Definition: vtkNew.h:58
T * GetPointer() const
Get the contained pointer.
vtkSmartPointer(const vtkSmartPointer< U > &r)
Initialize smart pointer with a new reference to the same object referenced by given smart pointer...
T & operator*() const
Dereference the pointer and return a reference to the contained object.
#define VTK_SMART_POINTER_DEFINE_OPERATOR(op)
T * Get() const
Get the contained pointer.