VTK
dox/Common/vtkWeakPointer.h
Go to the documentation of this file.
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 =========================================================================*/
00042 #ifndef __vtkWeakPointer_h
00043 #define __vtkWeakPointer_h
00044 
00045 #include "vtkWeakPointerBase.h"
00046 
00047 template <class T>
00048 class vtkWeakPointer: public vtkWeakPointerBase
00049 {
00050 public:
00052   vtkWeakPointer() {}
00053 
00055   vtkWeakPointer(T* r): vtkWeakPointerBase(r) {}
00056 
00058   vtkWeakPointer(const vtkWeakPointerBase& r): vtkWeakPointerBase(r) {}
00059 
00061 
00062   vtkWeakPointer& operator=(T* r)
00063     {
00064     this->vtkWeakPointerBase::operator=(r);
00065     return *this;
00066     }
00068 
00070 
00071   vtkWeakPointer& operator=(const vtkWeakPointerBase& r)
00072     {
00073     this->vtkWeakPointerBase::operator=(r);
00074     return *this;
00075     }
00077 
00079 
00080   T* GetPointer() const
00081     {
00082     return static_cast<T*>(this->Object);
00083     }
00085 
00087 
00088   operator T* () const
00089     {
00090     return static_cast<T*>(this->Object);
00091     }
00093 
00095 
00097   T& operator*() const
00098     {
00099     return *static_cast<T*>(this->Object);
00100     }
00102 
00104 
00105   T* operator->() const
00106     {
00107     return static_cast<T*>(this->Object);
00108     }
00110 
00111   // Work-around for HP and IBM overload resolution bug.  Since
00112   // NullPointerOnly is a private type the only pointer value that can
00113   // be passed by user code is a null pointer.  This operator will be
00114   // chosen by the compiler when comparing against null explicitly and
00115   // avoid the bogus ambiguous overload error.
00116 #if defined(__HP_aCC) || defined(__IBMCPP__)
00117 # define VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(op) \
00118   vtkstd_bool operator op (NullPointerOnly*) const        \
00119     {                                                     \
00120     return ::operator op (*this, 0);                      \
00121     }
00122 private:
00123   class NullPointerOnly {};
00124 public:
00125   VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(==)
00126   VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(!=)
00127   VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(<)
00128   VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(<=)
00129   VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(>)
00130   VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(>=)
00131 # undef VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND
00132 #endif
00133 protected:
00134   vtkWeakPointer(T* r, const NoReference& n): vtkWeakPointerBase(r, n) {}
00135 private:
00136   // These are purposely not implemented to prevent callers from
00137   // trying to take references from other smart pointers.
00138   void TakeReference(const vtkWeakPointerBase&);  // Not implemented.
00139   static void Take(const vtkWeakPointerBase&);  // Not implemented.
00140 };
00141 
00142 #define VTK_WEAK_POINTER_DEFINE_OPERATOR(op) \
00143   template <class T> \
00144   inline vtkstd_bool \
00145   operator op (const vtkWeakPointer<T>& l, const vtkWeakPointer<T>& r) \
00146     { \
00147     return (l.GetPointer() op r.GetPointer()); \
00148     } \
00149   template <class T> \
00150   inline vtkstd_bool operator op (T* l, const vtkWeakPointer<T>& r) \
00151     { \
00152     return (l op r.GetPointer()); \
00153     } \
00154   template <class T> \
00155   inline vtkstd_bool operator op (const vtkWeakPointer<T>& l, T* r) \
00156     { \
00157     return (l.GetPointer() op r); \
00158     }
00159 
00160 VTK_WEAK_POINTER_DEFINE_OPERATOR(==)
00161 VTK_WEAK_POINTER_DEFINE_OPERATOR(!=)
00162 VTK_WEAK_POINTER_DEFINE_OPERATOR(<)
00163 VTK_WEAK_POINTER_DEFINE_OPERATOR(<=)
00164 VTK_WEAK_POINTER_DEFINE_OPERATOR(>)
00165 VTK_WEAK_POINTER_DEFINE_OPERATOR(>=)
00166 
00167 #undef VTK_WEAK_POINTER_DEFINE_OPERATOR
00168 
00170 
00171 template <class T>
00172 inline ostream& operator << (ostream& os, const vtkWeakPointer<T>& p)
00174 {
00175   return os << static_cast<const vtkWeakPointerBase&>(p);
00176 }
00177 
00178 
00179 #endif
00180 
00181