00001 /*========================================================================= 00002 00003 Program: Insight Segmentation & Registration Toolkit 00004 Module: $RCSfile: itkSmartPointer.h,v $ 00005 Language: C++ 00006 Date: $Date: 2008-05-16 20:15:32 $ 00007 Version: $Revision: 1.39 $ 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 <iostream> 00021 00022 namespace itk 00023 { 00024 00041 template <class TObjectType> 00042 class SmartPointer 00043 { 00044 public: 00045 typedef TObjectType ObjectType; 00046 00048 SmartPointer () 00049 { m_Pointer = 0; } 00050 00052 SmartPointer (const SmartPointer<ObjectType> &p): 00053 m_Pointer(p.m_Pointer) 00054 { this->Register(); } 00055 00057 SmartPointer (ObjectType *p): 00058 m_Pointer(p) 00059 { this->Register(); } 00060 00062 ~SmartPointer () 00063 { 00064 this->UnRegister(); 00065 m_Pointer = 0; 00066 } 00068 00070 ObjectType *operator -> () const 00071 { return m_Pointer; } 00072 00074 operator ObjectType * () const 00075 { return m_Pointer; } 00076 00078 bool IsNotNull() const 00079 { return m_Pointer != 0; } 00080 bool IsNull() const 00081 { return m_Pointer == 0; } 00083 00085 template <typename R> 00086 bool operator == ( R r ) const 00087 { return (m_Pointer == static_cast<const ObjectType*>(r) ); } 00088 00089 template <typename R> 00090 bool operator != ( R r ) const 00091 { return (m_Pointer != static_cast<const ObjectType*>(r) ); } 00092 00094 ObjectType *GetPointer () const 00095 { return m_Pointer; } 00096 00098 bool operator < (const SmartPointer &r) const 00099 { return (void*)m_Pointer < (void*) r.m_Pointer; } 00100 00102 bool operator > (const SmartPointer &r) const 00103 { return (void*)m_Pointer > (void*) r.m_Pointer; } 00104 00106 bool operator <= (const SmartPointer &r) const 00107 { return (void*)m_Pointer <= (void*) r.m_Pointer; } 00108 00110 bool operator >= (const SmartPointer &r) const 00111 { return (void*)m_Pointer >= (void*) r.m_Pointer; } 00112 00114 SmartPointer &operator = (const SmartPointer &r) 00115 { return this->operator = (r.GetPointer()); } 00116 00118 SmartPointer &operator = (ObjectType *r) 00119 { 00120 if (m_Pointer != r) 00121 { 00122 ObjectType* tmp = m_Pointer; //avoid recursive unregisters by retaining temporarily 00123 m_Pointer = r; 00124 this->Register(); 00125 if ( tmp ) { tmp->UnRegister(); } 00126 } 00127 return *this; 00128 } 00130 00132 ObjectType *Print (std::ostream& os) const 00133 { 00134 // This prints the object pointed to by the pointer 00135 (*m_Pointer).Print(os); 00136 return m_Pointer; 00137 } 00139 00140 private: 00142 ObjectType* m_Pointer; 00143 00144 void Register() 00145 { 00146 if(m_Pointer) { m_Pointer->Register(); } 00147 } 00148 00149 void UnRegister() 00150 { 00151 if(m_Pointer) { m_Pointer->UnRegister(); } 00152 } 00153 }; 00154 00155 00156 template <typename T> 00157 std::ostream& operator<< (std::ostream& os, SmartPointer<T> p) 00158 { 00159 p.Print(os); 00160 return os; 00161 } 00162 00163 } // end namespace itk 00164 00165 #endif 00166