itkAutoPointer.h
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) const
00181 {
00182 AutoPointer( r ).Swap( *this );
00183 return *this;
00184 }
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 }
00201
00202 private:
00203
00205 void Swap(AutoPointer &r) throw()
00206 {
00207 ObjectType * temp = m_Pointer;
00208 m_Pointer = r.m_Pointer;
00209 r.m_Pointer = temp;
00210 }
00211
00212
00214 ObjectType* m_Pointer;
00215 bool m_IsOwner;
00216 };
00217
00218
00219 template <typename T>
00220 std::ostream& operator<< (std::ostream& os, AutoPointer<T> p)
00221 {
00222 p.Print(os);
00223 os << "Owner: " << p.IsOwner() << std::endl;
00224 return os;
00225 }
00226
00227
00230 template <typename TAutoPointerBase, typename TAutoPointerDerived>
00231 void
00232 ITK_EXPORT TransferAutoPointer(TAutoPointerBase & pa, TAutoPointerDerived & pb)
00233 {
00234
00235 pa.TakeNoOwnership( pb.GetPointer() );
00236 if( pb.IsOwner() )
00237 {
00238 pa.TakeOwnership();
00239 pb.ReleaseOwnership();
00240 }
00241 }
00243
00244
00245 }
00246
00247 #endif
00248