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 #include "vtkNew.h" // For converting New pointers to Smart pointers
28 
29 template <class T>
31 {
32  static T* CheckType(T* t) { return t; }
33 public:
38 
43 
48 
53  template <class U>
55  vtkSmartPointerBase(CheckType(r.GetPointer())) {}
56 
58 
63  {
65  return *this;
66  }
68 
70 
75  {
77  return *this;
78  }
80 
82 
86  template <class U>
88  {
89  this->vtkSmartPointerBase::operator=(CheckType(r.GetPointer()));
90  return *this;
91  }
93 
95 
98  T* GetPointer() const
99  {
100  return static_cast<T*>(this->Object);
101  }
102  T* Get() const
103  {
104  return static_cast<T*>(this->Object);
105  }
107 
111  operator T* () const
112  {
113  return static_cast<T*>(this->Object);
114  }
115 
120  T& operator*() const
121  {
122  return *static_cast<T*>(this->Object);
123  }
124 
128  T* operator->() const
129  {
130  return static_cast<T*>(this->Object);
131  }
132 
145  void TakeReference(T* t)
146  {
147  *this = vtkSmartPointer<T>(t, NoReference());
148  }
149 
154  {
155  return vtkSmartPointer<T>(T::New(), NoReference());
156  }
157 
162  {
163  return vtkSmartPointer<T>(t->NewInstance(), NoReference());
164  }
165 
180  {
181  return vtkSmartPointer<T>(t, NoReference());
182  }
183 
184  // Work-around for HP and IBM overload resolution bug. Since
185  // NullPointerOnly is a private type the only pointer value that can
186  // be passed by user code is a null pointer. This operator will be
187  // chosen by the compiler when comparing against null explicitly and
188  // avoid the bogus ambiguous overload error.
189 #if defined(__HP_aCC) || defined(__IBMCPP__)
190 # define VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(op) \
191  bool operator op (NullPointerOnly*) const \
192  { \
193  return ::operator op (*this, 0); \
194  }
195 private:
196  class NullPointerOnly {};
197 public:
198  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(==)
199  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(!=)
200  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(<)
201  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(<=)
202  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(>)
203  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(>=)
204 # undef VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND
205 #endif
206 protected:
208 private:
209  // These are purposely not implemented to prevent callers from
210  // trying to take references from other smart pointers.
211  void TakeReference(const vtkSmartPointerBase&) = delete;
212  static void Take(const vtkSmartPointerBase&) = delete;
213 };
214 
215 #define VTK_SMART_POINTER_DEFINE_OPERATOR(op) \
216  template <class T> \
217  inline bool \
218  operator op (const vtkSmartPointer<T>& l, const vtkSmartPointer<T>& r) \
219  { \
220  return (l.GetPointer() op r.GetPointer()); \
221  } \
222  template <class T> \
223  inline bool operator op (T* l, const vtkSmartPointer<T>& r) \
224  { \
225  return (l op r.GetPointer()); \
226  } \
227  template <class T> \
228  inline bool operator op (const vtkSmartPointer<T>& l, T* r) \
229  { \
230  return (l.GetPointer() op r); \
231  }
232 
241 
242 #undef VTK_SMART_POINTER_DEFINE_OPERATOR
243 
247 template <class T>
248 inline ostream& operator << (ostream& os, const vtkSmartPointer<T>& p)
249 {
250  return os << static_cast<const vtkSmartPointerBase&>(p);
251 }
252 
253 #endif
254 // 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)
T * Get() const
Get the contained pointer.
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.
ostream & operator<<(ostream &os, const vtkSmartPointer< T > &p)
Compare smart pointer values.
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.
T * operator->() const
Provides normal pointer target member access using operator ->.
vtkObjectBase * Object
void TakeReference(T *t)
Transfer ownership of one reference to the given VTK object to this smart pointer.
Allocate and hold a VTK object.
Definition: vtkNew.h:52
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 * GetPointer() const
Get the contained pointer.