CrystalSpace

Public API Reference

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 #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 };
00255 
00256 #endif // __CS_WEAKREF_H__
00257 

Generated for Crystal Space 1.2.1 by doxygen 1.5.3