00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef __itkAutoPointer_h
00018 #define __itkAutoPointer_h
00019
00020 #include "itkMacro.h"
00021 #include <iostream>
00022
00023 namespace itk
00024 {
00025
00044 template <class TObjectType>
00045 class ITK_EXPORT AutoPointer
00046 {
00047 public:
00049 typedef TObjectType ObjectType;
00050 typedef AutoPointer Self;
00051
00053 AutoPointer ():
00054 m_Pointer(0),
00055 m_IsOwner(false)
00056 { }
00057
00059 explicit AutoPointer ( AutoPointer & p )
00060 {
00061 m_IsOwner = p.IsOwner();
00062 m_Pointer = p.ReleaseOwnership();
00063 }
00064
00065
00067 explicit AutoPointer ( ObjectType * p, bool takeOwnership ):
00068 m_Pointer(p),
00069 m_IsOwner(takeOwnership)
00070 { }
00071
00072
00074 ~AutoPointer ()
00075 {
00076 if( m_IsOwner && m_Pointer )
00077 {
00078 delete m_Pointer;
00079 }
00080 m_Pointer = 0;
00081 m_IsOwner = false;
00082 }
00083
00085 ObjectType *operator -> () const
00086 { return m_Pointer; }
00087
00090 void Reset( void )
00091 {
00092 if( m_IsOwner && m_Pointer )
00093 {
00094 delete m_Pointer;
00095 }
00096 m_Pointer = 0;
00097 m_IsOwner = false;
00098 }
00099
00100
00102 void TakeOwnership(void)
00103 { m_IsOwner = true; }
00104
00106 void TakeOwnership(ObjectType * objectptr)
00107 {
00108 if( m_IsOwner && m_Pointer )
00109 {
00110 delete m_Pointer;
00111 }
00112 m_Pointer = objectptr;
00113 m_IsOwner = true;
00114 }
00115
00117 void TakeNoOwnership(ObjectType * objectptr)
00118 {
00119 if( m_IsOwner && m_Pointer )
00120 {
00121 delete m_Pointer;
00122 }
00123 m_Pointer = objectptr;
00124 m_IsOwner = false;
00125 }
00126
00128 bool IsOwner(void) const
00129 { return m_IsOwner; }
00130
00145 ObjectType * ReleaseOwnership( void )
00146 {
00147 m_IsOwner = false;
00148 return m_Pointer;
00149 }
00150
00152 ObjectType *GetPointer () const
00153 { return m_Pointer; }
00154
00156 bool operator == (const AutoPointer &r) const
00157 { return (void*)m_Pointer == (void*) r.m_Pointer; }
00158
00160 bool operator != (const AutoPointer &r) const
00161 { return (void*)m_Pointer != (void*) r.m_Pointer; }
00162
00164 bool operator < (const AutoPointer &r) const
00165 { return (void*)m_Pointer < (void*) r.m_Pointer; }
00166
00168 bool operator > (const AutoPointer &r) const
00169 { return (void*)m_Pointer > (void*) r.m_Pointer; }
00170
00172 bool operator <= (const AutoPointer &r) const
00173 { return (void*)m_Pointer <= (void*) r.m_Pointer; }
00174
00176 bool operator >= (const AutoPointer &r) const
00177 { return (void*)m_Pointer >= (void*) r.m_Pointer; }
00178
00180 AutoPointer &operator = (AutoPointer &r) const
00181 {
00182 AutoPointer( r ).Swap( *this );
00183 return *this;
00184 }
00185
00188 operator bool () const
00189 { return (m_Pointer!=NULL); }
00190
00192 ObjectType *Print (std::ostream& os) const
00193 {
00194
00195 (*m_Pointer).Print(os);
00196 os << "Owner: " << m_IsOwner << std::endl;
00197 return m_Pointer;
00198 }
00199
00200 private:
00201
00203 void Swap(AutoPointer &r) throw()
00204 {
00205 ObjectType * temp = m_Pointer;
00206 m_Pointer = r.m_Pointer;
00207 r.m_Pointer = temp;
00208 }
00209
00210
00212 ObjectType* m_Pointer;
00213 bool m_IsOwner;
00214 };
00215
00216
00217 template <typename T>
00218 std::ostream& operator<< (std::ostream& os, AutoPointer<T> p)
00219 {
00220 p.Print(os);
00221 os << "Owner: " << p.IsOwner() << std::endl;
00222 return os;
00223 }
00224
00225
00228 template <typename TAutoPointerBase, typename TAutoPointerDerived>
00229 void
00230 ITK_EXPORT TransferAutoPointer(TAutoPointerBase & pa, TAutoPointerDerived & pb)
00231 {
00232 pa.TakeNoOwnership( pb.GetPointer() );
00233 if( pb.IsOwner() )
00234 {
00235 pa.TakeOwnership();
00236 pb.ReleaseOwnership();
00237 }
00238 }
00239
00240
00241 }
00242
00243 #endif