Main Page   Groups   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Concepts

itkAutoPointer.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkAutoPointer.h,v $
00005   Language:  C++
00006   Date:      $Date: 2003/09/10 14:29:00 $
00007   Version:   $Revision: 1.9 $
00008 
00009   Copyright (c) Insight Software Consortium. All rights reserved.
00010   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
00011 
00012      This software is distributed WITHOUT ANY WARRANTY; without even 
00013      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00014      PURPOSE.  See the above copyright notices for more information.
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(); // Ownership can only be taken from another owner
00062     m_Pointer = p.ReleaseOwnership(); // release ownership if appropriate
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; // remove the current one
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; // remove the current one
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     // This prints the object pointed to by the pointer  
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() ); // give a chance to natural polymorphism
00233   if( pb.IsOwner() )
00234     {
00235     pa.TakeOwnership();      // pa Take Ownership
00236     pb.ReleaseOwnership();   // pb Release Ownership and clears
00237     }
00238 }
00239 
00240 
00241 } // end namespace itk
00242   
00243 #endif

Generated at Wed May 24 22:47:13 2006 for ITK by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2000