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 
00038 template <class T>
00039 class csWeakRef
00040 {
00041 private:
00042   T* obj;
00043 
00049   void Unlink ()
00050   {
00051     if (obj) obj->RemoveRefOwner ((iBase**)&obj);
00052   }
00053 
00057   void Link ()
00058   {
00059     if (obj) obj->AddRefOwner ((iBase**)&obj);
00060   }
00061 
00062 public:
00066   csWeakRef () : obj (0) {}
00067 
00071   csWeakRef (T* newobj)
00072   {
00073     obj = newobj;
00074     Link ();
00075   }
00076 
00080   csWeakRef (csWeakRef const& other) : obj (other.obj)
00081   {
00082     Link ();
00083   }
00084 
00088   ~csWeakRef ()
00089   {
00090     Unlink ();
00091   }
00092 
00096   csWeakRef& operator = (T* newobj)
00097   {
00098     if (obj != newobj)
00099     {
00100       Unlink ();
00101       obj = newobj;
00102       Link ();
00103     }
00104     return *this;
00105   }
00106 
00110   csWeakRef& operator = (csWeakRef const& other)
00111   {
00112     this->operator=(other.obj);
00113     return *this;
00114   }
00115 
00117   inline friend bool operator == (const csWeakRef& r1, const csWeakRef& r2)
00118   {
00119     return r1.obj == r2.obj;
00120   }
00122   inline friend bool operator != (const csWeakRef& r1, const csWeakRef& r2)
00123   {
00124     return r1.obj != r2.obj;
00125   }
00127   inline friend bool operator == (const csWeakRef& r1, T* obj)
00128   {
00129     return r1.obj == obj;
00130   }
00132   inline friend bool operator != (const csWeakRef& r1, T* obj)
00133   {
00134     return r1.obj != obj;
00135   }
00137   inline friend bool operator == (T* obj, const csWeakRef& r1)
00138   {
00139     return r1.obj == obj;
00140   }
00142   inline friend bool operator != (T* obj, const csWeakRef& r1)
00143   {
00144     return r1.obj != obj;
00145   }
00146 
00148   T* operator -> () const
00149   { return obj; }
00150   
00152   operator T* () const
00153   { return obj; }
00154   
00156   T& operator* () const
00157   { return *obj; }
00158 
00163   bool IsValid () const
00164   { return (obj != 0); }
00165 };
00166 
00167 #endif // __CS_WEAKREF_H__
00168 

Generated for Crystal Space by doxygen 1.2.18