VTK
dox/Common/Core/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 =========================================================================*/
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     }
00087   T* Get() const
00088     {
00089     return static_cast<T*>(this->Object);
00090     }
00092 
00094 
00095   operator T* () const
00096     {
00097     return static_cast<T*>(this->Object);
00098     }
00100 
00102 
00104   T& operator*() const
00105     {
00106     return *static_cast<T*>(this->Object);
00107     }
00109 
00111 
00112   T* operator->() const
00113     {
00114     return static_cast<T*>(this->Object);
00115     }
00117 
00118   // Work-around for HP and IBM overload resolution bug.  Since
00119   // NullPointerOnly is a private type the only pointer value that can
00120   // be passed by user code is a null pointer.  This operator will be
00121   // chosen by the compiler when comparing against null explicitly and
00122   // avoid the bogus ambiguous overload error.
00123 #if defined(__HP_aCC) || defined(__IBMCPP__)
00124 # define VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(op) \
00125   bool operator op (NullPointerOnly*) const              \
00126     {                                                     \
00127     return ::operator op (*this, 0);                      \
00128     }
00129 private:
00130   class NullPointerOnly {};
00131 public:
00132   VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(==)
00133   VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(!=)
00134   VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(<)
00135   VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(<=)
00136   VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(>)
00137   VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(>=)
00138 # undef VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND
00139 #endif
00140 protected:
00141   vtkWeakPointer(T* r, const NoReference& n): vtkWeakPointerBase(r, n) {}
00142 private:
00143   // These are purposely not implemented to prevent callers from
00144   // trying to take references from other smart pointers.
00145   void TakeReference(const vtkWeakPointerBase&);  // Not implemented.
00146   static void Take(const vtkWeakPointerBase&);  // Not implemented.
00147 };
00148 
00149 #define VTK_WEAK_POINTER_DEFINE_OPERATOR(op) \
00150   template <class T> \
00151   inline bool \
00152   operator op (const vtkWeakPointer<T>& l, const vtkWeakPointer<T>& r) \
00153     { \
00154     return (l.GetPointer() op r.GetPointer()); \
00155     } \
00156   template <class T> \
00157   inline bool operator op (T* l, const vtkWeakPointer<T>& r) \
00158     { \
00159     return (l op r.GetPointer()); \
00160     } \
00161   template <class T> \
00162   inline bool operator op (const vtkWeakPointer<T>& l, T* r) \
00163     { \
00164     return (l.GetPointer() op r); \
00165     }
00166 
00167 
00168 VTK_WEAK_POINTER_DEFINE_OPERATOR(==)
00169 VTK_WEAK_POINTER_DEFINE_OPERATOR(!=)
00170 VTK_WEAK_POINTER_DEFINE_OPERATOR(<)
00171 VTK_WEAK_POINTER_DEFINE_OPERATOR(<=)
00172 VTK_WEAK_POINTER_DEFINE_OPERATOR(>)
00173 VTK_WEAK_POINTER_DEFINE_OPERATOR(>=)
00175 
00176 #undef VTK_WEAK_POINTER_DEFINE_OPERATOR
00177 
00179 
00180 template <class T>
00181 inline ostream& operator << (ostream& os, const vtkWeakPointer<T>& p)
00182 {
00183   return os << static_cast<const vtkWeakPointerBase&>(p);
00184 }
00186 
00187 
00188 #endif
00189 
00190 
00191 // VTK-HeaderTest-Exclude: vtkWeakPointer.h