00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef __itkSmartPointer_h
00018 #define __itkSmartPointer_h
00019
00020 #include "itkMacro.h"
00021 #include <iostream>
00022
00023 namespace itk
00024 {
00025
00042 template <class TObjectType>
00043 class ITK_EXPORT SmartPointer
00044 {
00045 public:
00046 typedef TObjectType ObjectType;
00047
00049 SmartPointer ()
00050 { m_Pointer = 0; }
00051
00053 SmartPointer (const SmartPointer<ObjectType> &p):
00054 m_Pointer(p.m_Pointer)
00055 { this->Register(); }
00056
00058 SmartPointer (ObjectType *p):
00059 m_Pointer(p)
00060 { this->Register(); }
00061
00063 ~SmartPointer ()
00064 {
00065 this->UnRegister();
00066 m_Pointer = 0;
00067 }
00068
00070 ObjectType *operator -> () const
00071 { return m_Pointer; }
00072
00074 operator ObjectType * () const
00075 { return m_Pointer; }
00076
00078 bool IsNotNull() const
00079 { return m_Pointer != 0; }
00080 bool IsNull() const
00081 { return m_Pointer == 0; }
00082
00084 template <typename R>
00085 bool operator == ( R r ) const
00086 { return (m_Pointer == static_cast<const ObjectType*>(r) ); }
00087
00088 template <typename R>
00089 bool operator != ( R r ) const
00090 { return (m_Pointer != static_cast<const ObjectType*>(r) ); }
00091
00093 ObjectType *GetPointer () const
00094 { return m_Pointer; }
00095
00097 bool operator < (const SmartPointer &r) const
00098 { return (void*)m_Pointer < (void*) r.m_Pointer; }
00099
00101 bool operator > (const SmartPointer &r) const
00102 { return (void*)m_Pointer > (void*) r.m_Pointer; }
00103
00105 bool operator <= (const SmartPointer &r) const
00106 { return (void*)m_Pointer <= (void*) r.m_Pointer; }
00107
00109 bool operator >= (const SmartPointer &r) const
00110 { return (void*)m_Pointer >= (void*) r.m_Pointer; }
00111
00113 SmartPointer &operator = (const SmartPointer &r)
00114 { return this->operator = (r.GetPointer()); }
00115
00117 SmartPointer &operator = (ObjectType *r)
00118 {
00119 if (m_Pointer != r)
00120 {
00121 ObjectType* tmp = m_Pointer;
00122 m_Pointer = r;
00123 this->Register();
00124 if ( tmp ) { tmp->UnRegister(); }
00125 }
00126 return *this;
00127 }
00128
00130 ObjectType *Print (std::ostream& os) const
00131 {
00132
00133 (*m_Pointer).Print(os);
00134 return m_Pointer;
00135 }
00136
00137 private:
00139 ObjectType* m_Pointer;
00140
00141 void Register()
00142 {
00143 if(m_Pointer) { m_Pointer->Register(); }
00144 }
00145
00146 void UnRegister()
00147 {
00148 if(m_Pointer) { m_Pointer->UnRegister(); }
00149 }
00150 };
00151
00152
00153 template <typename T>
00154 std::ostream& operator<< (std::ostream& os, SmartPointer<T> p)
00155 {
00156 p.Print(os);
00157 return os;
00158 }
00159
00160 }
00161
00162 #endif