00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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):
00055 m_Pointer(p.m_Pointer) { }
00056
00058 WeakPointer (ObjectType *p):
00059 m_Pointer(p) { }
00060
00062 ~WeakPointer ()
00063 { m_Pointer = 0; }
00064
00066 ObjectType *operator -> () const
00067 { return m_Pointer; }
00068
00070 operator ObjectType * () const
00071 { return m_Pointer; }
00072
00074 template <typename R>
00075 bool operator == (R r) const
00076 {
00077 return (m_Pointer == (ObjectType*)r);
00078 }
00079 template <typename R>
00080 bool operator != (R r) const
00081 {
00082 return (m_Pointer != (ObjectType*)r);
00083 }
00084
00086 ObjectType *GetPointer () const
00087 { return m_Pointer; }
00088
00090 bool operator < (const WeakPointer &r)
00091 { return (void*)m_Pointer < (void*) r.m_Pointer; }
00092
00094 bool operator > (const WeakPointer &r)
00095 { return (void*)m_Pointer > (void*) r.m_Pointer; }
00096
00098 bool operator <= (const WeakPointer &r)
00099 { return (void*)m_Pointer <= (void*) r.m_Pointer; }
00100
00102 bool operator >= (const WeakPointer &r)
00103 { return (void*)m_Pointer >= (void*) r.m_Pointer; }
00104
00106 WeakPointer &operator = (const WeakPointer &r)
00107 { return this->operator = (r.GetPointer()); }
00108
00110 WeakPointer &operator = (ObjectType *r)
00111 {
00112 m_Pointer = r;
00113 return *this;
00114 }
00115
00117 ObjectType *Print (std::ostream& os) const
00118 {
00119
00120 (*m_Pointer).Print(os);
00121 return m_Pointer;
00122 }
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 }
00138
00139 #endif