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: 2002/10/01 18:37:42 $
00007   Version:   $Revision: 1.6 $
00008 
00009   Copyright (c) 2002 Insight 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 
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(); // Ownership can only be taken from another owner
00063     m_Pointer = p.ReleaseOwnership(); // release ownership if appropriate
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; // remove the current one
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; // remove the current one
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     // This prints the object pointed to by the pointer  
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() ); // give a chance to natural polymorphism
00234   if( pb.IsOwner() )
00235     {
00236     pa.TakeOwnership();      // pa Take Ownership
00237     pb.ReleaseOwnership();   // pb Release Ownership and clears
00238     }
00239 }
00240 
00241 
00242 } // end namespace itk
00243   
00244 #endif

Generated at Wed Mar 12 01:12:47 2003 for ITK by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2000