00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
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
00152
00153
00154
00155
00156 #if defined(__HP_aCC) || defined(__IBMCPP__)
00157 # define VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(op) \
00158 vtkstd_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
00177
00178 void TakeReference(const vtkSmartPointerBase&);
00179 static void Take(const vtkSmartPointerBase&);
00180 };
00181
00182 #define VTK_SMART_POINTER_DEFINE_OPERATOR(op) \
00183 template <class T> \
00184 inline vtkstd_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 vtkstd_bool operator op (T* l, const vtkSmartPointer<T>& r) \
00191 { \
00192 return (l op r.GetPointer()); \
00193 } \
00194 template <class T> \
00195 inline vtkstd_bool operator op (const vtkSmartPointer<T>& l, T* r) \
00196 { \
00197 return (l.GetPointer() op r); \
00198 }
00199
00200 VTK_SMART_POINTER_DEFINE_OPERATOR(==)
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
00207 #undef VTK_SMART_POINTER_DEFINE_OPERATOR
00208
00210
00211 template <class T>
00212 inline ostream& operator << (ostream& os, const vtkSmartPointer<T>& p)
00214 {
00215 return os << static_cast<const vtkSmartPointerBase&>(p);
00216 }
00217
00218 #endif