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: 2009-08-05 19:52:39 $
00007   Version:   $Revision: 1.13 $
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)
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     } 
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   // give a chance to natural polymorphism
00236   pa.TakeNoOwnership( pb.GetPointer() ); 
00237   if( pb.IsOwner() )
00238     {
00239     pa.TakeOwnership();      // pa Take Ownership
00240     pb.ReleaseOwnership();   // pb Release Ownership and clears
00241     }
00242 }
00244 
00245 
00246 } // end namespace itk
00247   
00248 #endif
00249 

Generated at Mon Jul 12 2010 17:45:27 for ITK by doxygen 1.7.1 written by Dimitri van Heesch, © 1997-2000