VTK
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     }
00080 
00082 
00083   operator T* () const
00084     {
00085     return static_cast<T*>(this->Object);
00086     }
00088 
00090 
00092   T& operator*() const
00093     {
00094     return *static_cast<T*>(this->Object);
00095     }
00097 
00099 
00100   T* operator->() const
00101     {
00102     return static_cast<T*>(this->Object);
00103     }
00105 
00107 
00114   void TakeReference(T* t)
00115     {
00116     *this = vtkSmartPointer<T>(t, NoReference());
00117     }
00119 
00121 
00122   static vtkSmartPointer<T> New()
00123     {
00124     return vtkSmartPointer<T>(T::New(), NoReference());
00125     }
00127 
00129 
00130   static vtkSmartPointer<T> NewInstance(T* t)
00131     {
00132     return vtkSmartPointer<T>(t->NewInstance(), NoReference());
00133     }
00135 
00137 
00145   static vtkSmartPointer<T> Take(T* t)
00146     {
00147     return vtkSmartPointer<T>(t, NoReference());
00148     }
00150 
00151   // Work-around for HP and IBM overload resolution bug.  Since
00152   // NullPointerOnly is a private type the only pointer value that can
00153   // be passed by user code is a null pointer.  This operator will be
00154   // chosen by the compiler when comparing against null explicitly and
00155   // avoid the bogus ambiguous overload error.
00156 #if defined(__HP_aCC) || defined(__IBMCPP__)
00157 # define VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(op) \
00158   bool operator op (NullPointerOnly*) const        \
00159     {                                                     \
00160     return ::operator op (*this, 0);                      \
00161     }
00162 private:
00163   class NullPointerOnly {};
00164 public:
00165   VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(==)
00166   VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(!=)
00167   VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(<)
00168   VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(<=)
00169   VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(>)
00170   VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(>=)
00171 # undef VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND
00172 #endif
00173 protected:
00174   vtkSmartPointer(T* r, const NoReference& n): vtkSmartPointerBase(r, n) {}
00175 private:
00176   // These are purposely not implemented to prevent callers from
00177   // trying to take references from other smart pointers.
00178   void TakeReference(const vtkSmartPointerBase&);  // Not implemented.
00179   static void Take(const vtkSmartPointerBase&);  // Not implemented.
00180 };
00181 
00182 #define VTK_SMART_POINTER_DEFINE_OPERATOR(op) \
00183   template <class T> \
00184   inline bool \
00185   operator op (const vtkSmartPointer<T>& l, const vtkSmartPointer<T>& r) \
00186     { \
00187     return (l.GetPointer() op r.GetPointer()); \
00188     } \
00189   template <class T> \
00190   inline bool operator op (T* l, const vtkSmartPointer<T>& r) \
00191     { \
00192     return (l op r.GetPointer()); \
00193     } \
00194   template <class T> \
00195   inline bool operator op (const vtkSmartPointer<T>& l, T* r) \
00196     { \
00197     return (l.GetPointer() op r); \
00198     }
00199 
00200 
00201 VTK_SMART_POINTER_DEFINE_OPERATOR(==)
00202 VTK_SMART_POINTER_DEFINE_OPERATOR(!=)
00203 VTK_SMART_POINTER_DEFINE_OPERATOR(<)
00204 VTK_SMART_POINTER_DEFINE_OPERATOR(<=)
00205 VTK_SMART_POINTER_DEFINE_OPERATOR(>)
00206 VTK_SMART_POINTER_DEFINE_OPERATOR(>=)
00208 
00209 #undef VTK_SMART_POINTER_DEFINE_OPERATOR
00210 
00212 
00213 template <class T>
00214 inline ostream& operator << (ostream& os, const vtkSmartPointer<T>& p)
00215 {
00216   return os << static_cast<const vtkSmartPointerBase&>(p);
00217 }
00219 
00220 #endif
00221 // VTK-HeaderTest-Exclude: vtkSmartPointer.h