nux-0.9.48
|
00001 /* 00002 * Copyright 2010 Inalogic® Inc. 00003 * 00004 * This program is free software: you can redistribute it and/or modify it 00005 * under the terms of the GNU Lesser General Public License, as 00006 * published by the Free Software Foundation; either version 2.1 or 3.0 00007 * of the License. 00008 * 00009 * This program is distributed in the hope that it will be useful, but 00010 * WITHOUT ANY WARRANTY; without even the implied warranties of 00011 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR 00012 * PURPOSE. See the applicable version of the GNU Lesser General Public 00013 * License for more details. 00014 * 00015 * You should have received a copy of both the GNU Lesser General Public 00016 * License along with this program. If not, see <http://www.gnu.org/licenses/> 00017 * 00018 * Authored by: Jay Taoko <jaytaoko@inalogic.com> 00019 * 00020 */ 00021 00022 00023 #ifndef INTRUSIVESP_H 00024 #define INTRUSIVESP_H 00025 00026 namespace nux 00027 { 00028 00029 // forward definitions 00030 00031 template <typename T> 00032 class ObjectWeakPtr; 00033 00034 template <typename T> 00035 class ObjectPtr; 00036 00037 #define IntrusiveSP ObjectPtr 00038 #define IntrusiveWeakSP ObjectWeakPtr 00039 00041 template <typename T> 00042 class ObjectPtr 00043 { 00044 public: 00046 ObjectPtr() 00047 : ptr_ (0) 00048 { 00049 _reference_count = 0; 00050 _weak_reference_count = 0; 00051 _objectptr_count = 0; 00052 _destroyed = 0; 00053 } 00054 00056 ObjectPtr (const ObjectPtr<T>& other) 00057 : ptr_ (0) 00058 { 00059 ptr_ = other.ptr_; 00060 00061 if (ptr_ != 0) 00062 { 00063 _reference_count = other->_reference_count; 00064 _weak_reference_count = other->_weak_reference_count; 00065 _objectptr_count = other->_objectptr_count; 00066 _objectptr_count->Increment (); 00067 _destroyed = other->_destroyed; 00068 ptr_->Reference(); 00069 } 00070 } 00071 00073 00076 template <typename O> 00077 ObjectPtr (const ObjectPtr<O>& other) 00078 : ptr_ (0) 00079 { 00080 _reference_count = 0; 00081 _weak_reference_count = 0; 00082 _objectptr_count = 0; 00083 _destroyed = 0; 00084 00085 if (other.ptr_ && other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) ) 00086 { 00087 ptr_ = (T *) other.ptr_; 00088 _reference_count = other->_reference_count; 00089 _weak_reference_count = other->_weak_reference_count; 00090 _objectptr_count = other->_objectptr_count; 00091 _objectptr_count->Increment (); 00092 _destroyed = other->_destroyed; 00093 ptr_->Reference(); 00094 } 00095 } 00096 00098 00104 explicit ObjectPtr (T *ptr, bool WarnMissuse = false) 00105 : ptr_ (0) 00106 { 00107 _reference_count = 0; 00108 _weak_reference_count = 0; 00109 _objectptr_count = 0; 00110 _destroyed = 0; 00111 00112 if (ptr != 0) 00113 { 00114 if (WarnMissuse && (ptr->OwnsTheReference() == false) ) 00115 { 00116 nuxDebugMsg (TEXT ("[ObjectPtr::ObjectPtr] Warning: Constructing a smart pointer from an object with a floating reference.") ); 00117 } 00118 00119 ptr_ = ptr; 00120 _reference_count = ptr->_reference_count; 00121 _weak_reference_count = ptr->_weak_reference_count; 00122 _objectptr_count = ptr->_objectptr_count; 00123 _objectptr_count->Increment (); 00124 _destroyed = ptr->_destroyed; 00125 ptr_->Reference(); 00126 } 00127 } 00128 00130 00136 template <typename O> 00137 explicit ObjectPtr (O *ptr, bool WarnMissuse = false) 00138 : ptr_ (0) 00139 { 00140 _reference_count = 0; 00141 _weak_reference_count = 0; 00142 _objectptr_count = 0; 00143 _destroyed = 0; 00144 00145 if (ptr != 0) 00146 { 00147 if (ptr->Type().IsDerivedFromType (T::StaticObjectType) ) 00148 { 00149 if (WarnMissuse && (ptr->OwnsTheReference() == false) ) 00150 { 00151 nuxDebugMsg (TEXT ("[ObjectPtr::ObjectPtr] Warning: Constructing a smart pointer from an object with a floating reference.") ); 00152 } 00153 00154 ptr_ = (T *) ptr; 00155 _reference_count = ptr->_reference_count; 00156 _weak_reference_count = ptr->_weak_reference_count; 00157 _objectptr_count = ptr->_objectptr_count; 00158 _objectptr_count->Increment (); 00159 _destroyed = ptr->_destroyed; 00160 ptr_->Reference(); 00161 } 00162 } 00163 } 00164 00166 00169 ObjectPtr &operator = (const ObjectPtr<T>& other) 00170 { 00171 if (ptr_ != other.ptr_) 00172 { 00173 ReleaseReference (); 00174 00175 ptr_ = other.ptr_; 00176 00177 if (ptr_ != 0) 00178 { 00179 _reference_count = other->_reference_count; 00180 _weak_reference_count = other->_weak_reference_count; 00181 _objectptr_count = other->_objectptr_count; 00182 _objectptr_count->Increment (); 00183 _destroyed = other->_destroyed; 00184 ptr_->Reference(); 00185 } 00186 } 00187 00188 return *this; 00189 } 00190 00192 00195 template <typename O> 00196 ObjectPtr &operator = (const ObjectPtr<O>& other) 00197 { 00198 if (other.ptr_ && other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) ) 00199 { 00200 if (ptr_ != other.ptr_) 00201 { 00202 ReleaseReference (); 00203 00204 ptr_ = other.ptr_; 00205 00206 if (ptr_ != 0) 00207 { 00208 _reference_count = other->_reference_count; 00209 _weak_reference_count = other->_weak_reference_count; 00210 _objectptr_count = other->_objectptr_count; 00211 _objectptr_count->Increment (); 00212 _destroyed = other->_destroyed; 00213 ptr_->Reference(); 00214 } 00215 } 00216 } 00217 else 00218 { 00219 ReleaseReference (); 00220 } 00221 00222 return *this; 00223 } 00224 00225 // WARNING: THIS FUNCTION IS MADE PUBLIC TEMPORARILY TO FACILITATE TRANSITION TO A PURE POINTER BASE USAGE IN NUX. 00226 // THIS FUNCTION WILL BE REMOVED! 00227 T *GetSPPointer() const 00228 { 00229 return ptr_; 00230 } 00231 00232 ~ObjectPtr () 00233 { 00234 ReleaseReference (); 00235 } 00236 00237 T &operator * () const 00238 { 00239 nuxAssert (ptr_ != 0); 00240 return *ptr_; 00241 } 00242 00243 T *operator -> () const 00244 { 00245 nuxAssert (ptr_ != 0); 00246 return ptr_; 00247 } 00248 00249 // //! Swap the content of 2 smart pointers. 00250 // /*! 00251 // @param other Smart pointer to swap with. 00252 // */ 00253 // void Swap (ObjectPtr<T>& other) 00254 // { 00255 // std::swap (ptr_, other.ptr_); 00256 // std::swap (refCounts_, other.refCounts_); 00257 // } 00258 00260 00263 bool operator () () const 00264 { 00265 return ptr_ != 0; 00266 } 00267 00269 00272 bool IsNull() const 00273 { 00274 if (ptr_ == 0) 00275 return true; 00276 00277 return false; 00278 } 00279 00281 00284 bool IsValid() const 00285 { 00286 if (ptr_ != 0) 00287 return true; 00288 00289 return false; 00290 } 00291 00292 bool operator < (T *ptr) const 00293 { 00294 return (ptr_ < ptr); 00295 } 00296 00297 bool operator > (T *ptr) const 00298 { 00299 return (ptr_ > ptr); 00300 } 00301 00302 bool operator < (ObjectPtr<T> other) const 00303 { 00304 return (ptr_ < other.ptr_); 00305 } 00306 00307 bool operator > (ObjectPtr<T> other) const 00308 { 00309 return (ptr_ > other.ptr_); 00310 } 00311 00312 bool operator != (T *ptr) const 00313 { 00314 return ( (void *) ptr_ != (void *) ptr); 00315 } 00316 00317 bool operator == (T *ptr) const 00318 { 00319 return ( (void *) ptr_ == (void *) ptr); 00320 } 00321 00322 template<typename U> 00323 bool operator != (U *ptr) const 00324 { 00325 if (ptr && (!ptr->Type().IsDerivedFromType (T::StaticObjectType) ) ) 00326 return true; 00327 00328 return ( (void *) ptr_ != (void *) ptr); 00329 } 00330 00331 template<typename U> 00332 bool operator == (U *ptr) const 00333 { 00334 if (ptr && (!ptr->Type().IsDerivedFromType (T::StaticObjectType) ) ) 00335 return false; 00336 00337 return ( (void *) ptr_ == (void *) ptr); 00338 } 00339 00340 template<typename U> 00341 bool operator != (const ObjectPtr<U>& other) const 00342 { 00343 if (other.ptr_ && (!other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) ) ) 00344 return true; 00345 00346 return (void *) ptr_ != (void *) other.ptr_; 00347 } 00348 00349 template<typename U> 00350 bool operator == (const ObjectPtr<U>& other) const 00351 { 00352 if (other.ptr_ && (!other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) ) ) 00353 return false; 00354 00355 return (void *) ptr_ == (void *) other.ptr_; 00356 } 00357 00358 template<typename U> 00359 bool operator != (const ObjectWeakPtr<U>& other) const 00360 { 00361 if (other.ptr_ && (!other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) ) ) 00362 return true; 00363 00364 return (void *) ptr_ != (void *) other.ptr_; 00365 } 00366 00367 template<typename U> 00368 bool operator == (const ObjectWeakPtr<U>& other) const 00369 { 00370 if (other.ptr_ && (!other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) ) ) 00371 return false; 00372 00373 return (void *) ptr_ == (void *) other.ptr_; 00374 } 00375 00377 00386 bool Release() 00387 { 00388 return ReleaseReference (); 00389 } 00390 00391 private: 00392 00393 bool ReleaseReference() 00394 { 00395 if (ptr_ == 0) 00396 { 00397 return false; 00398 } 00399 00400 // Decrease the number of strong reference on the hosted pointer. 00401 if(_objectptr_count) 00402 _objectptr_count->Decrement (); 00403 00404 bool delete_warning = (_reference_count->GetValue() == 1) && (_weak_reference_count->GetValue() == 1); 00405 00406 // If delete_warning == true, then we know that the cal to Unref will delete the pointer ptr. Also, the pointer to 00407 // _reference_count and _weak_reference_count will be deleted. 00408 if (delete_warning) 00409 { 00410 nuxAssert (_objectptr_count->GetValue () == 0); 00411 } 00412 00413 bool destroyed = ptr_->UnReference(); 00414 // if destroyed is true and delete_warning is false, it means that the pointer was destroyed. But to to hanging Weak pointers, 00415 // _weak_reference_count and _weak_reference_count have not been deleted. 00416 00417 if (delete_warning) 00418 { 00419 nuxAssert (destroyed); 00420 } 00421 00422 if (destroyed && delete_warning) 00423 { 00424 // Everything has been destroyed and there are no hanging weak pointers. Reset the pointers for this object. 00425 _reference_count = 0; 00426 _weak_reference_count = 0; 00427 _objectptr_count = 0; 00428 _destroyed = 0; 00429 ptr_ = 0; 00430 } 00431 else if (destroyed && (delete_warning == false) ) 00432 { 00433 // Only the ptr_ has been destroyed.There are still hanging weak pointers. Reset the pointers for this object. 00434 nuxAssert (_reference_count->GetValue() == 0); 00435 nuxAssert (_objectptr_count->GetValue () == 0); 00436 nuxAssert (_weak_reference_count->GetValue() >= 1); 00437 _reference_count = 0; 00438 _weak_reference_count = 0; 00439 _objectptr_count = 0; 00440 _destroyed = 0; 00441 ptr_ = 0; 00442 } 00443 else 00444 { 00445 // There are still pending references to ptr_. Reset the pointers for this object. 00446 // Notice how we do the same thing here as in the previous conditions. We end up setting 00447 // _reference_count, _weak_reference_count and ptr_ to 0. 00448 _reference_count = 0; 00449 _weak_reference_count = 0; 00450 _objectptr_count = 0; 00451 _destroyed = 0; 00452 ptr_ = 0; 00453 } 00454 00455 return destroyed; 00456 } 00457 00458 T *ptr_; 00459 NThreadSafeCounter *_reference_count; 00460 NThreadSafeCounter *_weak_reference_count; 00461 NThreadSafeCounter *_objectptr_count; 00462 bool *_destroyed; 00463 00464 template <typename U> 00465 friend ObjectPtr<U> Create (); 00466 00467 template <typename U, typename P1> 00468 friend ObjectPtr<U> Create (P1 p1); 00469 00470 template <typename U, typename P1, typename P2> 00471 friend ObjectPtr<U> Create (P1 p1, P2 p2); 00472 00473 template <typename U, typename P1, typename P2, typename P3> 00474 friend ObjectPtr<U> Create (P1 p1, P2 p2, P3 p3); 00475 00476 template <typename U, typename P1, typename P2, typename P3, typename P4> 00477 friend ObjectPtr<U> Create (P1 p1, P2 p2, P3 p3, P4 p4); 00478 00479 template <typename U, typename P1, typename P2, typename P3, typename P4, typename P5> 00480 friend ObjectPtr<U> Create (P1 p1, P2 p2, P3 p3, P4 p4, P5 p5); 00481 00482 template <typename U, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6> 00483 friend ObjectPtr<U> Create (P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6); 00484 00485 template <typename U, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7> 00486 friend ObjectPtr<U> Create (P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7); 00487 00488 template <typename U> 00489 friend ObjectPtr<U> WrapWSPtr (U *u); 00490 00491 template <typename O> 00492 friend class ObjectPtr; 00493 00494 template <typename O> 00495 friend class ObjectWeakPtr; 00496 00497 // template<typename T, typename U> 00498 // friend bool operator == (const ObjectPtr<T>& a, const ObjectPtr<U>& b); 00499 00500 // template<typename T, typename U> 00501 // friend bool operator != (const ObjectPtr<T>& a, const ObjectPtr<U>& b); 00502 00503 // template<typename T> 00504 // friend bool operator == (const ObjectPtr<T>& a, T*); 00505 00506 template<typename U> 00507 friend bool operator == (U *, const ObjectPtr<U>& a); 00508 00509 // template<typename T> 00510 // friend bool operator != (const ObjectPtr<T>& a, T*); 00511 00512 template<typename U> 00513 friend bool operator != (U *, const ObjectPtr<U>& a); 00514 00515 // template<typename T, typename U> 00516 // friend bool operator == (const ObjectPtr<T>& a, const ObjectWeakPtr<U>& b); 00517 00518 // template<typename T, typename U> 00519 // friend bool operator == (const ObjectWeakPtr<T>& a, const ObjectPtr<U>& b); 00520 // 00521 // template<typename T, typename U> 00522 // friend bool operator != (const ObjectPtr<T>& a, const ObjectWeakPtr<U>& b); 00523 // 00524 // template<typename T, typename U> 00525 // friend bool operator != (const ObjectWeakPtr<T>& a, const ObjectPtr<U>& b); 00526 00527 template <typename U, typename F> 00528 friend ObjectPtr<U> staticCast (const ObjectPtr<F>& from); 00529 00530 template <typename U, typename F> 00531 friend ObjectPtr<U> constCast (const ObjectPtr<F>& from); 00532 00533 template <typename U, typename F> 00534 friend ObjectPtr<U> dynamicCast (const ObjectPtr<F>& from); 00535 00536 template <typename U, typename F> 00537 friend ObjectPtr<U> checkedCast (const ObjectPtr<F>& from); 00538 00539 template <typename U, typename F> 00540 friend ObjectPtr<U> queryCast (const ObjectPtr<F>& from); 00541 }; 00542 00543 00545 00550 template <typename T> 00551 class ObjectWeakPtr 00552 { 00553 public: 00555 ObjectWeakPtr () 00556 : ptr_ (0) 00557 { 00558 _reference_count = 0; 00559 _weak_reference_count = 0; 00560 _destroyed = 0; 00561 } 00562 00564 00570 explicit ObjectWeakPtr (T *ptr, bool WarnMissuse = false) 00571 : ptr_ (0) 00572 { 00573 _reference_count = 0; 00574 _weak_reference_count = 0; 00575 _destroyed = 0; 00576 00577 if (ptr != 0) 00578 { 00579 if (WarnMissuse && (ptr->OwnsTheReference () == false)) 00580 { 00581 //nuxDebugMsg (TEXT ("[ObjectWeakPtr::ObjectWeakPtr] Warning: Constructing a weak smart pointer from an object with a floating reference.") ); 00582 } 00583 00584 ptr_ = ptr; 00585 _reference_count = ptr->_reference_count; 00586 _weak_reference_count = ptr->_weak_reference_count; 00587 _destroyed = ptr->_destroyed; 00588 ptr_->IncrementWeakCounter (); 00589 } 00590 } 00591 00593 00599 template <typename O> 00600 explicit ObjectWeakPtr (O *ptr, bool WarnMissuse = false) 00601 : ptr_ (0) 00602 { 00603 _reference_count = 0; 00604 _weak_reference_count = 0; 00605 _destroyed = 0; 00606 00607 if (ptr != 0) 00608 { 00609 if (ptr->Type ().IsDerivedFromType (T::StaticObjectType)) 00610 { 00611 if (WarnMissuse && (ptr->OwnsTheReference () == false)) 00612 { 00613 //nuxDebugMsg (TEXT ("[ObjectWeakPtr::ObjectWeakPtr] Warning: Constructing a weak smart pointer from an object with a floating reference.") ); 00614 } 00615 00616 ptr_ = (T *) ptr; 00617 _reference_count = ptr->_reference_count; 00618 _weak_reference_count = ptr->_weak_reference_count; 00619 _destroyed = ptr->_destroyed; 00620 ptr_->IncrementWeakCounter (); 00621 } 00622 } 00623 } 00624 00626 00629 ObjectWeakPtr (const ObjectWeakPtr<T> &other) 00630 { 00631 ptr_ = other.ptr_; 00632 _reference_count = other._reference_count; 00633 _weak_reference_count = other._weak_reference_count; 00634 _destroyed = other._destroyed; 00635 00636 // Warning: We cannot count on ptr_ to be valid. 00637 // If _weak_reference_count is not null, then imcrement it. 00638 if (_weak_reference_count) 00639 { 00640 _weak_reference_count->Increment (); 00641 } 00642 else 00643 { 00644 // Sanity checks 00645 nuxAssert (_reference_count == 0); 00646 nuxAssert (_weak_reference_count == 0); 00647 nuxAssert (_destroyed == 0); 00648 } 00649 } 00650 00651 // Warning: We are not sure that other.ptr_ is valid. 00652 // Warning: Cannot call other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) 00654 00657 template <typename O> 00658 ObjectWeakPtr (const ObjectWeakPtr<O>& other) 00659 : ptr_ (0) 00660 { 00661 _reference_count = 0; 00662 _weak_reference_count = 0; 00663 _destroyed = 0; 00664 00665 if (other.ptr_ && other.ptr_->Type ().IsDerivedFromType (T::StaticObjectType)) 00666 { 00667 ptr_ = other.ptr_; 00668 _reference_count = other._reference_count; 00669 _weak_reference_count = other._weak_reference_count; 00670 _destroyed = other._destroyed; 00671 00672 // Warning: We cannot count on ptr_ to be valid. 00673 // If _weak_reference_count is not null, then imcrement it. 00674 if (_weak_reference_count) 00675 { 00676 _weak_reference_count->Increment (); 00677 } 00678 else 00679 { 00680 // Sanity checks 00681 nuxAssert (_reference_count == 0); 00682 nuxAssert (_weak_reference_count == 0); 00683 nuxAssert (_destroyed == 0); 00684 } 00685 } 00686 } 00687 00689 00692 template <typename O> 00693 ObjectWeakPtr (const ObjectPtr<O> &other) 00694 : ptr_ (0) 00695 { 00696 _reference_count = 0; 00697 _weak_reference_count = 0; 00698 _destroyed = 0; 00699 00700 if (other.ptr_ && other.ptr_->Type ().IsDerivedFromType (T::StaticObjectType)) 00701 { 00702 ptr_ = other.ptr_; 00703 _reference_count = other._reference_count; 00704 _weak_reference_count = other._weak_reference_count; 00705 _destroyed = other._destroyed; 00706 00707 if (ptr_ != 0) 00708 { 00709 _weak_reference_count->Increment (); 00710 } 00711 else 00712 { 00713 // Sanity checks 00714 nuxAssert (_reference_count == 0); 00715 nuxAssert (_weak_reference_count == 0); 00716 nuxAssert (_destroyed == 0); 00717 } 00718 } 00719 } 00720 00722 00725 ObjectWeakPtr& operator = (const ObjectWeakPtr<T> &other) 00726 { 00727 if (GetPointer () != other.GetPointer () ) // Avoid self assignment. 00728 { 00729 ReleaseReference (); 00730 00731 ptr_ = other.ptr_; 00732 _reference_count = other._reference_count; 00733 _weak_reference_count = other._weak_reference_count; 00734 _destroyed = other._destroyed; 00735 00736 // Warning: We cannot count on ptr_ to be valid. 00737 // If _weak_reference_count is not null, then imcrement it. 00738 if (_weak_reference_count) 00739 { 00740 _weak_reference_count->Increment (); 00741 } 00742 else 00743 { 00744 // Sanity checks 00745 nuxAssert (_reference_count == 0); 00746 nuxAssert (_weak_reference_count == 0); 00747 nuxAssert (_destroyed == 0); 00748 } 00749 } 00750 00751 return *this; 00752 } 00753 00754 // Warning: We are not sure that other.ptr_ is valid. 00755 // Warning: Cannot call other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) 00757 00760 template <typename O> 00761 ObjectWeakPtr &operator = (const ObjectWeakPtr<O>& other) 00762 { 00763 if (other.ptr_ && other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) ) 00764 { 00765 if (GetPointer () != other.GetPointer () ) // Avoid self assignment. 00766 { 00767 ReleaseReference (); 00768 00769 ptr_ = other.ptr_; 00770 //refCounts_ = other.refCounts_; 00771 _reference_count = other._reference_count; 00772 _weak_reference_count = other._weak_reference_count; 00773 _destroyed = other._destroyed; 00774 00775 // Warning: We cannot count on ptr_ to be valid. 00776 // If _weak_reference_count is not null, then imcrement it. 00777 if (_weak_reference_count) 00778 { 00779 _weak_reference_count->Increment (); 00780 } 00781 else 00782 { 00783 // Sanity checks 00784 nuxAssert (_reference_count == 0); 00785 nuxAssert (_weak_reference_count == 0); 00786 nuxAssert (_destroyed == 0); 00787 } 00788 } 00789 } 00790 else 00791 { 00792 ReleaseReference(); 00793 } 00794 00795 return *this; 00796 } 00797 00799 00802 /*ObjectWeakPtr& operator = (const ObjectPtr<T> &other) 00803 { 00804 if (GetPointer () != other.ptr_) // Avoid self assignment. 00805 { 00806 ReleaseReference (); 00807 00808 ptr_ = other.ptr_; 00809 //refCounts_ = other.refCounts_; 00810 _reference_count = other._reference_count; 00811 _weak_reference_count = other._weak_reference_count; 00812 _destroyed = other._destroyed; 00813 00814 if (ptr_ != 0) 00815 { 00816 _weak_reference_count->Increment (); 00817 } 00818 } 00819 else 00820 { 00821 ReleaseReference (); 00822 } 00823 00824 return *this; 00825 }*/ 00826 00828 00831 template <typename O> 00832 ObjectWeakPtr &operator = (const ObjectPtr<O>& other) 00833 { 00834 if (other.ptr_ && other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) ) 00835 { 00836 if (GetPointer () != other.ptr_) // Avoid self assignment. 00837 { 00838 ReleaseReference (); 00839 00840 ptr_ = other.ptr_; 00841 //refCounts_ = other.refCounts_; 00842 _reference_count = other._reference_count; 00843 _weak_reference_count = other._weak_reference_count; 00844 _destroyed = other._destroyed; 00845 00846 if (ptr_ != 0) 00847 { 00848 //refCounts_->totalRefs_.Increment(); 00849 _weak_reference_count->Increment(); 00850 } 00851 } 00852 } 00853 else 00854 { 00855 ReleaseReference(); 00856 } 00857 00858 return *this; 00859 } 00860 00862 00868 ObjectWeakPtr& operator = (T *ptr) 00869 { 00870 if (ptr_ && _reference_count && (_reference_count->GetValue() != 0) ) 00871 { 00872 ptr_->DecrementWeakCounter(); 00873 } 00874 else if (_reference_count && _weak_reference_count) 00875 { 00876 _weak_reference_count->Decrement(); 00877 } 00878 00879 if (_reference_count && _weak_reference_count && (_reference_count->GetValue() == 0) && (_weak_reference_count->GetValue() == 0) ) 00880 { 00881 if (!(*_destroyed)) 00882 { 00883 // The object is between Object::Destroy() and Object::~Object() 00884 ptr_->_reference_count = 0; 00885 ptr_->_weak_reference_count = 0; 00886 } 00887 else 00888 { 00889 // The object has been destroyed and this is the last weak reference to it. 00890 delete _destroyed; 00891 } 00892 delete _reference_count; 00893 delete _weak_reference_count; 00894 00895 } 00896 00897 ptr_ = 0; 00898 _reference_count = 0; 00899 _weak_reference_count = 0; 00900 _destroyed = 0; 00901 00902 if (ptr != 0) 00903 { 00904 if (ptr->OwnsTheReference() == false) 00905 { 00906 //nuxDebugMsg (TEXT ("[ObjectWeakPtr::operator = ()] Warning: Constructing a weak smart pointer from an object with a floating reference.") ); 00907 } 00908 00909 ptr_ = ptr; 00910 _reference_count = ptr->_reference_count; 00911 _weak_reference_count = ptr->_weak_reference_count; 00912 _destroyed = ptr->_destroyed; 00913 ptr_->IncrementWeakCounter(); 00914 } 00915 00916 return *this; 00917 } 00918 00920 00926 template <typename O> 00927 ObjectWeakPtr &operator = (O *ptr) 00928 { 00929 if (ptr_ && _reference_count && (_reference_count->GetValue() != 0) ) 00930 { 00931 ptr_->DecrementWeakCounter(); 00932 } 00933 else if (_reference_count && _weak_reference_count) 00934 { 00935 _weak_reference_count->Decrement(); 00936 } 00937 else 00938 { 00939 nuxAssertMsg (0, TEXT ("Could there be something wrong her?") ); 00940 } 00941 00942 if (_reference_count && _weak_reference_count && (_reference_count->GetValue() == 0) && (_weak_reference_count->GetValue() == 0) ) 00943 { 00944 if (!(*_destroyed)) 00945 { 00946 // The object is between Object::Destroy() and Object::~Object() 00947 ptr_->_reference_count = 0; 00948 ptr_->_weak_reference_count = 0; 00949 } 00950 else 00951 { 00952 // The object has been destroyed and this is the last weak reference to it. 00953 delete _destroyed; 00954 } 00955 delete _reference_count; 00956 delete _weak_reference_count; 00957 } 00958 00959 ptr_ = 0; 00960 _reference_count = 0; 00961 _weak_reference_count = 0; 00962 _destroyed = 0; 00963 00964 if (ptr != 0) 00965 { 00966 if (ptr->Type().IsDerivedFromType (T::StaticObjectType) ) 00967 { 00968 if (ptr->OwnsTheReference() == false) 00969 { 00970 //nuxDebugMsg (TEXT ("[ObjectWeakPtr::operator = ()] Warning: Constructing a weak smart pointer from an object with a floating reference.") ); 00971 } 00972 00973 ptr_ = (T *) ptr; 00974 _reference_count = ptr->_reference_count; 00975 _weak_reference_count = ptr->_weak_reference_count; 00976 _destroyed = ptr->_destroyed; 00977 ptr_->IncrementWeakCounter(); 00978 } 00979 } 00980 } 00981 00982 ~ObjectWeakPtr () 00983 { 00984 ReleaseReference (); 00985 } 00986 00987 T &operator * () const 00988 { 00989 nuxAssert (_reference_count && (_reference_count->GetValue() != 0) && (ptr_ != 0) ); 00990 00991 return *(NUX_CONST_CAST (T*, GetPointer ())); 00992 } 00993 00994 T *operator -> () const 00995 { 00996 nuxAssert (_reference_count && (_reference_count->GetValue() != 0) && (ptr_ != 0) ); 00997 00998 return NUX_CONST_CAST (T*, GetPointer ()); 00999 } 01000 01001 // void Swap (ObjectWeakPtr<T>& other) 01002 // { 01003 // std::swap (ptr_, other.ptr_); 01004 // std::swap (refCounts_, other.refCounts_); 01005 // } 01006 01007 bool operator < (T *ptr) const 01008 { 01009 return (ptr_ < ptr); 01010 } 01011 01012 bool operator > (T *ptr) const 01013 { 01014 return (ptr_ > ptr); 01015 } 01016 01017 bool operator < (ObjectWeakPtr<T> other) const 01018 { 01019 return (ptr_ < other.ptr_); 01020 } 01021 01022 bool operator > (ObjectWeakPtr<T> other) const 01023 { 01024 return (ptr_ > other.ptr_); 01025 } 01026 01027 bool operator != (T *ptr) const 01028 { 01029 return ( (void *) ptr_ != (void *) ptr); 01030 } 01031 01032 bool operator == (T *ptr) const 01033 { 01034 return ( (void *) ptr_ == (void *) ptr); 01035 } 01036 01037 template<typename U> 01038 bool operator != (U *ptr) const 01039 { 01040 if (ptr && (!ptr->Type().IsDerivedFromType (T::StaticObjectType) ) ) 01041 return true; 01042 01043 return ( (void *) ptr_ != (void *) ptr); 01044 } 01045 01046 template<typename U> 01047 bool operator == (U *ptr) const 01048 { 01049 if (ptr && (!ptr->Type().IsDerivedFromType (T::StaticObjectType) ) ) 01050 return false; 01051 01052 return ( (void *) ptr_ == (void *) ptr); 01053 } 01054 01059 template<typename U> 01060 bool operator != (const ObjectWeakPtr<U>& other) const 01061 { 01062 // Test if the object in other has been destroyed. If yes, then we can't call other.ptr_->Type(). 01063 if (other._reference_count->GetValue () != 0) 01064 { 01065 if (other.ptr_ && (!other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) ) ) 01066 return true; 01067 } 01068 01069 return ( (void *) ptr_ != (void *) other.ptr_); 01070 } 01071 01076 template<typename U> 01077 bool operator == (const ObjectWeakPtr<U>& other) const 01078 { 01079 // Test if the object in other has been destroyed. If yes, then we can't call other.ptr_->Type(). 01080 if (other._reference_count->GetValue () != 0) 01081 { 01082 if (other.ptr_ && (!other.ptr_->Type().IsDerivedFromType (T::StaticObjectType))) 01083 return false; 01084 } 01085 01086 return ( (void *) ptr_ == (void *) other.ptr_); 01087 } 01088 01093 template<typename U> 01094 bool operator != (const ObjectPtr<U>& other) const 01095 { 01096 if (other.ptr_ && (!other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) ) ) 01097 return true; 01098 01099 return ( (void *) ptr_ != (void *) other.ptr_); 01100 } 01101 01106 template<typename U> 01107 bool operator == (const ObjectPtr<U>& other) const 01108 { 01109 if (other.ptr_ && (!other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) ) ) 01110 return false; 01111 01112 return ( (void *) ptr_ == (void *) other.ptr_); 01113 } 01114 01116 01119 bool operator () () const 01120 { 01121 return GetPointer() != 0; 01122 } 01123 01125 01128 bool IsValid() const 01129 { 01130 return GetPointer() != 0; 01131 } 01132 01134 01137 bool IsNull() const 01138 { 01139 return GetPointer() == 0; 01140 } 01141 01143 01152 bool Release() 01153 { 01154 return ReleaseReference (); 01155 } 01156 01158 01162 const T* GetPointer () const 01163 { 01164 if ((_weak_reference_count == 0) || (_weak_reference_count->GetValue () == 0)) 01165 { 01166 return 0; 01167 } 01168 01169 if ((_reference_count == 0) || (_reference_count->GetValue () == 0)) 01170 { 01171 return 0; 01172 } 01173 01174 return ptr_; 01175 } 01176 01178 01182 T *GetPointer() 01183 { 01184 return NUX_CONST_CAST (T*, (const_cast< const ObjectWeakPtr* > (this))->GetPointer ()); 01185 } 01186 01187 int GetReferenceCount () 01188 { 01189 if (_reference_count == 0) 01190 { 01191 return 0; 01192 } 01193 01194 return _reference_count()->GetValue (); 01195 } 01196 01197 int GetWeakReferenceCount () 01198 { 01199 if (_weak_reference_count == 0) 01200 { 01201 return 0; 01202 } 01203 01204 return _weak_reference_count->GetValue (); 01205 } 01206 01207 private: 01208 bool ReleaseReference () 01209 { 01210 if (ptr_ == 0) 01211 { 01212 return false; 01213 } 01214 01215 nuxAssert (_weak_reference_count->GetValue() >= 1); 01216 01217 _weak_reference_count->Decrement(); 01218 bool delete_warning = (_weak_reference_count->GetValue() == 0); 01219 01220 if (delete_warning) 01221 { 01222 if (!(*_destroyed)) 01223 { 01224 // The object is between Object::Destroy() and Object::~Object() 01225 ptr_->_reference_count = 0; 01226 ptr_->_weak_reference_count = 0; 01227 } 01228 else 01229 { 01230 // The object has been destroyed and this is the last weak reference to it. 01231 delete _destroyed; 01232 } 01233 delete _reference_count; 01234 delete _weak_reference_count; 01235 } 01236 01237 _reference_count = 0; 01238 _weak_reference_count = 0; 01239 ptr_ = 0; 01240 01241 return delete_warning; 01242 } 01243 01244 T *ptr_; 01245 NThreadSafeCounter *_reference_count; 01246 NThreadSafeCounter *_weak_reference_count; 01247 bool *_destroyed; 01248 01249 template <typename O> 01250 friend class ObjectWeakPtr; 01251 01252 template <typename O> 01253 friend class SmartPtr; 01254 01255 // template<typename T, typename U> 01256 // friend bool operator == (const ObjectWeakPtr<T>& a, const ObjectWeakPtr<U>& b); 01257 01258 // template<typename T, typename U> 01259 // friend bool operator != (const ObjectWeakPtr<T>& a, const ObjectWeakPtr<U>& b); 01260 01261 // template<typename T> 01262 // friend bool operator == (const ObjectWeakPtr<T>& a, T*); 01263 01264 template<typename U> 01265 friend bool operator == (U *, const ObjectWeakPtr<U>& a); 01266 01267 // template<typename T> 01268 // friend bool operator != (const ObjectWeakPtr<T>& a, T*); 01269 01270 template<typename U> 01271 friend bool operator != (U *, const ObjectWeakPtr<U>& a); 01272 01273 // template<typename T, typename U> 01274 // friend bool operator == (const ObjectPtr<T>& a, const ObjectWeakPtr<U>& b); 01275 // 01276 // template<typename T, typename U> 01277 // friend bool operator == (const ObjectWeakPtr<T>& a, const ObjectPtr<U>& b); 01278 // 01279 // template<typename T, typename U> 01280 // friend bool operator != (const ObjectPtr<T>& a, const ObjectWeakPtr<U>& b); 01281 // 01282 // template<typename T, typename U> 01283 // friend bool operator != (const ObjectWeakPtr<T>& a, const ObjectPtr<U>& b); 01284 01285 template <typename U, typename F> 01286 friend ObjectWeakPtr<U> staticCast (const ObjectWeakPtr<F>& from); 01287 01288 template <typename U, typename F> 01289 friend ObjectWeakPtr<U> constCast (const ObjectWeakPtr<F>& from); 01290 01291 template <typename U, typename F> 01292 friend ObjectWeakPtr<U> dynamicCast (const ObjectWeakPtr<F>& from); 01293 01294 template <typename U, typename F> 01295 friend ObjectWeakPtr<U> checkedCast (const ObjectWeakPtr<F>& from); 01296 01297 template <typename U, typename F> 01298 friend ObjectWeakPtr<U> queryCast (const ObjectWeakPtr<F>& from); 01299 }; 01300 01302 // globals 01303 01304 // template<typename T, typename U> 01305 // inline bool operator == (const ObjectPtr<T>& a, const ObjectPtr<U>& b) 01306 // { 01307 // return a.ptr_ == b.ptr_; 01308 // } 01309 // 01310 // template<typename T, typename U> 01311 // inline bool operator == (const ObjectWeakPtr<T>& a, const ObjectWeakPtr<U>& b) 01312 // { 01313 // return a.GetPointer () == b.GetPointer (); 01314 // } 01315 // 01316 // template<typename T, typename U> 01317 // inline bool operator == (const ObjectPtr<T>& a, const ObjectWeakPtr<U>& b) 01318 // { 01319 // return a.ptr_ == b.GetPointer (); 01320 // } 01321 // 01322 // template<typename T, typename U> 01323 // inline bool operator == (const ObjectWeakPtr<T>& a, const ObjectPtr<U>& b) 01324 // { 01325 // return a.GetPointer () == b.ptr_; 01326 // } 01327 // 01328 // template<typename T, typename U> 01329 // inline bool operator != (const ObjectPtr<T>& a, const ObjectPtr<U>& b) 01330 // { 01331 // return a.ptr_ != b.ptr_; 01332 // } 01333 // 01334 // template<typename T, typename U> 01335 // inline bool operator != (const ObjectWeakPtr<T>& a, const ObjectWeakPtr<U>& b) 01336 // { 01337 // return a.GetPointer () != b.GetPointer (); 01338 // } 01339 // 01340 // template<typename T, typename U> 01341 // inline bool operator != (const ObjectPtr<T>& a, const ObjectWeakPtr<U>& b) 01342 // { 01343 // return a.ptr_ != b.GetPointer (); 01344 // } 01345 // 01346 // template<typename T, typename U> 01347 // inline bool operator != (const ObjectWeakPtr<T>& a, const ObjectPtr<U>& b) 01348 // { 01349 // return a.GetPointer () != b.ptr_; 01350 // } 01351 01352 // template<typename T> 01353 // inline bool operator == (const ObjectPtr<T>& a, T* ptr) 01354 // { 01355 // return a.ptr_ == ptr; 01356 // } 01357 01358 template<typename T> 01359 inline bool operator == (T *ptr, const ObjectPtr<T>& a) 01360 { 01361 return a.ptr_ == ptr; 01362 } 01363 01364 // template<typename T> 01365 // inline bool operator != (const ObjectPtr<T>& a, T* ptr) 01366 // { 01367 // return a.ptr_ != ptr; 01368 // } 01369 01370 template<typename T> 01371 inline bool operator != (T *ptr, const ObjectPtr<T>& a) 01372 { 01373 return a.ptr_ != ptr; 01374 } 01375 01376 // template<typename T> 01377 // inline bool operator == (const ObjectWeakPtr<T>& a, T* ptr) 01378 // { 01379 // return a.ptr_ == ptr; 01380 // } 01381 01382 template<typename T> 01383 inline bool operator == (T *ptr, const ObjectWeakPtr<T>& a) 01384 { 01385 return a.ptr_ == ptr; 01386 } 01387 01388 // template<typename T> 01389 // inline bool operator != (const ObjectWeakPtr<T>& a, T* ptr) 01390 // { 01391 // return a.ptr_ != ptr; 01392 // } 01393 01394 template<typename T> 01395 inline bool operator != (T *ptr, const ObjectWeakPtr<T>& a) 01396 { 01397 return a.ptr_ != ptr; 01398 } 01399 01400 // /////////////////////////////////////////////////////// 01401 // // creation functions 01402 // 01403 // template <typename T> 01404 // ObjectPtr<T> Create () 01405 // { 01406 // RefCounts* rc = new RefCounts; 01407 // 01408 // try 01409 // { 01410 // T* t = new T; 01411 // return ObjectPtr<T> (t, rc); 01412 // } 01413 // catch (...) 01414 // { 01415 // delete rc; 01416 // throw; 01417 // } 01418 // } 01419 // 01420 // template <typename T, typename P1> 01421 // ObjectPtr<T> Create (P1 p1) 01422 // { 01423 // RefCounts* rc = new RefCounts; 01424 // 01425 // try 01426 // { 01427 // T* t = new T (p1); 01428 // return ObjectPtr<T> (t, rc); 01429 // } 01430 // catch (...) 01431 // { 01432 // delete rc; 01433 // throw; 01434 // } 01435 // } 01436 // 01437 // template <typename T, typename P1, typename P2> 01438 // ObjectPtr<T> Create (P1 p1, P2 p2) 01439 // { 01440 // RefCounts* rc = new RefCounts; 01441 // 01442 // try 01443 // { 01444 // T* t = new T (p1, p2); 01445 // return ObjectPtr<T> (t, rc); 01446 // } 01447 // catch (...) 01448 // { 01449 // delete rc; 01450 // throw; 01451 // } 01452 // } 01453 // 01454 // template <typename T, typename P1, typename P2, typename P3> 01455 // ObjectPtr<T> Create (P1 p1, P2 p2, P3 p3) 01456 // { 01457 // RefCounts* rc = new RefCounts; 01458 // 01459 // try 01460 // { 01461 // T* t = new T (p1, p2, p3); 01462 // return ObjectPtr<T> (t, rc); 01463 // } 01464 // catch (...) 01465 // { 01466 // delete rc; 01467 // throw; 01468 // } 01469 // } 01470 // 01471 // template <typename T, typename P1, typename P2, typename P3, typename P4> 01472 // ObjectPtr<T> Create (P1 p1, P2 p2, P3 p3, P4 p4) 01473 // { 01474 // RefCounts* rc = new RefCounts; 01475 // 01476 // try 01477 // { 01478 // T* t = new T (p1, p2, p3, p4); 01479 // return ObjectPtr<T> (t, rc); 01480 // } 01481 // catch (...) 01482 // { 01483 // delete rc; 01484 // throw; 01485 // } 01486 // } 01487 // 01488 // template <typename T, typename P1, typename P2, typename P3, typename P4, typename P5> 01489 // ObjectPtr<T> Create (P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) 01490 // { 01491 // RefCounts* rc = new RefCounts; 01492 // 01493 // try 01494 // { 01495 // T* t = new T (p1, p2, p3, p4, p5); 01496 // return ObjectPtr<T> (t, rc); 01497 // } 01498 // catch (...) 01499 // { 01500 // delete rc; 01501 // throw; 01502 // } 01503 // } 01504 // 01505 // template <typename T, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6> 01506 // ObjectPtr<T> Create (P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6) 01507 // { 01508 // RefCounts* rc = new RefCounts; 01509 // 01510 // try 01511 // { 01512 // T* t = new T (p1, p2, p3, p4, p5, p6); 01513 // return ObjectPtr<T> (t, rc); 01514 // } 01515 // catch (...) 01516 // { 01517 // delete rc; 01518 // throw; 01519 // } 01520 // } 01521 // 01522 // template <typename T, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7> 01523 // ObjectPtr<T> Create (P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7) 01524 // { 01525 // RefCounts* rc = new RefCounts; 01526 // 01527 // try 01528 // { 01529 // T* t = new T (p1, p2, p3, p4, p5, p6, p7); 01530 // return ObjectPtr<T> (t, rc); 01531 // } 01532 // catch (...) 01533 // { 01534 // delete rc; 01535 // throw; 01536 // } 01537 // } 01538 // 01539 // template <typename T> 01540 // ObjectPtr<T> WrapWSPtr (T* t) 01541 // { 01542 // if (t == 0) 01543 // { 01544 // return ObjectPtr<T> (); 01545 // } 01546 // 01547 // try 01548 // { 01549 // RefCounts* rc = new RefCounts; 01550 // 01551 // return ObjectPtr<T> (t, rc); 01552 // } 01553 // catch (...) 01554 // { 01555 // delete t; 01556 // throw; 01557 // } 01558 // } 01559 // 01560 // /////////////////////////////////////////////////////// 01561 // // casts 01562 // 01563 // template <typename U, typename F> 01564 // ObjectPtr<U> staticCast (const ObjectPtr<F>& from) 01565 // { 01566 // if (from.ptr_ == 0) 01567 // { 01568 // return ObjectPtr<U>(); 01569 // } 01570 // 01571 // U* ptr = static_cast <U*> (from.ptr_); 01572 // RefCounts* refCounts = from.refCounts_; 01573 // 01574 // if (ptr != 0) 01575 // { 01576 // //refCounts->strongRefs_.Increment(); 01577 // //refCounts->totalRefs_.Increment(); 01578 // from._reference_count->Increment(); 01579 // from._weak_reference_count->Increment(); 01580 // } 01581 // 01582 // return ObjectPtr<U> (ptr, refCounts); 01583 // } 01584 // 01585 // template <typename T, typename F> 01586 // ObjectPtr<T> constCast (const ObjectPtr<F>& from) 01587 // { 01588 // if (from.ptr_ == 0) 01589 // { 01590 // return ObjectPtr<T>(); 01591 // } 01592 // 01593 // T* ptr = const_cast <T*> (from.ptr_); 01594 // RefCounts* refCounts = from.refCounts_; 01595 // 01596 // if (ptr != 0) { 01597 // //refCounts->strongRefs_.Increment(); 01598 // //refCounts->totalRefs_.Increment(); 01599 // from._reference_count->Increment(); 01600 // from._weak_reference_count->Increment(); 01601 // } 01602 // 01603 // return ObjectPtr<T> (ptr, refCounts); 01604 // } 01605 // 01606 // template <typename T, typename F> 01607 // ObjectPtr<T> dynamicCast (const ObjectPtr<F>& from) 01608 // { 01609 // if (from.ptr_ == 0) 01610 // { 01611 // return ObjectPtr<T>(); 01612 // } 01613 // 01614 // T* ptr = &dynamic_cast <T&> (*from.ptr_); 01615 // RefCounts* refCounts = from.refCounts_; 01616 // 01617 // if (ptr != 0) 01618 // { 01619 // //refCounts->strongRefs_.Increment(); 01620 // //refCounts->totalRefs_.Increment(); 01621 // from._reference_count->Increment(); 01622 // from._weak_reference_count->Increment(); 01623 // } 01624 // 01625 // return ObjectPtr<T> (ptr, refCounts); 01626 // } 01627 // 01628 // template <typename T, typename F> 01629 // ObjectPtr<T> queryCast (const ObjectPtr<F>& from) 01630 // { 01631 // T* ptr = dynamic_cast <T*> (from.ptr_); 01632 // 01633 // if (ptr == 0) 01634 // { 01635 // return ObjectPtr<T>(); 01636 // } 01637 // 01638 // RefCounts* refCounts = from.refCounts_; 01639 // 01640 // if (ptr != 0) 01641 // { 01642 // //refCounts->strongRefs_.Increment(); 01643 // //refCounts->totalRefs_.Increment(); 01644 // from._reference_count->Increment(); 01645 // from._weak_reference_count->Increment(); 01646 // } 01647 // 01648 // return ObjectPtr<T> (ptr, refCounts); 01649 // } 01650 // 01651 // template <typename T, typename F> 01652 // ObjectPtr<T> checkedCast (const ObjectPtr<F>& from) 01653 // { 01654 // if (from.ptr_ == 0) 01655 // { 01656 // return ObjectPtr<T>(); 01657 // } 01658 // 01659 // nuxAssert(dynamic_cast<T*> (from.ptr_) != 0); 01660 // 01661 // T* ptr = static_cast <T*> (from.ptr_); 01662 // RefCounts* refCounts = from.refCounts_; 01663 // 01664 // if (ptr != 0) 01665 // { 01666 // //refCounts->strongRefs_.Increment(); 01667 // //refCounts->totalRefs_.Increment(); 01668 // from._reference_count->Increment(); 01669 // from._weak_reference_count->Increment(); 01670 // } 01671 // 01672 // return ObjectPtr<T> (ptr, refCounts); 01673 // } 01674 // 01675 // template <typename U, typename F> 01676 // ObjectWeakPtr<U> staticCast (const ObjectWeakPtr<F>& from) 01677 // { 01678 // if (from.GetPointer () == 0) 01679 // { 01680 // return ObjectWeakPtr<U>(); 01681 // } 01682 // 01683 // U* ptr = static_cast <U*> (from.ptr_); 01684 // RefCounts* refCounts = from.refCounts_; 01685 // 01686 // if (ptr != 0) 01687 // { 01688 // refCounts->totalRefs_.Increment(); 01689 // } 01690 // 01691 // return ObjectWeakPtr<U> (ptr, refCounts); 01692 // } 01693 // 01694 // template <typename T, typename F> 01695 // ObjectWeakPtr<T> constCast (const ObjectWeakPtr<F>& from) 01696 // { 01697 // if (from.GetPointer () == 0) 01698 // { 01699 // return ObjectWeakPtr<T>(); 01700 // } 01701 // 01702 // T* ptr = const_cast <T*> (from.ptr_); 01703 // RefCounts* refCounts = from.refCounts_; 01704 // 01705 // if (ptr != 0) 01706 // { 01707 // //refCounts->totalRefs_.Increment(); 01708 // from._weak_reference_count->Increment(); 01709 // } 01710 // 01711 // return ObjectWeakPtr<T> (ptr, refCounts); 01712 // } 01713 // 01714 // template <typename T, typename F> 01715 // ObjectWeakPtr<T> dynamicCast (const ObjectWeakPtr<F>& from) 01716 // { 01717 // if (from.GetPointer () == 0) 01718 // { 01719 // return ObjectWeakPtr<T>(); 01720 // } 01721 // 01722 // T* ptr = &dynamic_cast <T&> (*from.ptr_); 01723 // RefCounts* refCounts = from.refCounts_; 01724 // 01725 // if (ptr != 0) 01726 // { 01727 // //refCounts->totalRefs_.Increment(); 01728 // from._weak_reference_count->Increment(); 01729 // } 01730 // 01731 // return ObjectWeakPtr<T> (ptr, refCounts); 01732 // } 01733 // 01734 // template <typename T, typename F> 01735 // ObjectWeakPtr<T> queryCast (const ObjectWeakPtr<F>& from) 01736 // { 01737 // T* ptr = dynamic_cast <T*> (from.GetPointer ()); 01738 // 01739 // if (ptr == 0) 01740 // { 01741 // return ObjectWeakPtr<T>(); 01742 // } 01743 // 01744 // RefCounts* refCounts = from.refCounts_; 01745 // 01746 // if (ptr != 0) 01747 // { 01748 // //refCounts->totalRefs_.Increment(); 01749 // from._weak_reference_count->Increment(); 01750 // } 01751 // 01752 // return ObjectWeakPtr<T> (ptr, refCounts); 01753 // } 01754 // 01755 // template <typename T, typename F> 01756 // ObjectWeakPtr<T> checkedCast (const ObjectWeakPtr<F>& from) 01757 // { 01758 // if (from.GetPointer () == 0) 01759 // { 01760 // return ObjectWeakPtr<T>(); 01761 // } 01762 // 01763 // nuxAssert(dynamic_cast<T*> (from.ptr_) != 0); 01764 // 01765 // T* ptr = static_cast <T*> (from.ptr_); 01766 // RefCounts* refCounts = from.refCounts_; 01767 // 01768 // if (ptr != 0) 01769 // { 01770 // //refCounts->totalRefs_.Increment(); 01771 // from._weak_reference_count->Increment(); 01772 // } 01773 // 01774 // return ObjectWeakPtr<T> (ptr, refCounts); 01775 // } 01776 01777 // /////////////////////////////////////////////////////// 01778 // // std specializations 01779 // 01780 // template <typename T> 01781 // inline void swap (ObjectPtr<T>& t1, ObjectPtr<T>& t2) 01782 // { 01783 // t1.swap (t2); 01784 // } 01785 // 01786 // template <typename T> 01787 // inline void swap (ObjectWeakPtr<T>& t1, ObjectWeakPtr<T>& t2) 01788 // { 01789 // t1.swap (t2); 01790 // } 01791 01792 01793 } 01794 01795 #endif // INTRUSIVESP_H 01796 01797