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

itkWeakPointer.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkWeakPointer.h,v $
00005   Language:  C++
00006   Date:      $Date: 2006-05-10 20:27:16 $
00007   Version:   $Revision: 1.10 $
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 __itkWeakPointer_h
00018 #define __itkWeakPointer_h
00019 
00020 #include "itkMacro.h"
00021 #include <iostream>
00022 
00023 namespace itk
00024 {
00025 
00042 template <class TObjectType>
00043 class ITK_EXPORT WeakPointer
00044 {
00045 public:
00047   typedef TObjectType ObjectType;
00048 
00050   WeakPointer ()
00051     { m_Pointer = 0; }
00052 
00054   WeakPointer (const WeakPointer<ObjectType> &p): m_Pointer(p.m_Pointer) {}
00055 
00057   WeakPointer (ObjectType *p): m_Pointer(p) { }
00058 
00060   ~WeakPointer ()
00061     { m_Pointer = 0; }
00062 
00064   ObjectType *operator -> () const
00065     { return m_Pointer; }
00066 
00068   operator ObjectType * () const
00069     { return m_Pointer; }
00070 
00072   template <typename R>
00073   bool operator == (R r) const
00074     {
00075     return (m_Pointer == (ObjectType*)r);
00076     }
00077   template <typename R>
00078   bool operator != (R r) const
00079     {
00080     return (m_Pointer != (ObjectType*)r);
00081     }
00083 
00085   ObjectType *GetPointer () const
00086     { return m_Pointer; }
00087 
00089   bool operator < (const WeakPointer &r) const
00090     { return (void*)m_Pointer < (void*) r.m_Pointer; }
00091 
00093   bool operator > (const WeakPointer &r) const
00094     { return (void*)m_Pointer > (void*) r.m_Pointer; }
00095 
00097   bool operator <= (const WeakPointer &r) const
00098     { return (void*)m_Pointer <= (void*) r.m_Pointer; }
00099 
00101   bool operator >= (const WeakPointer &r) const
00102     { return (void*)m_Pointer >= (void*) r.m_Pointer; }
00103 
00105   WeakPointer &operator = (const WeakPointer &r)
00106     { return this->operator = (r.GetPointer()); }
00107 
00109   WeakPointer &operator = (ObjectType *r)
00110     {
00111     m_Pointer = r;
00112     return *this;
00113     }
00114 
00116   ObjectType *Print (std::ostream& os) const
00117     {
00118     // This prints the object pointed to by the pointer
00119     (*m_Pointer).Print(os);
00120     return m_Pointer;
00121     }
00123 
00124 private:
00126   ObjectType* m_Pointer;
00127 };
00128 
00129 
00130 template <typename T>
00131 std::ostream& operator<< (std::ostream& os, WeakPointer<T> p)
00132 {
00133   p.Print(os);
00134   return os;
00135 }
00136 
00137 } // end namespace itk
00138 
00139 #endif
00140 

Generated at Mon Jul 12 2010 20:18:56 for ITK by doxygen 1.7.1 written by Dimitri van Heesch, © 1997-2000