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
00045 template <class TObjectType>
00046 class ITK_EXPORT AutoPointer
00047 {
00048 public:
00050 typedef TObjectType ObjectType;
00051 typedef AutoPointer Self;
00052
00054 AutoPointer ():
00055 m_Pointer(0),
00056 m_IsOwner(false)
00057 { }
00058
00060 explicit AutoPointer ( AutoPointer & p )
00061 {
00062 m_IsOwner = p.IsOwner();
00063 m_Pointer = p.ReleaseOwnership();
00064 }
00065
00066
00068 explicit AutoPointer ( ObjectType * p, bool takeOwnership ):
00069 m_Pointer(p),
00070 m_IsOwner(takeOwnership)
00071 { }
00072
00073
00075 ~AutoPointer ()
00076 {
00077 if( m_IsOwner && m_Pointer )
00078 {
00079 delete m_Pointer;
00080 }
00081 m_Pointer = 0;
00082 m_IsOwner = false;
00083 }
00084
00086 ObjectType *operator -> () const
00087 { return m_Pointer; }
00088
00091 void Reset( void )
00092 {
00093 if( m_IsOwner && m_Pointer )
00094 {
00095 delete m_Pointer;
00096 }
00097 m_Pointer = 0;
00098 m_IsOwner = false;
00099 }
00100
00101
00103 void TakeOwnership(void)
00104 { m_IsOwner = true; }
00105
00107 void TakeOwnership(ObjectType * objectptr)
00108 {
00109 if( m_IsOwner && m_Pointer )
00110 {
00111 delete m_Pointer;
00112 }
00113 m_Pointer = objectptr;
00114 m_IsOwner = true;
00115 }
00116
00118 void TakeNoOwnership(ObjectType * objectptr)
00119 {
00120 if( m_IsOwner && m_Pointer )
00121 {
00122 delete m_Pointer;
00123 }
00124 m_Pointer = objectptr;
00125 m_IsOwner = false;
00126 }
00127
00129 bool IsOwner(void) const
00130 { return m_IsOwner; }
00131
00146 ObjectType * ReleaseOwnership( void )
00147 {
00148 m_IsOwner = false;
00149 return m_Pointer;
00150 }
00151
00153 ObjectType *GetPointer () const
00154 { return m_Pointer; }
00155
00157 bool operator == (const AutoPointer &r) const
00158 { return (void*)m_Pointer == (void*) r.m_Pointer; }
00159
00161 bool operator != (const AutoPointer &r) const
00162 { return (void*)m_Pointer != (void*) r.m_Pointer; }
00163
00165 bool operator < (const AutoPointer &r) const
00166 { return (void*)m_Pointer < (void*) r.m_Pointer; }
00167
00169 bool operator > (const AutoPointer &r) const
00170 { return (void*)m_Pointer > (void*) r.m_Pointer; }
00171
00173 bool operator <= (const AutoPointer &r) const
00174 { return (void*)m_Pointer <= (void*) r.m_Pointer; }
00175
00177 bool operator >= (const AutoPointer &r) const
00178 { return (void*)m_Pointer >= (void*) r.m_Pointer; }
00179
00181 AutoPointer &operator = (AutoPointer &r) const
00182 {
00183 AutoPointer( r ).Swap( *this );
00184 return *this;
00185 }
00186
00189 operator bool () const
00190 { return (m_Pointer!=NULL); }
00191
00193 ObjectType *Print (std::ostream& os) const
00194 {
00195
00196 (*m_Pointer).Print(os);
00197 os << "Owner: " << m_IsOwner << std::endl;
00198 return m_Pointer;
00199 }
00200
00201 private:
00202
00204 void Swap(AutoPointer &r) throw()
00205 {
00206 ObjectType * temp = m_Pointer;
00207 m_Pointer = r.m_Pointer;
00208 r.m_Pointer = temp;
00209 }
00210
00211
00213 ObjectType* m_Pointer;
00214 bool m_IsOwner;
00215 };
00216
00217
00218 template <typename T>
00219 std::ostream& operator<< (std::ostream& os, AutoPointer<T> p)
00220 {
00221 p.Print(os);
00222 os << "Owner: " << p.IsOwner() << std::endl;
00223 return os;
00224 }
00225
00226
00229 template <typename TAutoPointerBase, typename TAutoPointerDerived>
00230 void
00231 ITK_EXPORT TransferAutoPointer(TAutoPointerBase & pa, TAutoPointerDerived & pb)
00232 {
00233 pa.TakeNoOwnership( pb.GetPointer() );
00234 if( pb.IsOwner() )
00235 {
00236 pa.TakeOwnership();
00237 pb.ReleaseOwnership();
00238 }
00239 }
00240
00241
00242 }
00243
00244 #endif