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 #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 #if defined(CS_DEBUG)
00059 void* this_saved;
00060 #endif
00061
00067 void Unlink ()
00068 {
00069 if (obj) obj->RemoveRefOwner (&obj_void);
00070 }
00071
00075 void Link ()
00076 {
00077 if (obj) obj->AddRefOwner (&obj_void);
00078 }
00079
00080 public:
00084 csWeakRef () : obj (0)
00085 {
00086 #if defined(CS_DEBUG)
00087 this_saved = this;
00088 #endif
00089 }
00090
00094 csWeakRef (T* newobj)
00095 {
00096 #if defined(CS_DEBUG)
00097 this_saved = this;
00098 #endif
00099 obj = newobj;
00100 Link ();
00101 }
00102
00106 csWeakRef (csRef<T> const& newobj)
00107 {
00108 #if defined(CS_DEBUG)
00109 this_saved = this;
00110 #endif
00111 obj = newobj;
00112 Link ();
00113 }
00114
00118 csWeakRef (csWeakRef const& other) : obj (other.obj)
00119 {
00120 #if defined(CS_DEBUG)
00121 this_saved = this;
00122 #endif
00123 Link ();
00124 }
00125
00130 csWeakRef (const csPtr<T>& newobj)
00131 {
00132 #if defined(CS_DEBUG)
00133 this_saved = this;
00134 #endif
00135 csRef<T> r = newobj;
00136 obj = r;
00137 Link ();
00138 }
00139
00143 ~csWeakRef ()
00144 {
00145 #if defined(CS_DEBUG)
00146 CS_ASSERT_MSG ("A csWeakRef<> was memcpy()ed, which is not allowed",
00147 this_saved == this);
00148 #endif
00149 Unlink ();
00150 }
00151
00155 csWeakRef& operator = (T* newobj)
00156 {
00157 if (obj != newobj)
00158 {
00159 Unlink ();
00160 obj = newobj;
00161 Link ();
00162 }
00163 return *this;
00164 }
00165
00169 csWeakRef& operator = (csRef<T> const& newobj)
00170 {
00171 if (newobj != obj)
00172 {
00173 Unlink ();
00174 obj = newobj;
00175 Link ();
00176 }
00177 return *this;
00178 }
00179
00184 csWeakRef& operator = (csPtr<T> newobj)
00185 {
00186 csRef<T> r = newobj;
00187 if (obj != r)
00188 {
00189 Unlink ();
00190 obj = r;
00191 Link ();
00192 }
00193 return *this;
00194 }
00195
00199 csWeakRef& operator = (csWeakRef const& other)
00200 {
00201 this->operator=(other.obj);
00202 return *this;
00203 }
00204
00206 inline friend bool operator == (const csWeakRef& r1, const csWeakRef& r2)
00207 {
00208 return r1.obj == r2.obj;
00209 }
00211 inline friend bool operator != (const csWeakRef& r1, const csWeakRef& r2)
00212 {
00213 return r1.obj != r2.obj;
00214 }
00216 inline friend bool operator == (const csWeakRef& r1, T* obj)
00217 {
00218 return r1.obj == obj;
00219 }
00221 inline friend bool operator != (const csWeakRef& r1, T* obj)
00222 {
00223 return r1.obj != obj;
00224 }
00226 inline friend bool operator == (T* obj, const csWeakRef& r1)
00227 {
00228 return r1.obj == obj;
00229 }
00231 inline friend bool operator != (T* obj, const csWeakRef& r1)
00232 {
00233 return r1.obj != obj;
00234 }
00235
00237 T* operator -> () const
00238 { return obj; }
00239
00241 operator T* () const
00242 { return obj; }
00243
00245 T& operator* () const
00246 { return *obj; }
00247
00252 bool IsValid () const
00253 { return (obj != 0); }
00254
00256 uint GetHash() const
00257 { return (uintptr_t)obj; }
00258 };
00259
00260 #endif // __CS_WEAKREF_H__
00261