Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages
ref.h
Go to the documentation of this file.00001 /* 00002 Crystal Space Smart Pointers 00003 Copyright (C) 2002 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_REF_H__ 00021 #define __CS_REF_H__ 00022 00027 #include "csextern.h" 00028 00029 #define CS_VOIDED_PTR 0xffffffff 00030 00031 template <class T> class csRef; 00032 00033 #if defined(CS_DEBUG) 00034 # define CS_TEST_VOIDPTRUSAGE 00035 #else 00036 # undef CS_TEST_VOIDPTRUSAGE 00037 #endif 00038 00039 #ifdef CS_REF_TRACKER 00040 #include <typeinfo> 00041 #include "csutil/reftrackeraccess.h" 00042 00043 #define CSREF_TRACK(x, cmd, refCount, obj, tag) \ 00044 { \ 00045 const int rc = obj ? refCount : -1; \ 00046 if (obj) cmd; \ 00047 if (obj) \ 00048 { \ 00049 csRefTrackerAccess::SetDescription (obj, \ 00050 typeid(T).name()); \ 00051 csRefTrackerAccess::Match ## x (obj, rc, tag);\ 00052 } \ 00053 } 00054 #define CSREF_TRACK_INCREF(obj,tag) \ 00055 CSREF_TRACK(IncRef, obj->IncRef(), obj->GetRefCount(), obj, tag); 00056 #define CSREF_TRACK_DECREF(obj,tag) \ 00057 CSREF_TRACK(DecRef, obj->DecRef(), obj->GetRefCount(), obj, tag); 00058 #define CSREF_TRACK_ASSIGN(obj,tag) \ 00059 CSREF_TRACK(IncRef, (0), obj->GetRefCount() - 1, obj, tag); 00060 #else 00061 #define CSREF_TRACK_INCREF(obj,tag) \ 00062 if (obj) obj->IncRef(); 00063 #define CSREF_TRACK_DECREF(obj,tag) \ 00064 if (obj) obj->DecRef(); 00065 #define CSREF_TRACK_ASSIGN(obj,tag) 00066 #endif 00067 00078 template <class T> 00079 class csPtr 00080 { 00081 private: 00082 friend class csRef<T>; 00083 T* obj; 00084 00085 public: 00086 csPtr (T* p) : obj (p) { CSREF_TRACK_ASSIGN(obj, this); } 00087 00088 template <class T2> 00089 explicit csPtr (csRef<T2> const& r) : obj((T2*)r) 00090 { 00091 CSREF_TRACK_INCREF (obj, this); 00092 } 00093 00094 #ifdef CS_TEST_VOIDPTRUSAGE 00095 ~csPtr () 00096 { 00097 // If not assigned to a csRef we have a problem (leak). 00098 // So if this assert fires for you, then you are calling 00099 // a function that returns a csPtr and not using the result 00100 // (or at least not assigning it to a csRef). This is a memory 00101 // leak and you should fix that. 00102 CS_ASSERT_MSG ("csPtr<> was not assigned to a csRef<> prior destruction", 00103 obj == (T*)CS_VOIDED_PTR); 00104 } 00105 #endif 00106 00107 csPtr (const csPtr<T>& copy) 00108 { 00109 obj = copy.obj; 00110 #ifdef CS_TEST_VOIDPTRUSAGE 00111 ((csPtr<T>&)copy).obj = (T*)CS_VOIDED_PTR; 00112 #endif 00113 } 00114 }; 00115 00122 template <class T> 00123 class csRef 00124 { 00125 private: 00126 T* obj; 00127 00128 public: 00134 csRef () : obj (0) {} 00135 00141 csRef (const csPtr<T>& newobj) 00142 { 00143 obj = newobj.obj; 00144 # ifdef CS_TEST_VOIDPTRUSAGE 00145 CS_ASSERT_MSG ("csPtr<> was already assigned to a csRef<>", 00146 newobj.obj != (T*)CS_VOIDED_PTR); 00147 # endif 00148 // The following line is outside the ifdef to make sure 00149 // we have binary compatibility. 00150 ((csPtr<T>&)newobj).obj = (T*)CS_VOIDED_PTR; 00151 } 00152 00157 csRef (T* newobj) : obj (newobj) 00158 { 00159 CSREF_TRACK_INCREF (obj, this); 00160 } 00161 00165 template <class T2> 00166 csRef (csRef<T2> const& other) : obj ((T2*)other) 00167 { 00168 CSREF_TRACK_INCREF (obj, this); 00169 } 00170 00174 csRef (csRef const& other) : obj (other.obj) 00175 { 00176 CSREF_TRACK_INCREF (obj, this); 00177 } 00178 00182 ~csRef () 00183 { 00184 CSREF_TRACK_DECREF (obj, this); 00185 } 00186 00196 csRef& operator = (const csPtr<T>& newobj) 00197 { 00198 T* oldobj = obj; 00199 // First assign and then DecRef() of old object! 00200 obj = newobj.obj; 00201 # ifdef CS_TEST_VOIDPTRUSAGE 00202 CS_ASSERT_MSG ("csPtr<> was already assigned to a csRef<>", 00203 newobj.obj != (T*)CS_VOIDED_PTR); 00204 # endif 00205 // The following line is outside the ifdef to make sure 00206 // we have binary compatibility. 00207 ((csPtr<T>&)newobj).obj = (T*)CS_VOIDED_PTR; 00208 CSREF_TRACK_DECREF (oldobj, this); 00209 return *this; 00210 } 00211 00224 csRef& operator = (T* newobj) 00225 { 00226 if (obj != newobj) 00227 { 00228 T* oldobj = obj; 00229 // It is very important to first assign the new value to 00230 // 'obj' BEFORE calling DecRef() on the old object. Otherwise 00231 // it is easy to get in infinite loops with objects being 00232 // destructed forever (when ref=0 is used for example). 00233 obj = newobj; 00234 CSREF_TRACK_INCREF (newobj, this); 00235 CSREF_TRACK_DECREF (oldobj, this); 00236 } 00237 return *this; 00238 } 00239 00262 void AttachNew (csPtr<T> newObj) 00263 { 00264 // Note: The parameter usage of csPtr<T> instead of csPtr<T>& is 00265 // deliberate and not to be considered a bug. 00266 00267 // Just Re-use csPtr assignment logic 00268 *this = newObj; 00269 } 00270 00272 template <class T2> 00273 csRef& operator = (csRef<T2> const& other) 00274 { 00275 T* p = (T2*)other; 00276 this->operator=(p); 00277 return *this; 00278 } 00279 00281 csRef& operator = (csRef const& other) 00282 { 00283 this->operator=(other.obj); 00284 return *this; 00285 } 00286 00288 inline friend bool operator == (const csRef& r1, const csRef& r2) 00289 { 00290 return r1.obj == r2.obj; 00291 } 00293 inline friend bool operator != (const csRef& r1, const csRef& r2) 00294 { 00295 return r1.obj != r2.obj; 00296 } 00298 inline friend bool operator == (const csRef& r1, T* obj) 00299 { 00300 return r1.obj == obj; 00301 } 00303 inline friend bool operator != (const csRef& r1, T* obj) 00304 { 00305 return r1.obj != obj; 00306 } 00308 inline friend bool operator == (T* obj, const csRef& r1) 00309 { 00310 return r1.obj == obj; 00311 } 00313 inline friend bool operator != (T* obj, const csRef& r1) 00314 { 00315 return r1.obj != obj; 00316 } 00322 inline friend bool operator < (const csRef& r1, const csRef& r2) 00323 { 00324 return r1.obj < r2.obj; 00325 } 00326 00327 00329 T* operator -> () const 00330 { return obj; } 00331 00333 operator T* () const 00334 { return obj; } 00335 00337 T& operator* () const 00338 { return *obj; } 00339 00344 bool IsValid () const 00345 { return (obj != 0); } 00346 00348 void Invalidate() 00349 { *this = (T*)0; } 00350 00352 uint GetHash() const 00353 { return (uintptr_t)obj; } 00354 }; 00355 00356 #undef CSREF_TRACK_INCREF 00357 #undef CSREF_TRACK_DECREF 00358 #undef CSREF_TRACK_ASSIGN 00359 00360 #endif // __CS_REF_H__
Generated for Crystal Space by doxygen 1.4.4