VTK
|
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 #include <vtksys/Configure.hxx> // need check for member template support 00030 00031 template <class T> 00032 class vtkSmartPointer: public vtkSmartPointerBase 00033 { 00034 static T* CheckType(T* t) { return t; } 00035 public: 00037 vtkSmartPointer() {} 00038 00040 vtkSmartPointer(T* r): vtkSmartPointerBase(r) {} 00041 00043 00045 #ifdef vtksys_CXX_HAS_MEMBER_TEMPLATES 00046 template <class U> 00047 vtkSmartPointer(const vtkSmartPointer<U>& r): 00048 vtkSmartPointerBase(CheckType(r.GetPointer())) {} 00050 #else 00051 vtkSmartPointer(const vtkSmartPointerBase& r): vtkSmartPointerBase(r) {} 00052 #endif 00053 00055 00057 vtkSmartPointer& operator=(T* r) 00058 { 00059 this->vtkSmartPointerBase::operator=(r); 00060 return *this; 00061 } 00063 00065 00067 #ifdef vtksys_CXX_HAS_MEMBER_TEMPLATES 00068 template <class U> 00069 vtkSmartPointer& operator=(const vtkSmartPointer<U>& r) 00070 { 00071 this->vtkSmartPointerBase::operator=(CheckType(r.GetPointer())); 00072 return *this; 00073 } 00075 #else 00076 vtkSmartPointer& operator=(const vtkSmartPointerBase& r) 00077 { 00078 this->vtkSmartPointerBase::operator=(r); 00079 return *this; 00080 } 00081 #endif 00082 00084 00085 T* GetPointer() const 00086 { 00087 return static_cast<T*>(this->Object); 00088 } 00090 00092 00093 operator T* () const 00094 { 00095 return static_cast<T*>(this->Object); 00096 } 00098 00100 00102 T& operator*() const 00103 { 00104 return *static_cast<T*>(this->Object); 00105 } 00107 00109 00110 T* operator->() const 00111 { 00112 return static_cast<T*>(this->Object); 00113 } 00115 00117 00124 void TakeReference(T* t) 00125 { 00126 *this = vtkSmartPointer<T>(t, NoReference()); 00127 } 00129 00131 00132 static vtkSmartPointer<T> New() 00133 { 00134 return vtkSmartPointer<T>(T::New(), NoReference()); 00135 } 00137 00139 00140 static vtkSmartPointer<T> NewInstance(T* t) 00141 { 00142 return vtkSmartPointer<T>(t->NewInstance(), NoReference()); 00143 } 00145 00147 00155 static vtkSmartPointer<T> Take(T* t) 00156 { 00157 return vtkSmartPointer<T>(t, NoReference()); 00158 } 00160 00161 // Work-around for HP and IBM overload resolution bug. Since 00162 // NullPointerOnly is a private type the only pointer value that can 00163 // be passed by user code is a null pointer. This operator will be 00164 // chosen by the compiler when comparing against null explicitly and 00165 // avoid the bogus ambiguous overload error. 00166 #if defined(__HP_aCC) || defined(__IBMCPP__) 00167 # define VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(op) \ 00168 vtkstd_bool operator op (NullPointerOnly*) const \ 00169 { \ 00170 return ::operator op (*this, 0); \ 00171 } 00172 private: 00173 class NullPointerOnly {}; 00174 public: 00175 VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(==) 00176 VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(!=) 00177 VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(<) 00178 VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(<=) 00179 VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(>) 00180 VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(>=) 00181 # undef VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND 00182 #endif 00183 protected: 00184 vtkSmartPointer(T* r, const NoReference& n): vtkSmartPointerBase(r, n) {} 00185 private: 00186 // These are purposely not implemented to prevent callers from 00187 // trying to take references from other smart pointers. 00188 void TakeReference(const vtkSmartPointerBase&); // Not implemented. 00189 static void Take(const vtkSmartPointerBase&); // Not implemented. 00190 }; 00191 00192 #define VTK_SMART_POINTER_DEFINE_OPERATOR(op) \ 00193 template <class T> \ 00194 inline vtkstd_bool \ 00195 operator op (const vtkSmartPointer<T>& l, const vtkSmartPointer<T>& r) \ 00196 { \ 00197 return (l.GetPointer() op r.GetPointer()); \ 00198 } \ 00199 template <class T> \ 00200 inline vtkstd_bool operator op (T* l, const vtkSmartPointer<T>& r) \ 00201 { \ 00202 return (l op r.GetPointer()); \ 00203 } \ 00204 template <class T> \ 00205 inline vtkstd_bool operator op (const vtkSmartPointer<T>& l, T* r) \ 00206 { \ 00207 return (l.GetPointer() op r); \ 00208 } 00209 00210 VTK_SMART_POINTER_DEFINE_OPERATOR(==) 00211 VTK_SMART_POINTER_DEFINE_OPERATOR(!=) 00212 VTK_SMART_POINTER_DEFINE_OPERATOR(<) 00213 VTK_SMART_POINTER_DEFINE_OPERATOR(<=) 00214 VTK_SMART_POINTER_DEFINE_OPERATOR(>) 00215 VTK_SMART_POINTER_DEFINE_OPERATOR(>=) 00216 00217 #undef VTK_SMART_POINTER_DEFINE_OPERATOR 00218 00220 00221 template <class T> 00222 inline ostream& operator << (ostream& os, const vtkSmartPointer<T>& p) 00224 { 00225 return os << static_cast<const vtkSmartPointerBase&>(p); 00226 } 00227 00228 #endif