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 
00045 template <class T>
00046 class csWeakRef
00047 {
00048 private:
00049   T* obj;
00050 
00056   void Unlink ()
00057   {
00058     if (obj) obj->RemoveRefOwner ((iBase**)&obj);
00059   }
00060 
00064   void Link ()
00065   {
00066     if (obj) obj->AddRefOwner ((iBase**)&obj);
00067   }
00068 
00069 public:
00073   csWeakRef () : obj (0) {}
00074 
00078   csWeakRef (T* newobj)
00079   {
00080     obj = newobj;
00081     Link ();
00082   }
00083 
00087   csWeakRef (csRef<T> const& newobj)
00088   {
00089     obj = newobj;
00090     Link ();
00091   }
00092 
00096   csWeakRef (csWeakRef const& other) : obj (other.obj)
00097   {
00098     Link ();
00099   }
00100 
00105   csWeakRef (const csPtr<T>& newobj)
00106   {
00107     csRef<T> r = newobj;
00108     obj = r;
00109     Link ();
00110   }
00111 
00115   ~csWeakRef ()
00116   {
00117     Unlink ();
00118   }
00119 
00123   csWeakRef& operator = (T* newobj)
00124   {
00125     if (obj != newobj)
00126     {
00127       Unlink ();
00128       obj = newobj;
00129       Link ();
00130     }
00131     return *this;
00132   }
00133 
00137   csWeakRef& operator = (csRef<T> const& newobj)
00138   {
00139     if (newobj != obj)
00140     {
00141       Unlink ();
00142       obj = newobj;
00143       Link ();
00144     }
00145     return *this;
00146   }
00147 
00152   csWeakRef& operator = (csPtr<T> newobj)
00153   {
00154     csRef<T> r = newobj;
00155     if (obj != r)
00156     {
00157       Unlink ();
00158       obj = r;
00159       Link ();
00160     }
00161     return *this;
00162   }
00163 
00167   csWeakRef& operator = (csWeakRef const& other)
00168   {
00169     this->operator=(other.obj);
00170     return *this;
00171   }
00172 
00174   inline friend bool operator == (const csWeakRef& r1, const csWeakRef& r2)
00175   {
00176     return r1.obj == r2.obj;
00177   }
00179   inline friend bool operator != (const csWeakRef& r1, const csWeakRef& r2)
00180   {
00181     return r1.obj != r2.obj;
00182   }
00184   inline friend bool operator == (const csWeakRef& r1, T* obj)
00185   {
00186     return r1.obj == obj;
00187   }
00189   inline friend bool operator != (const csWeakRef& r1, T* obj)
00190   {
00191     return r1.obj != obj;
00192   }
00194   inline friend bool operator == (T* obj, const csWeakRef& r1)
00195   {
00196     return r1.obj == obj;
00197   }
00199   inline friend bool operator != (T* obj, const csWeakRef& r1)
00200   {
00201     return r1.obj != obj;
00202   }
00203 
00205   T* operator -> () const
00206   { return obj; }
00207   
00209   operator T* () const
00210   { return obj; }
00211   
00213   T& operator* () const
00214   { return *obj; }
00215 
00220   bool IsValid () const
00221   { return (obj != 0); }
00222 };
00223 
00224 #endif // __CS_WEAKREF_H__
00225 

Generated for Crystal Space by doxygen 1.4.6