00001 #ifndef IOTRHANDLE 00002 #define IOTRHANDLE 00003 00004 #include <assert.h> 00005 00019 template <class T> 00020 class Handle { 00021 public: 00023 Handle() : obj(0) {}; 00025 00031 Handle( const Handle<T>& handle ) 00032 { 00033 this->obj = handle.ptr(); 00034 }; 00035 #ifdef HAVE_MEMBER_TEMPLATES 00036 template <class S> 00037 Handle( const Handle<S>& handle ) 00038 { 00039 this->obj = handle.ptr(); 00040 } 00041 #endif 00042 00043 00044 00045 T& operator*() { return *obj; }; 00046 const T& operator*() const { return *obj; }; 00048 00049 00053 Handle<T>& operator=( const Handle<T>& handle) 00054 { 00055 T * newobj = handle.ptr(); 00056 if( this->obj ) IotrRelease( &this->obj ); 00057 00058 this->obj = newobj; 00059 00060 return *this; 00061 }; 00062 #ifdef HAVE_MEMBER_TEMPLATES 00063 template <class S> 00064 Handle<T>& operator=(const Handle<S>& handle) 00065 { 00066 S * ptrin = handle.ptr(); 00067 if( this->obj ) IotrRelease( &this->obj ); 00068 00069 this->obj = ptrin; 00070 00071 return *this; 00072 } 00074 #endif 00075 00078 ~Handle() 00079 { 00080 if( obj ) IotrRelease( &obj ); 00081 } 00083 00086 T * operator->() { 00087 return obj; 00088 } 00089 const T * operator->() const { 00090 return obj; 00091 } 00093 00097 operator T*() 00098 { 00099 return obj; 00100 } 00103 static void bind( Handle<T>& handle, T ** obj ) 00104 { 00105 handle.obj = *obj; 00106 *obj = 0; 00107 } 00110 static void referTo( Handle<T>& handle, T * obj ) 00111 { 00112 if( obj ) IotrAddRef( &obj ); 00113 if( handle.obj ) IotrRelease( &handle.obj ); 00114 00115 handle.obj = obj; 00116 } 00119 T * ptr() const 00120 { 00121 if (obj) IotrAddRef( &obj ); 00122 00123 return obj; 00124 } 00125 00129 #ifdef HAVE_EXPLICIT 00130 explicit 00131 #endif 00132 Handle( T * t ) : obj(t) {} 00133 protected: 00135 T * obj; 00136 }; 00137 00141 template <class T> 00142 inline void HandleNil( Handle<T>& handle ) 00143 { 00144 T * t = 0; 00145 Handle<T>::bind( handle, &t ); 00146 } 00147 00152 template <class T> 00153 inline void HandleReferTo( Handle<T>& handle, T * obj ) 00154 { 00155 Handle<T>::referTo( handle, obj ); 00156 } 00157 00158 00166 template <class T> 00167 inline T * HandleAsPointer( Handle<T>& handle ) 00168 { 00169 return handle.ptr(); 00170 } 00171 00172 #endif