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 00045 template <class T> 00046 class csWeakRef 00047 { 00048 private: 00049 union 00050 { 00051 T* obj; 00052 void* obj_void; 00053 }; 00054 00060 void Unlink () 00061 { 00062 if (obj) obj->RemoveRefOwner (&obj_void); 00063 } 00064 00068 void Link () 00069 { 00070 if (obj) obj->AddRefOwner (&obj_void); 00071 } 00072 00073 public: 00077 csWeakRef () : obj (0) {} 00078 00082 csWeakRef (T* newobj) 00083 { 00084 obj = newobj; 00085 Link (); 00086 } 00087 00091 csWeakRef (csRef<T> const& newobj) 00092 { 00093 obj = newobj; 00094 Link (); 00095 } 00096 00100 csWeakRef (csWeakRef const& other) : obj (other.obj) 00101 { 00102 Link (); 00103 } 00104 00109 csWeakRef (const csPtr<T>& newobj) 00110 { 00111 csRef<T> r = newobj; 00112 obj = r; 00113 Link (); 00114 } 00115 00119 ~csWeakRef () 00120 { 00121 Unlink (); 00122 } 00123 00127 csWeakRef& operator = (T* newobj) 00128 { 00129 if (obj != newobj) 00130 { 00131 Unlink (); 00132 obj = newobj; 00133 Link (); 00134 } 00135 return *this; 00136 } 00137 00141 csWeakRef& operator = (csRef<T> const& newobj) 00142 { 00143 if (newobj != obj) 00144 { 00145 Unlink (); 00146 obj = newobj; 00147 Link (); 00148 } 00149 return *this; 00150 } 00151 00156 csWeakRef& operator = (csPtr<T> newobj) 00157 { 00158 csRef<T> r = newobj; 00159 if (obj != r) 00160 { 00161 Unlink (); 00162 obj = r; 00163 Link (); 00164 } 00165 return *this; 00166 } 00167 00171 csWeakRef& operator = (csWeakRef const& other) 00172 { 00173 this->operator=(other.obj); 00174 return *this; 00175 } 00176 00178 inline friend bool operator == (const csWeakRef& r1, const csWeakRef& r2) 00179 { 00180 return r1.obj == r2.obj; 00181 } 00183 inline friend bool operator != (const csWeakRef& r1, const csWeakRef& r2) 00184 { 00185 return r1.obj != r2.obj; 00186 } 00188 inline friend bool operator == (const csWeakRef& r1, T* obj) 00189 { 00190 return r1.obj == obj; 00191 } 00193 inline friend bool operator != (const csWeakRef& r1, T* obj) 00194 { 00195 return r1.obj != obj; 00196 } 00198 inline friend bool operator == (T* obj, const csWeakRef& r1) 00199 { 00200 return r1.obj == obj; 00201 } 00203 inline friend bool operator != (T* obj, const csWeakRef& r1) 00204 { 00205 return r1.obj != obj; 00206 } 00207 00209 T* operator -> () const 00210 { return obj; } 00211 00213 operator T* () const 00214 { return obj; } 00215 00217 T& operator* () const 00218 { return *obj; } 00219 00224 bool IsValid () const 00225 { return (obj != 0); } 00226 }; 00227 00228 #endif // __CS_WEAKREF_H__ 00229
Generated for Crystal Space by doxygen 1.4.7