VTK
vtkWeakPointer.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkWeakPointer.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 =========================================================================*/
45 #ifndef vtkWeakPointer_h
46 #define vtkWeakPointer_h
47 
48 #include "vtkWeakPointerBase.h"
49 
50 template <class T>
52 {
53 public:
56 
59 
62 
64 
66  {
68  return *this;
69  }
71 
73 
75  {
77  return *this;
78  }
80 
82 
83  T* GetPointer() const
84  {
85  return static_cast<T*>(this->Object);
86  }
87  T* Get() const
88  {
89  return static_cast<T*>(this->Object);
90  }
92 
94 
95  operator T* () const
96  {
97  return static_cast<T*>(this->Object);
98  }
100 
102 
104  T& operator*() const
105  {
106  return *static_cast<T*>(this->Object);
107  }
109 
111 
112  T* operator->() const
113  {
114  return static_cast<T*>(this->Object);
115  }
117 
118  // Work-around for HP and IBM overload resolution bug. Since
119  // NullPointerOnly is a private type the only pointer value that can
120  // be passed by user code is a null pointer. This operator will be
121  // chosen by the compiler when comparing against null explicitly and
122  // avoid the bogus ambiguous overload error.
123 #if defined(__HP_aCC) || defined(__IBMCPP__)
124 # define VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(op) \
125  bool operator op (NullPointerOnly*) const \
126  { \
127  return ::operator op (*this, 0); \
128  }
129 private:
130  class NullPointerOnly {};
131 public:
132  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(==)
133  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(!=)
134  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(<)
135  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(<=)
136  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(>)
137  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(>=)
138 # undef VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND
139 #endif
140 protected:
142 private:
143  // These are purposely not implemented to prevent callers from
144  // trying to take references from other smart pointers.
145  void TakeReference(const vtkWeakPointerBase&); // Not implemented.
146  static void Take(const vtkWeakPointerBase&); // Not implemented.
147 };
148 
149 #define VTK_WEAK_POINTER_DEFINE_OPERATOR(op) \
150  template <class T> \
151  inline bool \
152  operator op (const vtkWeakPointer<T>& l, const vtkWeakPointer<T>& r) \
153  { \
154  return (l.GetPointer() op r.GetPointer()); \
155  } \
156  template <class T> \
157  inline bool operator op (T* l, const vtkWeakPointer<T>& r) \
158  { \
159  return (l op r.GetPointer()); \
160  } \
161  template <class T> \
162  inline bool operator op (const vtkWeakPointer<T>& l, T* r) \
163  { \
164  return (l.GetPointer() op r); \
165  }
166 
167 
175 
176 #undef VTK_WEAK_POINTER_DEFINE_OPERATOR
177 
179 
180 template <class T>
181 inline ostream& operator << (ostream& os, const vtkWeakPointer<T>& p)
182 {
183  return os << static_cast<const vtkWeakPointerBase&>(p);
184 }
186 
187 
188 #endif
189 
190 
191 // VTK-HeaderTest-Exclude: vtkWeakPointer.h
vtkObjectBase * Object
vtkWeakPointerBase & operator=(vtkObjectBase *r)
T & operator*() const
vtkWeakPointer(const vtkWeakPointerBase &r)
T * operator->() const
vtkWeakPointer & operator=(const vtkWeakPointerBase &r)
vtkWeakPointer(T *r, const NoReference &n)
a weak reference to a vtkObject.
Non-templated superclass for vtkWeakPointer.
vtkWeakPointer & operator=(T *r)
T * GetPointer() const
T * Get() const
#define VTK_WEAK_POINTER_DEFINE_OPERATOR(op)