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

itkSmartPointer.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkSmartPointer.h,v $
00005   Language:  C++
00006   Date:      $Date: 2002/05/09 13:51:18 $
00007   Version:   $Revision: 1.31 $
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 __itkSmartPointer_h
00018 #define __itkSmartPointer_h
00019 
00020 #include "itkMacro.h"
00021 #include "itkWeakPointer.h"
00022 #include <iostream>
00023 
00024 namespace itk
00025 {
00026 
00043 template <class TObjectType>
00044 class ITK_EXPORT SmartPointer 
00045 {
00046 public:
00047   typedef TObjectType ObjectType;
00048   
00050   SmartPointer () 
00051     { m_Pointer = 0; }
00052 
00054   SmartPointer (const SmartPointer<ObjectType> &p):
00055     m_Pointer(p.m_Pointer)
00056     { this->Register(); }
00057   
00059   SmartPointer (ObjectType *p):
00060     m_Pointer(p)
00061     { this->Register(); }                             
00062   
00064   SmartPointer (const WeakPointer<ObjectType> &p)
00065     {
00066     m_Pointer = p.GetPointer();
00067     this->Register(); 
00068     }
00069   
00071   ~SmartPointer ()
00072     {
00073     this->UnRegister();
00074     m_Pointer = 0;  
00075     }
00076   
00078   ObjectType *operator -> () const
00079     { return m_Pointer; }
00080 
00082   operator ObjectType * () const 
00083     { return m_Pointer; }
00084   
00086   template <typename R>
00087   bool operator == (R r) const
00088     { return (m_Pointer == (ObjectType*)r); }
00089 
00090   template <typename R>
00091   bool operator != (R r) const
00092     { return (m_Pointer != (ObjectType*)r); }
00093     
00095   ObjectType *GetPointer () const 
00096     { return m_Pointer; }
00097   
00099   bool operator < (const SmartPointer &r) const
00100     { return (void*)m_Pointer < (void*) r.m_Pointer; }
00101   
00103   bool operator > (const SmartPointer &r) const
00104     { return (void*)m_Pointer > (void*) r.m_Pointer; }
00105 
00107   bool operator <= (const SmartPointer &r) const
00108     { return (void*)m_Pointer <= (void*) r.m_Pointer; }
00109 
00111   bool operator >= (const SmartPointer &r) const
00112     { return (void*)m_Pointer >= (void*) r.m_Pointer; }
00113 
00115   SmartPointer &operator = (const SmartPointer &r)
00116     { return this->operator = (r.GetPointer()); }
00117   
00119   SmartPointer &operator = (const WeakPointer<ObjectType> &r)
00120     { return this->operator = (r.GetPointer()); }
00121   
00123   SmartPointer &operator = (ObjectType *r)
00124     {                                                              
00125     if (m_Pointer != r)
00126       {
00127       ObjectType* tmp = m_Pointer; //avoid recursive unregisters by retaining temporarily
00128       m_Pointer = r;
00129       this->Register();
00130       if ( tmp ) { tmp->UnRegister(); }
00131       }
00132     return *this;
00133     }
00134   
00136   ObjectType *Print (std::ostream& os) const 
00137     { 
00138       // This prints the object pointed to by the pointer  
00139       (*m_Pointer).Print(os);  
00140       return m_Pointer;
00141     } 
00142 
00143 private:
00145   ObjectType* m_Pointer;
00146 
00147   void Register()
00148     { 
00149     if(m_Pointer) { m_Pointer->Register(); }
00150     }
00151   
00152   void UnRegister()
00153     {
00154     if(m_Pointer) { m_Pointer->UnRegister(); }
00155     }
00156 };  
00157 
00158   
00159 template <typename T>
00160 std::ostream& operator<< (std::ostream& os, SmartPointer<T> p) 
00161 {
00162   p.Print(os); 
00163   return os;
00164 }
00165 
00166 } // end namespace itk
00167   
00168 #endif

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