VTK
/Users/kitware/Dashboards/MyTests/VTK_BLD_Release_docs/Utilities/Doxygen/dox/Common/Core/vtkSmartPointer.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkSmartPointer.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 =========================================================================*/
00028 #ifndef vtkSmartPointer_h
00029 #define vtkSmartPointer_h
00030 
00031 #include "vtkSmartPointerBase.h"
00032 
00033 template <class T>
00034 class vtkSmartPointer: public vtkSmartPointerBase
00035 {
00036   static T* CheckType(T* t) { return t; }
00037 public:
00039   vtkSmartPointer() {}
00040 
00042   vtkSmartPointer(T* r): vtkSmartPointerBase(r) {}
00043 
00045 
00047   template <class U>
00048   vtkSmartPointer(const vtkSmartPointer<U>& r):
00049     vtkSmartPointerBase(CheckType(r.GetPointer())) {}
00051 
00053 
00055   vtkSmartPointer& operator=(T* r)
00056     {
00057     this->vtkSmartPointerBase::operator=(r);
00058     return *this;
00059     }
00061 
00063 
00065   template <class U>
00066   vtkSmartPointer& operator=(const vtkSmartPointer<U>& r)
00067     {
00068     this->vtkSmartPointerBase::operator=(CheckType(r.GetPointer()));
00069     return *this;
00070     }
00072 
00074 
00075   T* GetPointer() const
00076     {
00077     return static_cast<T*>(this->Object);
00078     }
00079   T* Get() const
00080     {
00081     return static_cast<T*>(this->Object);
00082     }
00084 
00086 
00087   operator T* () const
00088     {
00089     return static_cast<T*>(this->Object);
00090     }
00092 
00094 
00096   T& operator*() const
00097     {
00098     return *static_cast<T*>(this->Object);
00099     }
00101 
00103 
00104   T* operator->() const
00105     {
00106     return static_cast<T*>(this->Object);
00107     }
00109 
00111 
00118   void TakeReference(T* t)
00119     {
00120     *this = vtkSmartPointer<T>(t, NoReference());
00121     }
00123 
00125 
00126   static vtkSmartPointer<T> New()
00127     {
00128     return vtkSmartPointer<T>(T::New(), NoReference());
00129     }
00131 
00133 
00134   static vtkSmartPointer<T> NewInstance(T* t)
00135     {
00136     return vtkSmartPointer<T>(t->NewInstance(), NoReference());
00137     }
00139 
00141 
00149   static vtkSmartPointer<T> Take(T* t)
00150     {
00151     return vtkSmartPointer<T>(t, NoReference());
00152     }
00154 
00155   // Work-around for HP and IBM overload resolution bug.  Since
00156   // NullPointerOnly is a private type the only pointer value that can
00157   // be passed by user code is a null pointer.  This operator will be
00158   // chosen by the compiler when comparing against null explicitly and
00159   // avoid the bogus ambiguous overload error.
00160 #if defined(__HP_aCC) || defined(__IBMCPP__)
00161 # define VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(op) \
00162   bool operator op (NullPointerOnly*) const        \
00163     {                                                     \
00164     return ::operator op (*this, 0);                      \
00165     }
00166 private:
00167   class NullPointerOnly {};
00168 public:
00169   VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(==)
00170   VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(!=)
00171   VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(<)
00172   VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(<=)
00173   VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(>)
00174   VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(>=)
00175 # undef VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND
00176 #endif
00177 protected:
00178   vtkSmartPointer(T* r, const NoReference& n): vtkSmartPointerBase(r, n) {}
00179 private:
00180   // These are purposely not implemented to prevent callers from
00181   // trying to take references from other smart pointers.
00182   void TakeReference(const vtkSmartPointerBase&);  // Not implemented.
00183   static void Take(const vtkSmartPointerBase&);  // Not implemented.
00184 };
00185 
00186 #define VTK_SMART_POINTER_DEFINE_OPERATOR(op) \
00187   template <class T> \
00188   inline bool \
00189   operator op (const vtkSmartPointer<T>& l, const vtkSmartPointer<T>& r) \
00190     { \
00191     return (l.GetPointer() op r.GetPointer()); \
00192     } \
00193   template <class T> \
00194   inline bool operator op (T* l, const vtkSmartPointer<T>& r) \
00195     { \
00196     return (l op r.GetPointer()); \
00197     } \
00198   template <class T> \
00199   inline bool operator op (const vtkSmartPointer<T>& l, T* r) \
00200     { \
00201     return (l.GetPointer() op r); \
00202     }
00203 
00204 
00205 VTK_SMART_POINTER_DEFINE_OPERATOR(==)
00206 VTK_SMART_POINTER_DEFINE_OPERATOR(!=)
00207 VTK_SMART_POINTER_DEFINE_OPERATOR(<)
00208 VTK_SMART_POINTER_DEFINE_OPERATOR(<=)
00209 VTK_SMART_POINTER_DEFINE_OPERATOR(>)
00210 VTK_SMART_POINTER_DEFINE_OPERATOR(>=)
00212 
00213 #undef VTK_SMART_POINTER_DEFINE_OPERATOR
00214 
00216 
00217 template <class T>
00218 inline ostream& operator << (ostream& os, const vtkSmartPointer<T>& p)
00219 {
00220   return os << static_cast<const vtkSmartPointerBase&>(p);
00221 }
00223 
00224 #endif
00225 // VTK-HeaderTest-Exclude: vtkSmartPointer.h