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

igtlSmartPointer.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Open IGT Link Library
00004   Module:    $HeadURL: http://svn.na-mic.org/NAMICSandBox/trunk/OpenIGTLink/Source/igtlSmartPointer.h $
00005   Language:  C++
00006   Date:      $Date: 2008-12-22 19:05:42 -0500 (Mon, 22 Dec 2008) $
00007   Version:   $Revision: 3460 $
00008 
00009   Copyright (c) Insight Software Consortium. All rights reserved.
00010 
00011   This software is distributed WITHOUT ANY WARRANTY; without even
00012   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00013   PURPOSE.  See the above copyright notices for more information.
00014 
00015 =========================================================================*/
00016 /*=========================================================================
00017 
00018   Program:   Insight Segmentation & Registration Toolkit
00019   Module:    $RCSfile: itkSmartPointer.h,v $
00020   Language:  C++
00021   Date:      $Date: 2008-12-22 19:05:42 -0500 (Mon, 22 Dec 2008) $
00022   Version:   $Revision: 3460 $
00023 
00024   Copyright (c) Insight Software Consortium. All rights reserved.
00025   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
00026 
00027      This software is distributed WITHOUT ANY WARRANTY; without even 
00028      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00029      PURPOSE.  See the above copyright notices for more information.
00030 
00031 =========================================================================*/
00032 #ifndef __igtlSmartPointer_h
00033 #define __igtlSmartPointer_h
00034 
00035 #include "igtlMacro.h"
00036 #include <iostream>
00037 
00038 namespace igtl
00039 {
00040 
00057 template <class TObjectType>
00058 class IGTL_EXPORT SmartPointer 
00059 {
00060 public:
00061   typedef TObjectType ObjectType;
00062   
00064   SmartPointer () 
00065     { m_Pointer = 0; }
00066 
00068   SmartPointer (const SmartPointer<ObjectType> &p):
00069     m_Pointer(p.m_Pointer)
00070     { this->Register(); }
00071 
00073   SmartPointer (ObjectType *p):
00074     m_Pointer(p)
00075     { this->Register(); }                             
00076 
00078   ~SmartPointer ()
00079     {
00080     this->UnRegister();
00081     m_Pointer = 0;  
00082     }
00084 
00086   ObjectType *operator -> () const
00087     { return m_Pointer; }
00088 
00090   operator ObjectType * () const 
00091     { return m_Pointer; }
00092 
00094   bool IsNotNull() const
00095   { return m_Pointer != 0; }
00096   bool IsNull() const
00097   { return m_Pointer == 0; }
00099 
00101   template <typename R>
00102   bool operator == ( R r ) const
00103     { return (m_Pointer == static_cast<const ObjectType*>(r) ); }
00104 
00105   template <typename R>
00106   bool operator != ( R r ) const
00107     { return (m_Pointer != static_cast<const ObjectType*>(r) ); }
00108     
00110   ObjectType *GetPointer () const 
00111     { return m_Pointer; }
00112 
00114   bool operator < (const SmartPointer &r) const
00115     { return (void*)m_Pointer < (void*) r.m_Pointer; }
00116 
00118   bool operator > (const SmartPointer &r) const
00119     { return (void*)m_Pointer > (void*) r.m_Pointer; }
00120 
00122   bool operator <= (const SmartPointer &r) const
00123     { return (void*)m_Pointer <= (void*) r.m_Pointer; }
00124 
00126   bool operator >= (const SmartPointer &r) const
00127     { return (void*)m_Pointer >= (void*) r.m_Pointer; }
00128 
00130   SmartPointer &operator = (const SmartPointer &r)
00131     { return this->operator = (r.GetPointer()); }
00132 
00134   SmartPointer &operator = (ObjectType *r)
00135     {                                                              
00136     if (m_Pointer != r)
00137       {
00138       ObjectType* tmp = m_Pointer; //avoid recursive unregisters by retaining temporarily
00139       m_Pointer = r;
00140       this->Register();
00141       if ( tmp ) { tmp->UnRegister(); }
00142       }
00143     return *this;
00144     }
00146 
00148   ObjectType *Print (std::ostream& os) const 
00149     { 
00150     // This prints the object pointed to by the pointer  
00151     (*m_Pointer).Print(os);  
00152     return m_Pointer;
00153     } 
00155 
00156 private:
00158   ObjectType* m_Pointer;
00159 
00160   void Register()
00161     { 
00162     if(m_Pointer) { m_Pointer->Register(); }
00163     }
00164 
00165   void UnRegister()
00166     {
00167     if(m_Pointer) { m_Pointer->UnRegister(); }
00168     }
00169 };  
00170 
00171   
00172 template <typename T>
00173 std::ostream& operator<< (std::ostream& os, SmartPointer<T> p) 
00174 {
00175   p.Print(os); 
00176   return os;
00177 }
00178 
00179 } // end namespace igtl
00180   
00181 #endif
00182 

Generated at Sat May 9 04:47:23 2009 for OpenIGTLink by doxygen 1.5.9 written by Dimitri van Heesch, © 1997-2000