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 
34 template <class T>
36 {
37  static T* CheckType(T* t) { return t; }
38 public:
43 
48 
53  template <class U>
55  vtkSmartPointerBase(CheckType(r.GetPointer())) {}
56 
58 
63  {
65  return *this;
66  }
68 
70 
74  template <class U>
76  {
77  this->vtkSmartPointerBase::operator=(CheckType(r.GetPointer()));
78  return *this;
79  }
81 
83 
86  T* GetPointer() const
87  {
88  return static_cast<T*>(this->Object);
89  }
90  T* Get() const
91  {
92  return static_cast<T*>(this->Object);
93  }
95 
99  operator T* () const
100  {
101  return static_cast<T*>(this->Object);
102  }
103 
108  T& operator*() const
109  {
110  return *static_cast<T*>(this->Object);
111  }
112 
116  T* operator->() const
117  {
118  return static_cast<T*>(this->Object);
119  }
120 
133  void TakeReference(T* t)
134  {
135  *this = vtkSmartPointer<T>(t, NoReference());
136  }
137 
142  {
143  return vtkSmartPointer<T>(T::New(), NoReference());
144  }
145 
150  {
151  return vtkSmartPointer<T>(t->NewInstance(), NoReference());
152  }
153 
168  {
169  return vtkSmartPointer<T>(t, NoReference());
170  }
171 
172  // Work-around for HP and IBM overload resolution bug. Since
173  // NullPointerOnly is a private type the only pointer value that can
174  // be passed by user code is a null pointer. This operator will be
175  // chosen by the compiler when comparing against null explicitly and
176  // avoid the bogus ambiguous overload error.
177 #if defined(__HP_aCC) || defined(__IBMCPP__)
178 # define VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(op) \
179  bool operator op (NullPointerOnly*) const \
180  { \
181  return ::operator op (*this, 0); \
182  }
183 private:
184  class NullPointerOnly {};
185 public:
186  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(==)
187  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(!=)
188  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(<)
189  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(<=)
190  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(>)
191  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(>=)
192 # undef VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND
193 #endif
194 protected:
196 private:
197  // These are purposely not implemented to prevent callers from
198  // trying to take references from other smart pointers.
199  void TakeReference(const vtkSmartPointerBase&) VTK_DELETE_FUNCTION;
200  static void Take(const vtkSmartPointerBase&) VTK_DELETE_FUNCTION;
201 };
202 
203 #define VTK_SMART_POINTER_DEFINE_OPERATOR(op) \
204  template <class T> \
205  inline bool \
206  operator op (const vtkSmartPointer<T>& l, const vtkSmartPointer<T>& r) \
207  { \
208  return (l.GetPointer() op r.GetPointer()); \
209  } \
210  template <class T> \
211  inline bool operator op (T* l, const vtkSmartPointer<T>& r) \
212  { \
213  return (l op r.GetPointer()); \
214  } \
215  template <class T> \
216  inline bool operator op (const vtkSmartPointer<T>& l, T* r) \
217  { \
218  return (l.GetPointer() op r); \
219  }
220 
229 
230 #undef VTK_SMART_POINTER_DEFINE_OPERATOR
231 
235 template <class T>
236 inline ostream& operator << (ostream& os, const vtkSmartPointer<T>& p)
237 {
238  return os << static_cast<const vtkSmartPointerBase&>(p);
239 }
240 
241 #endif
242 // 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.
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 ->.
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.