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 }
00069
00071 ObjectType *operator -> () const
00072 { return m_Pointer; }
00073
00075 operator ObjectType * () const
00076 { return m_Pointer; }
00077
00079 bool IsNotNull() const
00080 { return m_Pointer != 0; }
00081 bool IsNull() const
00082 { return m_Pointer == 0; }
00084
00086 template <typename R>
00087 bool operator == ( R r ) const
00088 { return (m_Pointer == static_cast<const ObjectType*>(r) ); }
00089
00090 template <typename R>
00091 bool operator != ( R r ) const
00092 { return (m_Pointer != static_cast<const ObjectType*>(r) ); }
00093
00095 ObjectType *GetPointer () const
00096 { return m_Pointer; }
00097
00099 bool operator < (const SmartPointer &r) const
00100 { return (void*)m_Pointer < (void*) r.m_Pointer; }
00101
00103 bool operator > (const SmartPointer &r) const
00104 { return (void*)m_Pointer > (void*) r.m_Pointer; }
00105
00107 bool operator <= (const SmartPointer &r) const
00108 { return (void*)m_Pointer <= (void*) r.m_Pointer; }
00109
00111 bool operator >= (const SmartPointer &r) const
00112 { return (void*)m_Pointer >= (void*) r.m_Pointer; }
00113
00115 SmartPointer &operator = (const SmartPointer &r)
00116 { return this->operator = (r.GetPointer()); }
00117
00119 SmartPointer &operator = (ObjectType *r)
00120 {
00121 if (m_Pointer != r)
00122 {
00123 ObjectType* tmp = m_Pointer;
00124 m_Pointer = r;
00125 this->Register();
00126 if ( tmp ) { tmp->UnRegister(); }
00127 }
00128 return *this;
00129 }
00131
00133 ObjectType *Print (std::ostream& os) const
00134 {
00135
00136 (*m_Pointer).Print(os);
00137 return m_Pointer;
00138 }
00140
00141 private:
00143 ObjectType* m_Pointer;
00144
00145 void Register()
00146 {
00147 if(m_Pointer) { m_Pointer->Register(); }
00148 }
00149
00150 void UnRegister()
00151 {
00152 if(m_Pointer) { m_Pointer->UnRegister(); }
00153 }
00154 };
00155
00156
00157 template <typename T>
00158 std::ostream& operator<< (std::ostream& os, SmartPointer<T> p)
00159 {
00160 p.Print(os);
00161 return os;
00162 }
00163
00164 }
00165
00166 #endif
00167