csutil/weakref.h
Go to the documentation of this file.00001 /* 00002 Crystal Space Weak Reference 00003 Copyright (C) 2003 by Jorrit Tyberghein and Matthias Braun 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public 00016 License along with this library; if not, write to the Free 00017 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00018 */ 00019 00020 #ifndef __CS_WEAKREF_H__ 00021 #define __CS_WEAKREF_H__ 00022 00027 #include "csextern.h" 00028 #include "csutil/ref.h" 00029 00030 struct iBase; 00031 00049 template <class T> 00050 class csWeakRef 00051 { 00052 private: 00053 union 00054 { 00055 T* obj; 00056 void* obj_void; 00057 }; 00058 00064 void Unlink () 00065 { 00066 if (obj) obj->RemoveRefOwner (&obj_void); 00067 } 00068 00072 void Link () 00073 { 00074 if (obj) obj->AddRefOwner (&obj_void); 00075 } 00076 00077 public: 00081 csWeakRef () : obj (0) {} 00082 00086 csWeakRef (T* newobj) 00087 { 00088 obj = newobj; 00089 Link (); 00090 } 00091 00095 csWeakRef (csRef<T> const& newobj) 00096 { 00097 obj = newobj; 00098 Link (); 00099 } 00100 00104 csWeakRef (csWeakRef const& other) : obj (other.obj) 00105 { 00106 Link (); 00107 } 00108 00113 csWeakRef (const csPtr<T>& newobj) 00114 { 00115 csRef<T> r = newobj; 00116 obj = r; 00117 Link (); 00118 } 00119 00123 ~csWeakRef () 00124 { 00125 Unlink (); 00126 } 00127 00131 csWeakRef& operator = (T* newobj) 00132 { 00133 if (obj != newobj) 00134 { 00135 Unlink (); 00136 obj = newobj; 00137 Link (); 00138 } 00139 return *this; 00140 } 00141 00145 csWeakRef& operator = (csRef<T> const& newobj) 00146 { 00147 if (newobj != obj) 00148 { 00149 Unlink (); 00150 obj = newobj; 00151 Link (); 00152 } 00153 return *this; 00154 } 00155 00160 csWeakRef& operator = (csPtr<T> newobj) 00161 { 00162 csRef<T> r = newobj; 00163 if (obj != r) 00164 { 00165 Unlink (); 00166 obj = r; 00167 Link (); 00168 } 00169 return *this; 00170 } 00171 00175 csWeakRef& operator = (csWeakRef const& other) 00176 { 00177 this->operator=(other.obj); 00178 return *this; 00179 } 00180 00182 inline friend bool operator == (const csWeakRef& r1, const csWeakRef& r2) 00183 { 00184 return r1.obj == r2.obj; 00185 } 00187 inline friend bool operator != (const csWeakRef& r1, const csWeakRef& r2) 00188 { 00189 return r1.obj != r2.obj; 00190 } 00192 inline friend bool operator == (const csWeakRef& r1, T* obj) 00193 { 00194 return r1.obj == obj; 00195 } 00197 inline friend bool operator != (const csWeakRef& r1, T* obj) 00198 { 00199 return r1.obj != obj; 00200 } 00202 inline friend bool operator == (T* obj, const csWeakRef& r1) 00203 { 00204 return r1.obj == obj; 00205 } 00207 inline friend bool operator != (T* obj, const csWeakRef& r1) 00208 { 00209 return r1.obj != obj; 00210 } 00211 00213 T* operator -> () const 00214 { return obj; } 00215 00217 operator T* () const 00218 { return obj; } 00219 00221 T& operator* () const 00222 { return *obj; } 00223 00228 bool IsValid () const 00229 { return (obj != 0); } 00230 }; 00231 00232 #endif // __CS_WEAKREF_H__ 00233
Generated for Crystal Space by doxygen 1.4.7