Go to the documentation of this file.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 (): m_Pointer(0),m_IsOwner(false)
00054 { }
00055
00057 explicit AutoPointer ( AutoPointer & p )
00058 {
00059 m_IsOwner = p.IsOwner();
00060 m_Pointer = p.ReleaseOwnership();
00061 }
00063
00064
00066 explicit AutoPointer ( ObjectType * p, bool takeOwnership ):
00067 m_Pointer(p), m_IsOwner(takeOwnership)
00068 { }
00069
00071 ~AutoPointer ()
00072 {
00073 if( m_IsOwner && m_Pointer )
00074 {
00075 delete m_Pointer;
00076 }
00077 m_Pointer = 0;
00078 m_IsOwner = false;
00079 }
00081
00083 ObjectType *operator -> () const
00084 { return m_Pointer; }
00085
00088 void Reset( void )
00089 {
00090 if( m_IsOwner && m_Pointer )
00091 {
00092 delete m_Pointer;
00093 }
00094 m_Pointer = 0;
00095 m_IsOwner = false;
00096 }
00098
00099
00101 void TakeOwnership(void)
00102 { m_IsOwner = true; }
00103
00105 void TakeOwnership(ObjectType * objectptr)
00106 {
00107 if( m_IsOwner && m_Pointer )
00108 {
00109 delete m_Pointer;
00110 }
00111 m_Pointer = objectptr;
00112 m_IsOwner = true;
00113 }
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 }
00127
00129 bool IsOwner(void) const
00130 { return m_IsOwner; }
00131
00144 ObjectType * ReleaseOwnership( void )
00145 {
00146 m_IsOwner = false;
00147 return m_Pointer;
00148 }
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)
00181 {
00182 AutoPointer( r ).Swap( *this );
00183 return *this;
00184 }
00186
00189 operator bool () const
00190 { return (m_Pointer!=NULL); }
00191
00193
00194
00195
00196
00197
00198
00199
00200
00202
00203 private:
00204
00206 void Swap(AutoPointer &r) throw()
00207 {
00208 ObjectType * temp = m_Pointer;
00209 m_Pointer = r.m_Pointer;
00210 r.m_Pointer = temp;
00211 }
00212
00213
00215 ObjectType* m_Pointer;
00216 bool m_IsOwner;
00217 };
00218
00219
00220 template <typename T>
00221 std::ostream& operator<< (std::ostream& os, AutoPointer<T> p)
00222 {
00223 p.Print(os);
00224 os << "Owner: " << p.IsOwner() << std::endl;
00225 return os;
00226 }
00227
00228
00231 template <typename TAutoPointerBase, typename TAutoPointerDerived>
00232 void
00233 ITK_EXPORT TransferAutoPointer(TAutoPointerBase & pa, TAutoPointerDerived & pb)
00234 {
00235
00236 pa.TakeNoOwnership( pb.GetPointer() );
00237 if( pb.IsOwner() )
00238 {
00239 pa.TakeOwnership();
00240 pb.ReleaseOwnership();
00241 }
00242 }
00244
00245
00246 }
00247
00248 #endif
00249