Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
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;
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
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 }
00180
00181 #endif
00182