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 =========================================================================*/ 00034 #ifndef __vtkSmartPointer_h 00035 #define __vtkSmartPointer_h 00036 00037 #include "vtkSmartPointerBase.h" 00038 00039 template <class T> 00040 class vtkSmartPointer: public vtkSmartPointerBase 00041 { 00042 public: 00044 vtkSmartPointer() {} 00045 00047 vtkSmartPointer(T* r): vtkSmartPointerBase(r) {} 00048 00051 vtkSmartPointer(const vtkSmartPointerBase& r): vtkSmartPointerBase(r) {} 00052 00054 00056 vtkSmartPointer& operator=(T* r) 00057 { 00058 this->vtkSmartPointerBase::operator=(r); 00059 return *this; 00060 } 00062 00064 00066 vtkSmartPointer& operator=(const vtkSmartPointerBase& r) 00067 { 00068 this->vtkSmartPointerBase::operator=(r); 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 00108 static vtkSmartPointer<T> New() 00109 { 00110 return vtkSmartPointer<T>(T::New(), NoReference()); 00111 } 00113 protected: 00114 vtkSmartPointer(T* r, const NoReference& n): vtkSmartPointerBase(r, n) {} 00115 }; 00116 00117 #define VTK_SMART_POINTER_DEFINE_OPERATOR(op) \ 00118 template <class T> \ 00119 inline vtkstd_bool \ 00120 operator op (const vtkSmartPointer<T>& l, const vtkSmartPointer<T>& r) \ 00121 { \ 00122 return (l.GetPointer() op r.GetPointer()); \ 00123 } \ 00124 template <class T> \ 00125 inline vtkstd_bool operator op (T* l, const vtkSmartPointer<T>& r) \ 00126 { \ 00127 return (l op r.GetPointer()); \ 00128 } \ 00129 template <class T> \ 00130 inline vtkstd_bool operator op (const vtkSmartPointer<T>& l, T* r) \ 00131 { \ 00132 return (l.GetPointer() op r); \ 00133 } 00134 00135 VTK_SMART_POINTER_DEFINE_OPERATOR(==) 00136 VTK_SMART_POINTER_DEFINE_OPERATOR(!=) 00137 VTK_SMART_POINTER_DEFINE_OPERATOR(<) 00138 VTK_SMART_POINTER_DEFINE_OPERATOR(<=) 00139 VTK_SMART_POINTER_DEFINE_OPERATOR(>) 00140 VTK_SMART_POINTER_DEFINE_OPERATOR(>=) 00141 00142 #undef VTK_SMART_POINTER_DEFINE_OPERATOR 00143 00145 00146 template <class T> 00147 inline ostream& operator << (ostream& os, const vtkSmartPointer<T>& p) 00149 { 00150 return os << static_cast<const vtkSmartPointerBase&>(p); 00151 } 00152 00153 #endif