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: 2003/02/24 03:32:43 $
00007   Version:   $Revision: 1.34 $
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   bool IsNotNull() const
00087   { return m_Pointer != 0; }
00088   bool IsNull() const
00089   { return m_Pointer == 0; }
00090 
00092   template <typename R>
00093   bool operator == ( R r ) const
00094     { return (m_Pointer == (ObjectType*)(r) ); }
00095 
00096   template <typename R>
00097   bool operator != ( R r ) const
00098     { return (m_Pointer != (ObjectType*)(r) ); }
00099     
00101   ObjectType *GetPointer () const 
00102     { return m_Pointer; }
00103   
00105   bool operator < (const SmartPointer &r) const
00106     { return (void*)m_Pointer < (void*) r.m_Pointer; }
00107   
00109   bool operator > (const SmartPointer &r) const
00110     { return (void*)m_Pointer > (void*) r.m_Pointer; }
00111 
00113   bool operator <= (const SmartPointer &r) const
00114     { return (void*)m_Pointer <= (void*) r.m_Pointer; }
00115 
00117   bool operator >= (const SmartPointer &r) const
00118     { return (void*)m_Pointer >= (void*) r.m_Pointer; }
00119 
00121   SmartPointer &operator = (const SmartPointer &r)
00122     { return this->operator = (r.GetPointer()); }
00123   
00125   SmartPointer &operator = (const WeakPointer<ObjectType> &r)
00126     { return this->operator = (r.GetPointer()); }
00127   
00129   SmartPointer &operator = (ObjectType *r)
00130     {                                                              
00131     if (m_Pointer != r)
00132       {
00133       ObjectType* tmp = m_Pointer; //avoid recursive unregisters by retaining temporarily
00134       m_Pointer = r;
00135       this->Register();
00136       if ( tmp ) { tmp->UnRegister(); }
00137       }
00138     return *this;
00139     }
00140   
00142   ObjectType *Print (std::ostream& os) const 
00143     { 
00144       // This prints the object pointed to by the pointer  
00145       (*m_Pointer).Print(os);  
00146       return m_Pointer;
00147     } 
00148 
00149 private:
00151   ObjectType* m_Pointer;
00152 
00153   void Register()
00154     { 
00155     if(m_Pointer) { m_Pointer->Register(); }
00156     }
00157   
00158   void UnRegister()
00159     {
00160     if(m_Pointer) { m_Pointer->UnRegister(); }
00161     }
00162 };  
00163 
00164   
00165 template <typename T>
00166 std::ostream& operator<< (std::ostream& os, SmartPointer<T> p) 
00167 {
00168   p.Print(os); 
00169   return os;
00170 }
00171 
00172 } // end namespace itk
00173   
00174 #endif

Generated at Fri May 21 01:15:19 2004 for ITK by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2000