00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
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 static T* CheckType(T* t) { return t; }
00034 public:
00036 vtkSmartPointer() {}
00037
00039 vtkSmartPointer(T* r): vtkSmartPointerBase(r) {}
00040
00042
00044 template <class U>
00045 vtkSmartPointer(const vtkSmartPointer<U>& r):
00046 vtkSmartPointerBase(CheckType(r.GetPointer())) {}
00048
00050
00052 vtkSmartPointer& operator=(T* r)
00053 {
00054 this->vtkSmartPointerBase::operator=(r);
00055 return *this;
00056 }
00058
00060
00062 template <class U>
00063 vtkSmartPointer& operator=(const vtkSmartPointer<U>& r)
00064 {
00065 this->vtkSmartPointerBase::operator=(CheckType(r.GetPointer()));
00066 return *this;
00067 }
00069
00071
00072 T* GetPointer() const
00073 {
00074 return static_cast<T*>(this->Object);
00075 }
00077
00079
00080 operator T* () const
00081 {
00082 return static_cast<T*>(this->Object);
00083 }
00085
00087
00089 T& operator*() const
00090 {
00091 return *static_cast<T*>(this->Object);
00092 }
00094
00096
00097 T* operator->() const
00098 {
00099 return static_cast<T*>(this->Object);
00100 }
00102
00104
00111 void TakeReference(T* t)
00112 {
00113 *this = vtkSmartPointer<T>(t, NoReference());
00114 }
00116
00118
00119 static vtkSmartPointer<T> New()
00120 {
00121 return vtkSmartPointer<T>(T::New(), NoReference());
00122 }
00124
00126
00127 static vtkSmartPointer<T> NewInstance(T* t)
00128 {
00129 return vtkSmartPointer<T>(t->NewInstance(), NoReference());
00130 }
00132
00134
00142 static vtkSmartPointer<T> Take(T* t)
00143 {
00144 return vtkSmartPointer<T>(t, NoReference());
00145 }
00147
00148
00149
00150
00151
00152
00153 #if defined(__HP_aCC) || defined(__IBMCPP__)
00154 # define VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(op) \
00155 vtkstd_bool operator op (NullPointerOnly*) const \
00156 { \
00157 return ::operator op (*this, 0); \
00158 }
00159 private:
00160 class NullPointerOnly {};
00161 public:
00162 VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(==)
00163 VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(!=)
00164 VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(<)
00165 VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(<=)
00166 VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(>)
00167 VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(>=)
00168 # undef VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND
00169 #endif
00170 protected:
00171 vtkSmartPointer(T* r, const NoReference& n): vtkSmartPointerBase(r, n) {}
00172 private:
00173
00174
00175 void TakeReference(const vtkSmartPointerBase&);
00176 static void Take(const vtkSmartPointerBase&);
00177 };
00178
00179 #define VTK_SMART_POINTER_DEFINE_OPERATOR(op) \
00180 template <class T> \
00181 inline vtkstd_bool \
00182 operator op (const vtkSmartPointer<T>& l, const vtkSmartPointer<T>& r) \
00183 { \
00184 return (l.GetPointer() op r.GetPointer()); \
00185 } \
00186 template <class T> \
00187 inline vtkstd_bool operator op (T* l, const vtkSmartPointer<T>& r) \
00188 { \
00189 return (l op r.GetPointer()); \
00190 } \
00191 template <class T> \
00192 inline vtkstd_bool operator op (const vtkSmartPointer<T>& l, T* r) \
00193 { \
00194 return (l.GetPointer() op r); \
00195 }
00196
00197 VTK_SMART_POINTER_DEFINE_OPERATOR(==)
00198 VTK_SMART_POINTER_DEFINE_OPERATOR(!=)
00199 VTK_SMART_POINTER_DEFINE_OPERATOR(<)
00200 VTK_SMART_POINTER_DEFINE_OPERATOR(<=)
00201 VTK_SMART_POINTER_DEFINE_OPERATOR(>)
00202 VTK_SMART_POINTER_DEFINE_OPERATOR(>=)
00203
00204 #undef VTK_SMART_POINTER_DEFINE_OPERATOR
00205
00207
00208 template <class T>
00209 inline ostream& operator << (ostream& os, const vtkSmartPointer<T>& p)
00211 {
00212 return os << static_cast<const vtkSmartPointerBase&>(p);
00213 }
00214
00215 #endif