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  }
80 
82 
83  operator T* () const
84  {
85  return static_cast<T*>(this->Object);
86  }
88 
90 
92  T& operator*() const
93  {
94  return *static_cast<T*>(this->Object);
95  }
97 
99 
100  T* operator->() const
101  {
102  return static_cast<T*>(this->Object);
103  }
105 
107 
114  void TakeReference(T* t)
115  {
116  *this = vtkSmartPointer<T>(t, NoReference());
117  }
119 
121 
123  {
124  return vtkSmartPointer<T>(T::New(), NoReference());
125  }
127 
129 
131  {
132  return vtkSmartPointer<T>(t->NewInstance(), NoReference());
133  }
135 
137 
146  {
147  return vtkSmartPointer<T>(t, NoReference());
148  }
150 
151  // Work-around for HP and IBM overload resolution bug. Since
152  // NullPointerOnly is a private type the only pointer value that can
153  // be passed by user code is a null pointer. This operator will be
154  // chosen by the compiler when comparing against null explicitly and
155  // avoid the bogus ambiguous overload error.
156 #if defined(__HP_aCC) || defined(__IBMCPP__)
157 # define VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(op) \
158  vtkstd_bool operator op (NullPointerOnly*) const \
159  { \
160  return ::operator op (*this, 0); \
161  }
162 private:
163  class NullPointerOnly {};
164 public:
165  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(==)
166  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(!=)
167  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(<)
168  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(<=)
169  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(>)
170  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(>=)
171 # undef VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND
172 #endif
173 protected:
175 private:
176  // These are purposely not implemented to prevent callers from
177  // trying to take references from other smart pointers.
178  void TakeReference(const vtkSmartPointerBase&); // Not implemented.
179  static void Take(const vtkSmartPointerBase&); // Not implemented.
180 };
181 
182 #define VTK_SMART_POINTER_DEFINE_OPERATOR(op) \
183  template <class T> \
184  inline vtkstd_bool \
185  operator op (const vtkSmartPointer<T>& l, const vtkSmartPointer<T>& r) \
186  { \
187  return (l.GetPointer() op r.GetPointer()); \
188  } \
189  template <class T> \
190  inline vtkstd_bool operator op (T* l, const vtkSmartPointer<T>& r) \
191  { \
192  return (l op r.GetPointer()); \
193  } \
194  template <class T> \
195  inline vtkstd_bool operator op (const vtkSmartPointer<T>& l, T* r) \
196  { \
197  return (l.GetPointer() op r); \
198  }
199 
200 
208 
209 #undef VTK_SMART_POINTER_DEFINE_OPERATOR
210 
212 
213 template <class T>
214 inline ostream& operator << (ostream& os, const vtkSmartPointer<T>& p)
215 {
216  return os << static_cast<const vtkSmartPointerBase&>(p);
217 }
219 
220 #endif
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)