CrystalSpace

Public API Reference

Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

csutil/weakref.h

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 
00023 #include "csextern.h"
00024 #include "csutil/ref.h"
00025 
00026 struct iBase;
00027 
00043 template <class T>
00044 class csWeakRef
00045 {
00046 private:
00047   T* obj;
00048 
00054   void Unlink ()
00055   {
00056     if (obj) obj->RemoveRefOwner ((iBase**)&obj);
00057   }
00058 
00062   void Link ()
00063   {
00064     if (obj) obj->AddRefOwner ((iBase**)&obj);
00065   }
00066 
00067 public:
00071   csWeakRef () : obj (0) {}
00072 
00076   csWeakRef (T* newobj)
00077   {
00078     obj = newobj;
00079     Link ();
00080   }
00081 
00085   csWeakRef (csRef<T> const& newobj)
00086   {
00087     obj = newobj;
00088     Link ();
00089   }
00090 
00094   csWeakRef (csWeakRef const& other) : obj (other.obj)
00095   {
00096     Link ();
00097   }
00098 
00103   csWeakRef (const csPtr<T>& newobj)
00104   {
00105     csRef<T> r = newobj;
00106     obj = r;
00107     Link ();
00108   }
00109 
00113   ~csWeakRef ()
00114   {
00115     Unlink ();
00116   }
00117 
00121   csWeakRef& operator = (T* newobj)
00122   {
00123     if (obj != newobj)
00124     {
00125       Unlink ();
00126       obj = newobj;
00127       Link ();
00128     }
00129     return *this;
00130   }
00131 
00135   csWeakRef& operator = (csRef<T> const& newobj)
00136   {
00137     if (newobj != obj)
00138     {
00139       Unlink ();
00140       obj = newobj;
00141       Link ();
00142     }
00143     return *this;
00144   }
00145 
00150   csWeakRef& operator = (csPtr<T> newobj)
00151   {
00152     csRef<T> r = newobj;
00153     if (obj != r)
00154     {
00155       Unlink ();
00156       obj = r;
00157       Link ();
00158     }
00159     return *this;
00160   }
00161 
00165   csWeakRef& operator = (csWeakRef const& other)
00166   {
00167     this->operator=(other.obj);
00168     return *this;
00169   }
00170 
00172   inline friend bool operator == (const csWeakRef& r1, const csWeakRef& r2)
00173   {
00174     return r1.obj == r2.obj;
00175   }
00177   inline friend bool operator != (const csWeakRef& r1, const csWeakRef& r2)
00178   {
00179     return r1.obj != r2.obj;
00180   }
00182   inline friend bool operator == (const csWeakRef& r1, T* obj)
00183   {
00184     return r1.obj == obj;
00185   }
00187   inline friend bool operator != (const csWeakRef& r1, T* obj)
00188   {
00189     return r1.obj != obj;
00190   }
00192   inline friend bool operator == (T* obj, const csWeakRef& r1)
00193   {
00194     return r1.obj == obj;
00195   }
00197   inline friend bool operator != (T* obj, const csWeakRef& r1)
00198   {
00199     return r1.obj != obj;
00200   }
00201 
00203   T* operator -> () const
00204   { return obj; }
00205   
00207   operator T* () const
00208   { return obj; }
00209   
00211   T& operator* () const
00212   { return *obj; }
00213 
00218   bool IsValid () const
00219   { return (obj != 0); }
00220 };
00221 
00222 #endif // __CS_WEAKREF_H__
00223 

Generated for Crystal Space by doxygen 1.2.18