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 (csWeakRef const& other) : obj (other.obj)
00086   {
00087     Link ();
00088   }
00089 
00094   csWeakRef (const csPtr<T>& newobj)
00095   {
00096     csRef<T> r = newobj;
00097     obj = r;
00098     Link ();
00099   }
00100 
00104   ~csWeakRef ()
00105   {
00106     Unlink ();
00107   }
00108 
00112   csWeakRef& operator = (T* newobj)
00113   {
00114     if (obj != newobj)
00115     {
00116       Unlink ();
00117       obj = newobj;
00118       Link ();
00119     }
00120     return *this;
00121   }
00122 
00127   csWeakRef& operator = (csPtr<T> newobj)
00128   {
00129     csRef<T> r = newobj;
00130     if (obj != r)
00131     {
00132       Unlink ();
00133       obj = r;
00134       Link ();
00135     }
00136     return *this;
00137   }
00138 
00142   csWeakRef& operator = (csWeakRef const& other)
00143   {
00144     this->operator=(other.obj);
00145     return *this;
00146   }
00147 
00149   inline friend bool operator == (const csWeakRef& r1, const csWeakRef& r2)
00150   {
00151     return r1.obj == r2.obj;
00152   }
00154   inline friend bool operator != (const csWeakRef& r1, const csWeakRef& r2)
00155   {
00156     return r1.obj != r2.obj;
00157   }
00159   inline friend bool operator == (const csWeakRef& r1, T* obj)
00160   {
00161     return r1.obj == obj;
00162   }
00164   inline friend bool operator != (const csWeakRef& r1, T* obj)
00165   {
00166     return r1.obj != obj;
00167   }
00169   inline friend bool operator == (T* obj, const csWeakRef& r1)
00170   {
00171     return r1.obj == obj;
00172   }
00174   inline friend bool operator != (T* obj, const csWeakRef& r1)
00175   {
00176     return r1.obj != obj;
00177   }
00178 
00180   T* operator -> () const
00181   { return obj; }
00182   
00184   operator T* () const
00185   { return obj; }
00186   
00188   T& operator* () const
00189   { return *obj; }
00190 
00195   bool IsValid () const
00196   { return (obj != 0); }
00197 };
00198 
00199 #endif // __CS_WEAKREF_H__
00200 

Generated for Crystal Space by doxygen 1.2.18