00001 /*========================================================================= 00002 00003 Program: Visualization Toolkit 00004 Module: $RCSfile: vtkWeakPointer.h,v $ 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 =========================================================================*/ 00024 #ifndef __vtkWeakPointer_h 00025 #define __vtkWeakPointer_h 00026 00027 #include "vtkWeakPointerBase.h" 00028 00029 template <class T> 00030 class vtkWeakPointer: public vtkWeakPointerBase 00031 { 00032 public: 00034 vtkWeakPointer() {} 00035 00037 vtkWeakPointer(T* r): vtkWeakPointerBase(r) {} 00038 00040 vtkWeakPointer(const vtkWeakPointerBase& r): vtkWeakPointerBase(r) {} 00041 00043 00044 vtkWeakPointer& operator=(T* r) 00045 { 00046 this->vtkWeakPointerBase::operator=(r); 00047 return *this; 00048 } 00050 00052 00053 vtkWeakPointer& operator=(const vtkWeakPointerBase& r) 00054 { 00055 this->vtkWeakPointerBase::operator=(r); 00056 return *this; 00057 } 00059 00061 00062 T* GetPointer() const 00063 { 00064 return static_cast<T*>(this->Object); 00065 } 00067 00069 00070 operator T* () const 00071 { 00072 return static_cast<T*>(this->Object); 00073 } 00075 00077 00079 T& operator*() const 00080 { 00081 return *static_cast<T*>(this->Object); 00082 } 00084 00086 00087 T* operator->() const 00088 { 00089 return static_cast<T*>(this->Object); 00090 } 00092 00093 // Work-around for HP and IBM overload resolution bug. Since 00094 // NullPointerOnly is a private type the only pointer value that can 00095 // be passed by user code is a null pointer. This operator will be 00096 // chosen by the compiler when comparing against null explicitly and 00097 // avoid the bogus ambiguous overload error. 00098 #if defined(__HP_aCC) || defined(__IBMCPP__) 00099 # define VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(op) \ 00100 vtkstd_bool operator op (NullPointerOnly*) const \ 00101 { \ 00102 return ::operator op (*this, 0); \ 00103 } 00104 private: 00105 class NullPointerOnly {}; 00106 public: 00107 VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(==) 00108 VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(!=) 00109 VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(<) 00110 VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(<=) 00111 VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(>) 00112 VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(>=) 00113 # undef VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND 00114 #endif 00115 protected: 00116 vtkWeakPointer(T* r, const NoReference& n): vtkWeakPointerBase(r, n) {} 00117 private: 00118 // These are purposely not implemented to prevent callers from 00119 // trying to take references from other smart pointers. 00120 void TakeReference(const vtkWeakPointerBase&); // Not implemented. 00121 static void Take(const vtkWeakPointerBase&); // Not implemented. 00122 }; 00123 00124 #define VTK_WEAK_POINTER_DEFINE_OPERATOR(op) \ 00125 template <class T> \ 00126 inline vtkstd_bool \ 00127 operator op (const vtkWeakPointer<T>& l, const vtkWeakPointer<T>& r) \ 00128 { \ 00129 return (l.GetPointer() op r.GetPointer()); \ 00130 } \ 00131 template <class T> \ 00132 inline vtkstd_bool operator op (T* l, const vtkWeakPointer<T>& r) \ 00133 { \ 00134 return (l op r.GetPointer()); \ 00135 } \ 00136 template <class T> \ 00137 inline vtkstd_bool operator op (const vtkWeakPointer<T>& l, T* r) \ 00138 { \ 00139 return (l.GetPointer() op r); \ 00140 } 00141 00142 VTK_WEAK_POINTER_DEFINE_OPERATOR(==) 00143 VTK_WEAK_POINTER_DEFINE_OPERATOR(!=) 00144 VTK_WEAK_POINTER_DEFINE_OPERATOR(<) 00145 VTK_WEAK_POINTER_DEFINE_OPERATOR(<=) 00146 VTK_WEAK_POINTER_DEFINE_OPERATOR(>) 00147 VTK_WEAK_POINTER_DEFINE_OPERATOR(>=) 00148 00149 #undef VTK_WEAK_POINTER_DEFINE_OPERATOR 00150 00152 00153 template <class T> 00154 inline ostream& operator << (ostream& os, const vtkWeakPointer<T>& p) 00156 { 00157 return os << static_cast<const vtkWeakPointerBase&>(p); 00158 } 00159 00160 00161 #endif 00162 00163