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 "itkWeakPointer.h"
00022 #include <iostream>
00023
00024 namespace itk
00025 {
00026
00043 template <class TObjectType>
00044 class ITK_EXPORT SmartPointer
00045 {
00046 public:
00047 typedef TObjectType ObjectType;
00048
00050 SmartPointer ()
00051 { m_Pointer = 0; }
00052
00054 SmartPointer (const SmartPointer<ObjectType> &p):
00055 m_Pointer(p.m_Pointer)
00056 { this->Register(); }
00057
00059 SmartPointer (ObjectType *p):
00060 m_Pointer(p)
00061 { this->Register(); }
00062
00064 SmartPointer (const WeakPointer<ObjectType> &p)
00065 {
00066 m_Pointer = p.GetPointer();
00067 this->Register();
00068 }
00069
00071 ~SmartPointer ()
00072 {
00073 this->UnRegister();
00074 m_Pointer = 0;
00075 }
00076
00078 ObjectType *operator -> () const
00079 { return m_Pointer; }
00080
00082 operator ObjectType * () const
00083 { return m_Pointer; }
00084
00086 template <typename R>
00087 bool operator == (R r) const
00088 { return (m_Pointer == (ObjectType*)r); }
00089
00090 template <typename R>
00091 bool operator != (R r) const
00092 { return (m_Pointer != (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 = (const WeakPointer<ObjectType> &r)
00120 { return this->operator = (r.GetPointer()); }
00121
00123 SmartPointer &operator = (ObjectType *r)
00124 {
00125 if (m_Pointer != r)
00126 {
00127 ObjectType* tmp = m_Pointer;
00128 m_Pointer = r;
00129 this->Register();
00130 if ( tmp ) { tmp->UnRegister(); }
00131 }
00132 return *this;
00133 }
00134
00136 ObjectType *Print (std::ostream& os) const
00137 {
00138
00139 (*m_Pointer).Print(os);
00140 return m_Pointer;
00141 }
00142
00143 private:
00145 ObjectType* m_Pointer;
00146
00147 void Register()
00148 {
00149 if(m_Pointer) { m_Pointer->Register(); }
00150 }
00151
00152 void UnRegister()
00153 {
00154 if(m_Pointer) { m_Pointer->UnRegister(); }
00155 }
00156 };
00157
00158
00159 template <typename T>
00160 std::ostream& operator<< (std::ostream& os, SmartPointer<T> p)
00161 {
00162 p.Print(os);
00163 return os;
00164 }
00165
00166 }
00167
00168 #endif