![]() |
Public API Reference |
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 00044 public: 00048 csWeakRef () : obj (0) {} 00049 00053 csWeakRef (T* newobj) 00054 { 00055 obj = newobj.obj; 00056 if (obj) obj->AddRefOwner (&obj); 00057 } 00058 00062 csWeakRef (csWeakRef const& other) : obj (other.obj) 00063 { 00064 if (obj) obj->AddRefOwner (&obj); 00065 } 00066 00070 ~csWeakRef () 00071 { 00072 if (obj) obj->RemoveRefOwner (&obj); 00073 } 00074 00078 csWeakRef& operator = (T* newobj) 00079 { 00080 if (obj != newobj) 00081 { 00082 if (obj) obj->RemoveRefOwner (&obj); 00083 obj = newobj; 00084 if (obj) obj->AddRefOwner (&obj); 00085 } 00086 return *this; 00087 } 00088 00092 csWeakRef& operator = (csWeakRef const& other) 00093 { 00094 this->operator=(other.obj); 00095 return *this; 00096 } 00097 00099 inline friend bool operator == (const csWeakRef& r1, const csWeakRef& r2) 00100 { 00101 return r1.obj == r2.obj; 00102 } 00104 inline friend bool operator != (const csWeakRef& r1, const csWeakRef& r2) 00105 { 00106 return r1.obj != r2.obj; 00107 } 00109 inline friend bool operator == (const csWeakRef& r1, T* obj) 00110 { 00111 return r1.obj == obj; 00112 } 00114 inline friend bool operator != (const csWeakRef& r1, T* obj) 00115 { 00116 return r1.obj != obj; 00117 } 00119 inline friend bool operator == (T* obj, const csWeakRef& r1) 00120 { 00121 return r1.obj == obj; 00122 } 00124 inline friend bool operator != (T* obj, const csWeakRef& r1) 00125 { 00126 return r1.obj != obj; 00127 } 00128 00130 T* operator -> () const 00131 { return obj; } 00132 00134 operator T* () const 00135 { return obj; } 00136 00138 T& operator* () const 00139 { return *obj; } 00140 00145 bool IsValid () const 00146 { return (obj != 0); } 00147 }; 00148 00149 #endif // __CS_WEAKREF_H__ 00150