AI_Ref.h
Go to the documentation of this file.00001 #ifndef AI_REF_H 00002 #define AI_REF_H 00003 //------------------------------------------------------------------------------ 00036 #include "AI_Referenced.h" 00037 00038 //------------------------------------------------------------------------------ 00039 template<class TYPE> class AI_Ref : public AI_Node 00040 { 00041 public: 00043 AI_Ref(); 00045 AI_Ref(TYPE* o); 00047 AI_Ref(const AI_Ref&); 00049 ~AI_Ref(); 00051 AI_Ref& operator=(TYPE *obj); 00053 AI_Ref& operator=(const AI_Ref& rhs); 00055 bool operator==( const AI_Ref& rhs); 00057 bool operator!=(const AI_Ref<TYPE>& rhs); 00059 bool operator==(TYPE* obj); 00061 bool operator!=(TYPE* obj); 00063 TYPE* operator->() const; 00065 TYPE& operator*() const; 00067 operator TYPE*() const; 00069 bool isvalid() const; 00071 void invalidate(); 00073 void set(TYPE *obj); 00075 TYPE* get() const; 00077 TYPE* get_unsafe() const; 00078 00079 protected: 00080 TYPE* targetObject; 00081 }; 00082 00083 //------------------------------------------------------------------------------ 00086 template<class TYPE> 00087 inline 00088 AI_Ref<TYPE>::AI_Ref() : 00089 targetObject(0) 00090 { 00091 // empty 00092 } 00093 00094 //------------------------------------------------------------------------------ 00097 template<class TYPE> 00098 inline 00099 AI_Ref<TYPE>::AI_Ref(TYPE* o) : 00100 targetObject(o) 00101 { 00102 ai_assert(o); 00103 ((AI_Referenced*)this->targetObject)->AddObjectRef((AI_Ref<AI_Referenced>*)this); 00104 } 00105 00106 //------------------------------------------------------------------------------ 00109 template<class TYPE> 00110 inline 00111 AI_Ref<TYPE>::AI_Ref( const AI_Ref<TYPE>& rhs ) : 00112 targetObject( rhs.get() ) 00113 { 00114 if (targetObject) 00115 { 00116 ((AI_Referenced*)this->targetObject)->AddObjectRef((AI_Ref<AI_Referenced> *)this); 00117 } 00118 } 00119 00120 //------------------------------------------------------------------------------ 00123 template<class TYPE> 00124 inline 00125 AI_Ref<TYPE>::~AI_Ref() 00126 { 00127 if (this->targetObject) 00128 { 00129 ((AI_Referenced*)this->targetObject)->RemObjectRef((AI_Ref<AI_Referenced> *)this); 00130 this->targetObject = 0; 00131 } 00132 } 00133 00134 //------------------------------------------------------------------------------ 00137 template<class TYPE> 00138 inline 00139 void 00140 AI_Ref<TYPE>::invalidate() 00141 { 00142 if (this->targetObject) 00143 { 00144 ((AI_Referenced*)this->targetObject)->RemObjectRef((AI_Ref<AI_Referenced> *)this); 00145 } 00146 this->targetObject = 0; 00147 } 00148 00149 //------------------------------------------------------------------------------ 00152 template<class TYPE> 00153 inline 00154 void 00155 AI_Ref<TYPE>::set(TYPE* obj) 00156 { 00157 this->invalidate(); 00158 this->targetObject = obj; 00159 if (obj) 00160 { 00161 ((AI_Referenced*)this->targetObject)->AddObjectRef((AI_Ref<AI_Referenced> *)this); 00162 } 00163 } 00164 00165 //------------------------------------------------------------------------------ 00168 template<class TYPE> 00169 inline 00170 TYPE* 00171 AI_Ref<TYPE>::get() const 00172 { 00173 ai_assert2(this->targetObject, "Null pointer access through AI_Ref!"); 00174 return (TYPE*) this->targetObject; 00175 } 00176 00177 //------------------------------------------------------------------------------ 00180 template<class TYPE> 00181 inline 00182 TYPE* 00183 AI_Ref<TYPE>::get_unsafe() const 00184 { 00185 return (TYPE*) this->targetObject; 00186 } 00187 00188 //------------------------------------------------------------------------------ 00191 template<class TYPE> 00192 inline 00193 bool 00194 AI_Ref<TYPE>::isvalid(void) const 00195 { 00196 return this->targetObject ? true : false; 00197 } 00198 00199 //------------------------------------------------------------------------------ 00202 template<class TYPE> 00203 inline 00204 TYPE* 00205 AI_Ref<TYPE>::operator->() const 00206 { 00207 ai_assert2(this->targetObject, "Null pointer access through AI_Ref!"); 00208 return this->targetObject; 00209 } 00210 00211 //------------------------------------------------------------------------------ 00214 template<class TYPE> 00215 inline 00216 TYPE& 00217 AI_Ref<TYPE>::operator*() const 00218 { 00219 ai_assert2(this->targetObject, "Null pointer access through AI_Ref!"); 00220 return *this->targetObject; 00221 } 00222 00223 //------------------------------------------------------------------------------ 00227 template<class TYPE> 00228 inline 00229 AI_Ref<TYPE>& 00230 AI_Ref<TYPE>::operator=(const AI_Ref<TYPE>& rhs) 00231 { 00232 this->set(rhs.targetObject); 00233 return *this; 00234 } 00235 00236 //------------------------------------------------------------------------------ 00239 template<class TYPE> 00240 inline 00241 AI_Ref<TYPE>& 00242 AI_Ref<TYPE>::operator=(TYPE *obj) 00243 { 00244 this->set(obj); 00245 return *this; 00246 } 00247 00248 //------------------------------------------------------------------------------ 00251 template<class TYPE> 00252 inline 00253 AI_Ref<TYPE>::operator TYPE*() const 00254 { 00255 ai_assert2(this->targetObject, "Null pointer access through AI_Ref!"); 00256 return this->targetObject; 00257 } 00258 00259 //------------------------------------------------------------------------------ 00263 template<class TYPE> 00264 inline 00265 bool 00266 AI_Ref<TYPE>::operator==(const AI_Ref<TYPE>& rhs) 00267 { 00268 return (this->targetObject == rhs.targetObject); 00269 } 00270 00271 //------------------------------------------------------------------------------ 00275 template<class TYPE> 00276 inline 00277 bool 00278 AI_Ref<TYPE>::operator!=(const AI_Ref<TYPE>& rhs) 00279 { 00280 return (this->targetObject != rhs.targetObject); 00281 } 00282 00283 //------------------------------------------------------------------------------ 00287 template<class TYPE> 00288 inline 00289 bool 00290 AI_Ref<TYPE>::operator==(TYPE* obj) 00291 { 00292 return (obj == this->targetObject); 00293 } 00294 00295 //------------------------------------------------------------------------------ 00299 template<class TYPE> 00300 inline 00301 bool 00302 AI_Ref<TYPE>::operator!=(TYPE* obj) 00303 { 00304 return (obj != this->targetObject); 00305 } 00306 00307 //------------------------------------------------------------------------------ 00308 #endif