00001 /*========================================================================= 00002 00003 Program: Visualization Toolkit 00004 Module: vtkWeakPointer.h 00005 00006 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen 00007 All rights reserved. 00008 See Copyright.txt or http://www.kitware.com/Copyright.htm for details. 00009 00010 This software is distributed WITHOUT ANY WARRANTY; without even 00011 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00012 PURPOSE. See the above copyright notice for more information. 00013 00014 =========================================================================*/ 00045 #ifndef __vtkWeakPointer_h 00046 #define __vtkWeakPointer_h 00047 00048 #include "vtkWeakPointerBase.h" 00049 00050 template <class T> 00051 class vtkWeakPointer: public vtkWeakPointerBase 00052 { 00053 public: 00055 vtkWeakPointer() {} 00056 00058 vtkWeakPointer(T* r): vtkWeakPointerBase(r) {} 00059 00061 vtkWeakPointer(const vtkWeakPointerBase& r): vtkWeakPointerBase(r) {} 00062 00064 00065 vtkWeakPointer& operator=(T* r) 00066 { 00067 this->vtkWeakPointerBase::operator=(r); 00068 return *this; 00069 } 00071 00073 00074 vtkWeakPointer& operator=(const vtkWeakPointerBase& r) 00075 { 00076 this->vtkWeakPointerBase::operator=(r); 00077 return *this; 00078 } 00080 00082 00083 T* GetPointer() const 00084 { 00085 return static_cast<T*>(this->Object); 00086 } 00088 00090 00091 operator T* () const 00092 { 00093 return static_cast<T*>(this->Object); 00094 } 00096 00098 00100 T& operator*() const 00101 { 00102 return *static_cast<T*>(this->Object); 00103 } 00105 00107 00108 T* operator->() const 00109 { 00110 return static_cast<T*>(this->Object); 00111 } 00113 00114 // Work-around for HP and IBM overload resolution bug. Since 00115 // NullPointerOnly is a private type the only pointer value that can 00116 // be passed by user code is a null pointer. This operator will be 00117 // chosen by the compiler when comparing against null explicitly and 00118 // avoid the bogus ambiguous overload error. 00119 #if defined(__HP_aCC) || defined(__IBMCPP__) 00120 # define VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(op) \ 00121 vtkstd_bool operator op (NullPointerOnly*) const \ 00122 { \ 00123 return ::operator op (*this, 0); \ 00124 } 00125 private: 00126 class NullPointerOnly {}; 00127 public: 00128 VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(==) 00129 VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(!=) 00130 VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(<) 00131 VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(<=) 00132 VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(>) 00133 VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(>=) 00134 # undef VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND 00135 #endif 00136 protected: 00137 vtkWeakPointer(T* r, const NoReference& n): vtkWeakPointerBase(r, n) {} 00138 private: 00139 // These are purposely not implemented to prevent callers from 00140 // trying to take references from other smart pointers. 00141 void TakeReference(const vtkWeakPointerBase&); // Not implemented. 00142 static void Take(const vtkWeakPointerBase&); // Not implemented. 00143 }; 00144 00145 #define VTK_WEAK_POINTER_DEFINE_OPERATOR(op) \ 00146 template <class T> \ 00147 inline vtkstd_bool \ 00148 operator op (const vtkWeakPointer<T>& l, const vtkWeakPointer<T>& r) \ 00149 { \ 00150 return (l.GetPointer() op r.GetPointer()); \ 00151 } \ 00152 template <class T> \ 00153 inline vtkstd_bool operator op (T* l, const vtkWeakPointer<T>& r) \ 00154 { \ 00155 return (l op r.GetPointer()); \ 00156 } \ 00157 template <class T> \ 00158 inline vtkstd_bool operator op (const vtkWeakPointer<T>& l, T* r) \ 00159 { \ 00160 return (l.GetPointer() op r); \ 00161 } 00162 00163 00164 VTK_WEAK_POINTER_DEFINE_OPERATOR(==) 00165 VTK_WEAK_POINTER_DEFINE_OPERATOR(!=) 00166 VTK_WEAK_POINTER_DEFINE_OPERATOR(<) 00167 VTK_WEAK_POINTER_DEFINE_OPERATOR(<=) 00168 VTK_WEAK_POINTER_DEFINE_OPERATOR(>) 00169 VTK_WEAK_POINTER_DEFINE_OPERATOR(>=) 00171 00172 #undef VTK_WEAK_POINTER_DEFINE_OPERATOR 00173 00175 00176 template <class T> 00177 inline ostream& operator << (ostream& os, const vtkWeakPointer<T>& p) 00178 { 00179 return os << static_cast<const vtkWeakPointerBase&>(p); 00180 } 00182 00183 00184 #endif 00185 00186