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: 2006/03/18 18:06:14 $
00007   Version:   $Revision: 1.11 $
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 (): m_Pointer(0),m_IsOwner(false)
00054     { }
00055 
00057   explicit AutoPointer ( AutoPointer & p )
00058     {
00059     m_IsOwner = p.IsOwner(); // Ownership can only be taken from another owner
00060     m_Pointer = p.ReleaseOwnership(); // release ownership if appropriate
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; // remove the current one
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; // remove the current one
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     // 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     } 
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   // give a chance to natural polymorphism
00235   pa.TakeNoOwnership( pb.GetPointer() ); 
00236   if( pb.IsOwner() )
00237     {
00238     pa.TakeOwnership();      // pa Take Ownership
00239     pb.ReleaseOwnership();   // pb Release Ownership and clears
00240     }
00241 }
00243 
00244 
00245 } // end namespace itk
00246   
00247 #endif
00248 

Generated at Wed Nov 5 20:19:28 2008 for ITK by doxygen 1.5.1 written by Dimitri van Heesch, © 1997-2000