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: 2006-03-06 15:07:34 $
00007   Version:   $Revision: 1.38 $
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 __itkSmartPointer_h
00018 #define __itkSmartPointer_h
00019 
00020 #include "itkMacro.h"
00021 #include <iostream>
00022 
00023 namespace itk
00024 {
00025 
00042 template <class TObjectType>
00043 class ITK_EXPORT SmartPointer 
00044 {
00045 public:
00046   typedef TObjectType ObjectType;
00047   
00049   SmartPointer () 
00050     { m_Pointer = 0; }
00051 
00053   SmartPointer (const SmartPointer<ObjectType> &p):
00054     m_Pointer(p.m_Pointer)
00055     { this->Register(); }
00056 
00058   SmartPointer (ObjectType *p):
00059     m_Pointer(p)
00060     { this->Register(); }                             
00061 
00063   ~SmartPointer ()
00064     {
00065     this->UnRegister();
00066     m_Pointer = 0;  
00067     }
00069 
00071   ObjectType *operator -> () const
00072     { return m_Pointer; }
00073 
00075   operator ObjectType * () const 
00076     { return m_Pointer; }
00077 
00079   bool IsNotNull() const
00080   { return m_Pointer != 0; }
00081   bool IsNull() const
00082   { return m_Pointer == 0; }
00084 
00086   template <typename R>
00087   bool operator == ( R r ) const
00088     { return (m_Pointer == static_cast<const ObjectType*>(r) ); }
00089 
00090   template <typename R>
00091   bool operator != ( R r ) const
00092     { return (m_Pointer != static_cast<const 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 = (ObjectType *r)
00120     {                                                              
00121     if (m_Pointer != r)
00122       {
00123       ObjectType* tmp = m_Pointer; //avoid recursive unregisters by retaining temporarily
00124       m_Pointer = r;
00125       this->Register();
00126       if ( tmp ) { tmp->UnRegister(); }
00127       }
00128     return *this;
00129     }
00131 
00133   ObjectType *Print (std::ostream& os) const 
00134     { 
00135     // This prints the object pointed to by the pointer  
00136     (*m_Pointer).Print(os);  
00137     return m_Pointer;
00138     } 
00140 
00141 private:
00143   ObjectType* m_Pointer;
00144 
00145   void Register()
00146     { 
00147     if(m_Pointer) { m_Pointer->Register(); }
00148     }
00149   
00150   void UnRegister()
00151     {
00152     if(m_Pointer) { m_Pointer->UnRegister(); }
00153     }
00154 };  
00155 
00156   
00157 template <typename T>
00158 std::ostream& operator<< (std::ostream& os, SmartPointer<T> p) 
00159 {
00160   p.Print(os); 
00161   return os;
00162 }
00163 
00164 } // end namespace itk
00165   
00166 #endif
00167 

Generated at Mon Apr 14 14:27:37 2008 for ITK by doxygen 1.5.1 written by Dimitri van Heesch, © 1997-2000