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 template <class T>
30 {
31  static T* CheckType(T* t) { return t; }
32 public:
37 
42 
47  template <class U>
49  vtkSmartPointerBase(CheckType(r.GetPointer())) {}
50 
52 
57  {
59  return *this;
60  }
62 
64 
68  template <class U>
70  {
71  this->vtkSmartPointerBase::operator=(CheckType(r.GetPointer()));
72  return *this;
73  }
75 
77 
80  T* GetPointer() const
81  {
82  return static_cast<T*>(this->Object);
83  }
84  T* Get() const
85  {
86  return static_cast<T*>(this->Object);
87  }
89 
93  operator T* () const
94  {
95  return static_cast<T*>(this->Object);
96  }
97 
102  T& operator*() const
103  {
104  return *static_cast<T*>(this->Object);
105  }
106 
110  T* operator->() const
111  {
112  return static_cast<T*>(this->Object);
113  }
114 
127  void TakeReference(T* t)
128  {
129  *this = vtkSmartPointer<T>(t, NoReference());
130  }
131 
136  {
137  return vtkSmartPointer<T>(T::New(), NoReference());
138  }
139 
144  {
145  return vtkSmartPointer<T>(t->NewInstance(), NoReference());
146  }
147 
162  {
163  return vtkSmartPointer<T>(t, NoReference());
164  }
165 
166  // Work-around for HP and IBM overload resolution bug. Since
167  // NullPointerOnly is a private type the only pointer value that can
168  // be passed by user code is a null pointer. This operator will be
169  // chosen by the compiler when comparing against null explicitly and
170  // avoid the bogus ambiguous overload error.
171 #if defined(__HP_aCC) || defined(__IBMCPP__)
172 # define VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(op) \
173  bool operator op (NullPointerOnly*) const \
174  { \
175  return ::operator op (*this, 0); \
176  }
177 private:
178  class NullPointerOnly {};
179 public:
180  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(==)
181  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(!=)
182  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(<)
183  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(<=)
184  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(>)
185  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(>=)
186 # undef VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND
187 #endif
188 protected:
190 private:
191  // These are purposely not implemented to prevent callers from
192  // trying to take references from other smart pointers.
193  void TakeReference(const vtkSmartPointerBase&) VTK_DELETE_FUNCTION;
194  static void Take(const vtkSmartPointerBase&) VTK_DELETE_FUNCTION;
195 };
196 
197 #define VTK_SMART_POINTER_DEFINE_OPERATOR(op) \
198  template <class T> \
199  inline bool \
200  operator op (const vtkSmartPointer<T>& l, const vtkSmartPointer<T>& r) \
201  { \
202  return (l.GetPointer() op r.GetPointer()); \
203  } \
204  template <class T> \
205  inline bool operator op (T* l, const vtkSmartPointer<T>& r) \
206  { \
207  return (l op r.GetPointer()); \
208  } \
209  template <class T> \
210  inline bool operator op (const vtkSmartPointer<T>& l, T* r) \
211  { \
212  return (l.GetPointer() op r); \
213  }
214 
223 
224 #undef VTK_SMART_POINTER_DEFINE_OPERATOR
225 
229 template <class T>
230 inline ostream& operator << (ostream& os, const vtkSmartPointer<T>& p)
231 {
232  return os << static_cast<const vtkSmartPointerBase&>(p);
233 }
234 
235 #endif
236 // VTK-HeaderTest-Exclude: vtkSmartPointer.h
vtkSmartPointer()
Initialize smart pointer to NULL.
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 -&gt;.
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.