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 =========================================================================*/
28 #ifndef vtkSmartPointer_h
29 #define vtkSmartPointer_h
30 
31 #include "vtkSmartPointerBase.h"
32 
33 template <class T>
35 {
36  static T* CheckType(T* t) { return t; }
37 public:
40 
43 
45 
47  template <class U>
49  vtkSmartPointerBase(CheckType(r.GetPointer())) {}
51 
53 
56  {
58  return *this;
59  }
61 
63 
65  template <class U>
67  {
68  this->vtkSmartPointerBase::operator=(CheckType(r.GetPointer()));
69  return *this;
70  }
72 
74 
75  T* GetPointer() const
76  {
77  return static_cast<T*>(this->Object);
78  }
79  T* Get() const
80  {
81  return static_cast<T*>(this->Object);
82  }
84 
86 
87  operator T* () const
88  {
89  return static_cast<T*>(this->Object);
90  }
92 
94 
96  T& operator*() const
97  {
98  return *static_cast<T*>(this->Object);
99  }
101 
103 
104  T* operator->() const
105  {
106  return static_cast<T*>(this->Object);
107  }
109 
111 
118  void TakeReference(T* t)
119  {
120  *this = vtkSmartPointer<T>(t, NoReference());
121  }
123 
125 
127  {
128  return vtkSmartPointer<T>(T::New(), NoReference());
129  }
131 
133 
135  {
136  return vtkSmartPointer<T>(t->NewInstance(), NoReference());
137  }
139 
141 
150  {
151  return vtkSmartPointer<T>(t, NoReference());
152  }
154 
155  // Work-around for HP and IBM overload resolution bug. Since
156  // NullPointerOnly is a private type the only pointer value that can
157  // be passed by user code is a null pointer. This operator will be
158  // chosen by the compiler when comparing against null explicitly and
159  // avoid the bogus ambiguous overload error.
160 #if defined(__HP_aCC) || defined(__IBMCPP__)
161 # define VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(op) \
162  bool operator op (NullPointerOnly*) const \
163  { \
164  return ::operator op (*this, 0); \
165  }
166 private:
167  class NullPointerOnly {};
168 public:
169  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(==)
170  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(!=)
171  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(<)
172  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(<=)
173  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(>)
174  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(>=)
175 # undef VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND
176 #endif
177 protected:
179 private:
180  // These are purposely not implemented to prevent callers from
181  // trying to take references from other smart pointers.
182  void TakeReference(const vtkSmartPointerBase&); // Not implemented.
183  static void Take(const vtkSmartPointerBase&); // Not implemented.
184 };
185 
186 #define VTK_SMART_POINTER_DEFINE_OPERATOR(op) \
187  template <class T> \
188  inline bool \
189  operator op (const vtkSmartPointer<T>& l, const vtkSmartPointer<T>& r) \
190  { \
191  return (l.GetPointer() op r.GetPointer()); \
192  } \
193  template <class T> \
194  inline bool operator op (T* l, const vtkSmartPointer<T>& r) \
195  { \
196  return (l op r.GetPointer()); \
197  } \
198  template <class T> \
199  inline bool operator op (const vtkSmartPointer<T>& l, T* r) \
200  { \
201  return (l.GetPointer() op r); \
202  }
203 
204 
212 
213 #undef VTK_SMART_POINTER_DEFINE_OPERATOR
214 
216 
217 template <class T>
218 inline ostream& operator << (ostream& os, const vtkSmartPointer<T>& p)
219 {
220  return os << static_cast<const vtkSmartPointerBase&>(p);
221 }
223 
224 #endif
225 // VTK-HeaderTest-Exclude: vtkSmartPointer.h
static vtkSmartPointer< T > Take(T *t)
static vtkSmartPointer< T > NewInstance(T *t)
vtkSmartPointerBase & operator=(vtkObjectBase *r)
vtkSmartPointer(T *r, const NoReference &n)
vtkSmartPointer & operator=(T *r)
Hold a reference to a vtkObjectBase instance.
static vtkSmartPointer< T > New()
vtkSmartPointer & operator=(const vtkSmartPointer< U > &r)
Non-templated superclass for vtkSmartPointer.
vtkObjectBase * Object
void TakeReference(T *t)
T * operator->() const
T * GetPointer() const
vtkSmartPointer(const vtkSmartPointer< U > &r)
T & operator*() const
#define VTK_SMART_POINTER_DEFINE_OPERATOR(op)
T * Get() const