Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

vtkSmartPointer.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    $RCSfile: vtkSmartPointer.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 =========================================================================*/
00025 #ifndef __vtkSmartPointer_h
00026 #define __vtkSmartPointer_h
00027 
00028 #include "vtkSmartPointerBase.h"
00029 
00030 template <class T>
00031 class vtkSmartPointer: public vtkSmartPointerBase
00032 {
00033 public:
00035   vtkSmartPointer() {}
00036 
00038   vtkSmartPointer(T* r): vtkSmartPointerBase(r) {}
00039 
00042   vtkSmartPointer(const vtkSmartPointerBase& r): vtkSmartPointerBase(r) {}
00043 
00045 
00047   vtkSmartPointer& operator=(T* r)
00048     {
00049     this->vtkSmartPointerBase::operator=(r);
00050     return *this;
00051     }
00053 
00055 
00057   vtkSmartPointer& operator=(const vtkSmartPointerBase& r)
00058     {
00059     this->vtkSmartPointerBase::operator=(r);
00060     return *this;
00061     }
00063 
00065 
00066   T* GetPointer() const
00067     {
00068     return static_cast<T*>(this->Object);
00069     }
00071 
00073 
00074   operator T* () const
00075     {
00076     return static_cast<T*>(this->Object);
00077     }
00079 
00081 
00083   T& operator*() const
00084     {
00085     return *static_cast<T*>(this->Object);
00086     }
00088 
00090 
00091   T* operator->() const
00092     {
00093     return static_cast<T*>(this->Object);
00094     }
00096 
00098 
00105   void TakeReference(T* t)
00106     {
00107     *this = vtkSmartPointer<T>(t, NoReference());
00108     }
00110 
00112 
00113   static vtkSmartPointer<T> New()
00114     {
00115     return vtkSmartPointer<T>(T::New(), NoReference());
00116     }
00118 
00120 
00121   static vtkSmartPointer<T> NewInstance(T* t)
00122     {
00123     return vtkSmartPointer<T>(t->NewInstance(), NoReference());
00124     }
00126 
00128 
00136   static vtkSmartPointer<T> Take(T* t)
00137     {
00138     return vtkSmartPointer<T>(t, NoReference());
00139     }
00141 
00142   // Work-around for HP overload resolution bug.  Since
00143   // NullPointerOnly is a private type the only pointer value that can
00144   // be passed by user code is a null pointer.  This operator will be
00145   // chosen by the compiler when comparing against null explicitly and
00146   // avoid the bogus ambiguous overload error.
00147 #if defined(__HP_aCC)
00148 # define VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(op) \
00149   vtkstd_bool operator op (NullPointerOnly*) const        \
00150     {                                                     \
00151     return ::operator op (*this, 0);                      \
00152     }
00153 private:
00154   class NullPointerOnly {};
00155 public:
00156   VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(==)
00157   VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(!=)
00158   VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(<)
00159   VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(<=)
00160   VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(>)
00161   VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(>=)
00162 # undef VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND
00163 #endif
00164 protected:
00165   vtkSmartPointer(T* r, const NoReference& n): vtkSmartPointerBase(r, n) {}
00166 private:
00167   // These are purposely not implemented to prevent callers from
00168   // trying to take references from other smart pointers.
00169   void TakeReference(const vtkSmartPointerBase&);  // Not implemented.
00170   static void Take(const vtkSmartPointerBase&);  // Not implemented.
00171 };
00172 
00173 #define VTK_SMART_POINTER_DEFINE_OPERATOR(op) \
00174   template <class T> \
00175   inline vtkstd_bool \
00176   operator op (const vtkSmartPointer<T>& l, const vtkSmartPointer<T>& r) \
00177     { \
00178     return (l.GetPointer() op r.GetPointer()); \
00179     } \
00180   template <class T> \
00181   inline vtkstd_bool operator op (T* l, const vtkSmartPointer<T>& r) \
00182     { \
00183     return (l op r.GetPointer()); \
00184     } \
00185   template <class T> \
00186   inline vtkstd_bool operator op (const vtkSmartPointer<T>& l, T* r) \
00187     { \
00188     return (l.GetPointer() op r); \
00189     }
00190 
00191 VTK_SMART_POINTER_DEFINE_OPERATOR(==)
00192 VTK_SMART_POINTER_DEFINE_OPERATOR(!=)
00193 VTK_SMART_POINTER_DEFINE_OPERATOR(<)
00194 VTK_SMART_POINTER_DEFINE_OPERATOR(<=)
00195 VTK_SMART_POINTER_DEFINE_OPERATOR(>)
00196 VTK_SMART_POINTER_DEFINE_OPERATOR(>=)
00197 
00198 #undef VTK_SMART_POINTER_DEFINE_OPERATOR
00199 
00201 
00202 template <class T>
00203 inline ostream& operator << (ostream& os, const vtkSmartPointer<T>& p)
00205 {
00206   return os << static_cast<const vtkSmartPointerBase&>(p);
00207 }
00208 
00209 #endif

Generated on Mon Jan 21 23:07:18 2008 for VTK by  doxygen 1.4.3-20050530