CrystalSpace

Public API Reference

Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

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 
00047 template <class T>
00048 class csWeakRef
00049 {
00050 private:
00051   T* obj;
00052 
00058   void Unlink ()
00059   {
00060     if (obj) obj->RemoveRefOwner ((iBase**)&obj);
00061   }
00062 
00066   void Link ()
00067   {
00068     if (obj) obj->AddRefOwner ((iBase**)&obj);
00069   }
00070 
00071 public:
00075   csWeakRef () : obj (0) {}
00076 
00080   csWeakRef (T* newobj)
00081   {
00082     obj = newobj;
00083     Link ();
00084   }
00085 
00089   csWeakRef (csRef<T> const& newobj)
00090   {
00091     obj = newobj;
00092     Link ();
00093   }
00094 
00098   csWeakRef (csWeakRef const& other) : obj (other.obj)
00099   {
00100     Link ();
00101   }
00102 
00107   csWeakRef (const csPtr<T>& newobj)
00108   {
00109     csRef<T> r = newobj;
00110     obj = r;
00111     Link ();
00112   }
00113 
00117   ~csWeakRef ()
00118   {
00119     Unlink ();
00120   }
00121 
00125   csWeakRef& operator = (T* newobj)
00126   {
00127     if (obj != newobj)
00128     {
00129       Unlink ();
00130       obj = newobj;
00131       Link ();
00132     }
00133     return *this;
00134   }
00135 
00139   csWeakRef& operator = (csRef<T> const& newobj)
00140   {
00141     if (newobj != obj)
00142     {
00143       Unlink ();
00144       obj = newobj;
00145       Link ();
00146     }
00147     return *this;
00148   }
00149 
00154   csWeakRef& operator = (csPtr<T> newobj)
00155   {
00156     csRef<T> r = newobj;
00157     if (obj != r)
00158     {
00159       Unlink ();
00160       obj = r;
00161       Link ();
00162     }
00163     return *this;
00164   }
00165 
00169   csWeakRef& operator = (csWeakRef const& other)
00170   {
00171     this->operator=(other.obj);
00172     return *this;
00173   }
00174 
00176   inline friend bool operator == (const csWeakRef& r1, const csWeakRef& r2)
00177   {
00178     return r1.obj == r2.obj;
00179   }
00181   inline friend bool operator != (const csWeakRef& r1, const csWeakRef& r2)
00182   {
00183     return r1.obj != r2.obj;
00184   }
00186   inline friend bool operator == (const csWeakRef& r1, T* obj)
00187   {
00188     return r1.obj == obj;
00189   }
00191   inline friend bool operator != (const csWeakRef& r1, T* obj)
00192   {
00193     return r1.obj != obj;
00194   }
00196   inline friend bool operator == (T* obj, const csWeakRef& r1)
00197   {
00198     return r1.obj == obj;
00199   }
00201   inline friend bool operator != (T* obj, const csWeakRef& r1)
00202   {
00203     return r1.obj != obj;
00204   }
00205 
00207   T* operator -> () const
00208   { return obj; }
00209   
00211   operator T* () const
00212   { return obj; }
00213   
00215   T& operator* () const
00216   { return *obj; }
00217 
00222   bool IsValid () const
00223   { return (obj != 0); }
00224 };
00225 
00226 #endif // __CS_WEAKREF_H__
00227 

Generated for Crystal Space by doxygen 1.4.4