nux-0.9.46

NuxCore/ObjectPtr.h

Go to the documentation of this file.
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       _objectptr_count->Decrement ();
00402 
00403       bool delete_warning = (_reference_count->GetValue() == 1) && (_weak_reference_count->GetValue() == 1);
00404 
00405       // If delete_warning == true, then we know that the cal to Unref will delete the pointer ptr. Also, the pointer to
00406       // _reference_count and _weak_reference_count will be deleted.
00407       if (delete_warning)
00408       {
00409         nuxAssert (_objectptr_count->GetValue () == 0);
00410       }
00411 
00412       bool destroyed = ptr_->UnReference();
00413       // if destroyed is true and delete_warning is false, it means that the pointer was destroyed. But to to hanging Weak pointers,
00414       // _weak_reference_count and _weak_reference_count have not been deleted.
00415 
00416       if (delete_warning)
00417       {
00418         nuxAssert (destroyed);
00419       }
00420 
00421       if (destroyed && delete_warning)
00422       {
00423         // Everything has been destroyed and there are no hanging weak pointers. Reset the pointers for this object.
00424         _reference_count = 0;
00425         _weak_reference_count = 0;
00426         _objectptr_count = 0;
00427         _destroyed = 0;
00428         ptr_ = 0;
00429       }
00430       else if (destroyed && (delete_warning == false) )
00431       {
00432         // Only the ptr_ has been destroyed.There are still hanging weak pointers. Reset the pointers for this object.
00433         nuxAssert (_reference_count->GetValue() == 0);
00434         nuxAssert (_objectptr_count->GetValue () == 0);
00435         nuxAssert (_weak_reference_count->GetValue() >= 1);
00436         _reference_count = 0;
00437         _weak_reference_count = 0;
00438         _objectptr_count = 0;
00439         _destroyed = 0;
00440         ptr_ = 0;
00441       }
00442       else
00443       {
00444         // There are still pending references to ptr_. Reset the pointers for this object.
00445         // Notice how we do the same thing here as in the previous conditions. We end up setting
00446         // _reference_count, _weak_reference_count and ptr_ to 0.
00447         _reference_count = 0;
00448         _weak_reference_count = 0;
00449         _objectptr_count = 0;
00450         _destroyed = 0;
00451         ptr_ = 0;
00452       }
00453 
00454       return destroyed;
00455     }
00456 
00457     T *ptr_;
00458     NThreadSafeCounter *_reference_count;
00459     NThreadSafeCounter *_weak_reference_count;
00460     NThreadSafeCounter *_objectptr_count;
00461     bool *_destroyed;
00462 
00463     template <typename U>
00464     friend ObjectPtr<U> Create ();
00465 
00466     template <typename U, typename P1>
00467     friend ObjectPtr<U> Create (P1 p1);
00468 
00469     template <typename U, typename P1, typename P2>
00470     friend ObjectPtr<U> Create (P1 p1, P2 p2);
00471 
00472     template <typename U, typename P1, typename P2, typename P3>
00473     friend ObjectPtr<U> Create (P1 p1, P2 p2, P3 p3);
00474 
00475     template <typename U, typename P1, typename P2, typename P3, typename P4>
00476     friend ObjectPtr<U> Create (P1 p1, P2 p2, P3 p3, P4 p4);
00477 
00478     template <typename U, typename P1, typename P2, typename P3, typename P4, typename P5>
00479     friend ObjectPtr<U> Create (P1 p1, P2 p2, P3 p3, P4 p4, P5 p5);
00480 
00481     template <typename U, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6>
00482     friend ObjectPtr<U> Create (P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6);
00483 
00484     template <typename U, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7>
00485     friend ObjectPtr<U> Create (P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7);
00486 
00487     template <typename U>
00488     friend ObjectPtr<U> WrapWSPtr (U *u);
00489 
00490     template <typename O>
00491     friend class ObjectPtr;
00492 
00493     template <typename O>
00494     friend class ObjectWeakPtr;
00495 
00496     //     template<typename T, typename U>
00497     //     friend bool operator == (const ObjectPtr<T>& a, const ObjectPtr<U>& b);
00498 
00499     //     template<typename T, typename U>
00500     //     friend bool operator != (const ObjectPtr<T>& a, const ObjectPtr<U>& b);
00501 
00502     //     template<typename T>
00503     //     friend bool operator == (const ObjectPtr<T>& a, T*);
00504 
00505     template<typename U>
00506     friend bool operator == (U *, const ObjectPtr<U>& a);
00507 
00508     //     template<typename T>
00509     //     friend bool operator != (const ObjectPtr<T>& a, T*);
00510 
00511     template<typename U>
00512     friend bool operator != (U *, const ObjectPtr<U>& a);
00513 
00514     //     template<typename T, typename U>
00515     //     friend bool operator == (const ObjectPtr<T>& a, const ObjectWeakPtr<U>& b);
00516 
00517     //     template<typename T, typename U>
00518     //     friend bool operator == (const ObjectWeakPtr<T>& a, const ObjectPtr<U>& b);
00519     //
00520     //     template<typename T, typename U>
00521     //     friend bool operator != (const ObjectPtr<T>& a, const ObjectWeakPtr<U>& b);
00522     //
00523     //     template<typename T, typename U>
00524     //     friend bool operator != (const ObjectWeakPtr<T>& a, const ObjectPtr<U>& b);
00525 
00526     template <typename U, typename F>
00527     friend ObjectPtr<U> staticCast (const ObjectPtr<F>& from);
00528 
00529     template <typename U, typename F>
00530     friend ObjectPtr<U> constCast (const ObjectPtr<F>& from);
00531 
00532     template <typename U, typename F>
00533     friend ObjectPtr<U> dynamicCast (const ObjectPtr<F>& from);
00534 
00535     template <typename U, typename F>
00536     friend ObjectPtr<U> checkedCast (const ObjectPtr<F>& from);
00537 
00538     template <typename U, typename F>
00539     friend ObjectPtr<U> queryCast (const ObjectPtr<F>& from);
00540   };
00541 
00542 
00544 
00549   template <typename T>
00550   class ObjectWeakPtr
00551   {
00552   public:
00554     ObjectWeakPtr ()
00555       :   ptr_ (0)
00556     {
00557       _reference_count = 0;
00558       _weak_reference_count = 0;
00559       _destroyed = 0;
00560     }
00561 
00563 
00569     explicit ObjectWeakPtr (T *ptr, bool WarnMissuse = false)
00570       :   ptr_ (0)
00571     {
00572       _reference_count = 0;
00573       _weak_reference_count = 0;
00574       _destroyed = 0;
00575 
00576       if (ptr != 0)
00577       {
00578         if (WarnMissuse && (ptr->OwnsTheReference () == false))
00579         {
00580           //nuxDebugMsg (TEXT ("[ObjectWeakPtr::ObjectWeakPtr] Warning: Constructing a weak smart pointer from an object with a floating reference.") );
00581         }
00582 
00583         ptr_ = ptr;
00584         _reference_count = ptr->_reference_count;
00585         _weak_reference_count = ptr->_weak_reference_count;
00586         _destroyed = ptr->_destroyed;
00587         ptr_->IncrementWeakCounter ();
00588       }
00589     }
00590 
00592 
00598     template <typename O>
00599     explicit ObjectWeakPtr (O *ptr, bool WarnMissuse = false)
00600       :   ptr_ (0)
00601     {
00602       _reference_count = 0;
00603       _weak_reference_count = 0;
00604       _destroyed = 0;
00605 
00606       if (ptr != 0)
00607       {
00608         if (ptr->Type ().IsDerivedFromType (T::StaticObjectType))
00609         {
00610           if (WarnMissuse && (ptr->OwnsTheReference () == false))
00611           {
00612             //nuxDebugMsg (TEXT ("[ObjectWeakPtr::ObjectWeakPtr] Warning: Constructing a weak smart pointer from an object with a floating reference.") );
00613           }
00614 
00615           ptr_ = (T *) ptr;
00616           _reference_count = ptr->_reference_count;
00617           _weak_reference_count = ptr->_weak_reference_count;
00618           _destroyed = ptr->_destroyed;
00619           ptr_->IncrementWeakCounter ();
00620         }
00621       }
00622     }
00623 
00625 
00628     ObjectWeakPtr (const ObjectWeakPtr<T> &other)
00629     {
00630       ptr_ = other.ptr_;
00631       _reference_count = other._reference_count;
00632       _weak_reference_count = other._weak_reference_count;
00633       _destroyed = other._destroyed;
00634 
00635       // Warning: We cannot count on ptr_ to be valid.
00636       // If _weak_reference_count is not null, then imcrement it.
00637       if (_weak_reference_count)
00638       {
00639         _weak_reference_count->Increment ();
00640       }
00641       else
00642       {
00643         // Sanity checks
00644         nuxAssert (_reference_count == 0);
00645         nuxAssert (_weak_reference_count == 0);
00646         nuxAssert (_destroyed == 0);
00647       }
00648     }
00649 
00650     // Warning: We are not sure that other.ptr_ is valid.
00651     // Warning: Cannot call other.ptr_->Type().IsDerivedFromType (T::StaticObjectType)
00653 
00656     template <typename O>
00657     ObjectWeakPtr (const ObjectWeakPtr<O>& other)
00658       :   ptr_ (0)
00659     {
00660       _reference_count = 0;
00661       _weak_reference_count = 0;
00662       _destroyed = 0;
00663 
00664       if (other.ptr_ && other.ptr_->Type ().IsDerivedFromType (T::StaticObjectType))
00665       {
00666         ptr_ = other.ptr_;
00667         _reference_count = other._reference_count;
00668         _weak_reference_count = other._weak_reference_count;
00669         _destroyed = other._destroyed;
00670 
00671         // Warning: We cannot count on ptr_ to be valid.
00672         // If _weak_reference_count is not null, then imcrement it.
00673         if (_weak_reference_count)
00674         {
00675           _weak_reference_count->Increment ();
00676         }
00677         else
00678         {
00679           // Sanity checks
00680           nuxAssert (_reference_count == 0);
00681           nuxAssert (_weak_reference_count == 0);
00682           nuxAssert (_destroyed == 0);
00683         }        
00684       }
00685     }
00686 
00688 
00691     template <typename O>
00692     ObjectWeakPtr (const ObjectPtr<O> &other)
00693       :   ptr_ (0)
00694     {
00695       _reference_count = 0;
00696       _weak_reference_count = 0;
00697       _destroyed = 0;
00698 
00699       if (other.ptr_ && other.ptr_->Type ().IsDerivedFromType (T::StaticObjectType))
00700       {
00701         ptr_ = other.ptr_;
00702         _reference_count = other._reference_count;
00703         _weak_reference_count = other._weak_reference_count;
00704         _destroyed = other._destroyed;
00705 
00706         if (ptr_ != 0)
00707         {
00708           _weak_reference_count->Increment ();
00709         }
00710         else
00711         {
00712           // Sanity checks
00713           nuxAssert (_reference_count == 0);
00714           nuxAssert (_weak_reference_count == 0);
00715           nuxAssert (_destroyed == 0);
00716         }
00717       }
00718     }
00719 
00721 
00724     ObjectWeakPtr& operator = (const ObjectWeakPtr<T> &other)
00725     {
00726       if (GetPointer () != other.GetPointer () ) // Avoid self assignment.
00727       {
00728         ReleaseReference ();
00729 
00730         ptr_ = other.ptr_;
00731         _reference_count = other._reference_count;
00732         _weak_reference_count = other._weak_reference_count;
00733         _destroyed = other._destroyed;
00734 
00735         // Warning: We cannot count on ptr_ to be valid.
00736         // If _weak_reference_count is not null, then imcrement it.
00737         if (_weak_reference_count)
00738         {
00739           _weak_reference_count->Increment ();
00740         }
00741         else
00742         {
00743           // Sanity checks
00744           nuxAssert (_reference_count == 0);
00745           nuxAssert (_weak_reference_count == 0);
00746           nuxAssert (_destroyed == 0);
00747         }       
00748       }
00749 
00750       return *this;
00751     }
00752 
00753     // Warning: We are not sure that other.ptr_ is valid.
00754     // Warning: Cannot call other.ptr_->Type().IsDerivedFromType (T::StaticObjectType)
00756 
00759     template <typename O>
00760     ObjectWeakPtr &operator = (const ObjectWeakPtr<O>& other)
00761     {
00762       if (other.ptr_ && other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) )
00763       {
00764         if (GetPointer () != other.GetPointer () ) // Avoid self assignment.
00765         {
00766           ReleaseReference ();
00767 
00768           ptr_ = other.ptr_;
00769           //refCounts_ = other.refCounts_;
00770           _reference_count = other._reference_count;
00771           _weak_reference_count = other._weak_reference_count;
00772           _destroyed = other._destroyed;
00773 
00774           // Warning: We cannot count on ptr_ to be valid.
00775           // If _weak_reference_count is not null, then imcrement it.
00776           if (_weak_reference_count)
00777           {
00778             _weak_reference_count->Increment ();
00779           }
00780           else
00781           {
00782             // Sanity checks
00783             nuxAssert (_reference_count == 0);
00784             nuxAssert (_weak_reference_count == 0);
00785             nuxAssert (_destroyed == 0);
00786           }
00787         }
00788       }
00789       else
00790       {
00791         ReleaseReference();
00792       }
00793 
00794       return *this;
00795     }
00796 
00798 
00801     /*ObjectWeakPtr& operator = (const ObjectPtr<T> &other)
00802     {
00803       if (GetPointer () != other.ptr_) // Avoid self assignment.
00804       {
00805         ReleaseReference ();
00806 
00807         ptr_ = other.ptr_;
00808         //refCounts_ = other.refCounts_;
00809         _reference_count = other._reference_count;
00810         _weak_reference_count = other._weak_reference_count;
00811         _destroyed = other._destroyed;
00812 
00813         if (ptr_ != 0)
00814         {
00815           _weak_reference_count->Increment ();
00816         }
00817       }
00818       else
00819       {
00820         ReleaseReference ();
00821       }
00822 
00823       return *this;
00824     }*/
00825 
00827 
00830     template <typename O>
00831     ObjectWeakPtr &operator = (const ObjectPtr<O>& other)
00832     {
00833       if (other.ptr_ && other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) )
00834       {
00835         if (GetPointer () != other.ptr_) // Avoid self assignment.
00836         {
00837           ReleaseReference ();
00838 
00839           ptr_ = other.ptr_;
00840           //refCounts_ = other.refCounts_;
00841           _reference_count = other._reference_count;
00842           _weak_reference_count = other._weak_reference_count;
00843           _destroyed = other._destroyed;
00844 
00845           if (ptr_ != 0)
00846           {
00847             //refCounts_->totalRefs_.Increment();
00848             _weak_reference_count->Increment();
00849           }
00850         }
00851       }
00852       else
00853       {
00854         ReleaseReference();
00855       }
00856 
00857       return *this;
00858     }
00859 
00861 
00867     ObjectWeakPtr& operator = (T *ptr)
00868     {
00869       if (ptr_ && _reference_count && (_reference_count->GetValue() != 0) )
00870       {
00871         ptr_->DecrementWeakCounter();
00872       }
00873       else if (_reference_count && _weak_reference_count)
00874       {
00875         _weak_reference_count->Decrement();
00876       }
00877 
00878       if (_reference_count && _weak_reference_count && (_reference_count->GetValue() == 0) && (_weak_reference_count->GetValue() == 0) )
00879       {
00880         if (!(*_destroyed))
00881         {
00882           // The object is between Object::Destroy() and Object::~Object()
00883           ptr_->_reference_count = 0;
00884           ptr_->_weak_reference_count = 0;
00885         }
00886         else
00887         {
00888           // The object has been destroyed and this is the last weak reference to it.
00889           delete _destroyed;
00890         }
00891         delete _reference_count;
00892         delete _weak_reference_count;
00893         
00894       }
00895       
00896       ptr_ = 0;
00897       _reference_count = 0;
00898       _weak_reference_count = 0;
00899       _destroyed = 0;
00900 
00901       if (ptr != 0)
00902       {
00903         if (ptr->OwnsTheReference() == false)
00904         {
00905           //nuxDebugMsg (TEXT ("[ObjectWeakPtr::operator = ()] Warning: Constructing a weak smart pointer from an object with a floating reference.") );
00906         }
00907 
00908         ptr_ = ptr;
00909         _reference_count = ptr->_reference_count;
00910         _weak_reference_count = ptr->_weak_reference_count;
00911         _destroyed = ptr->_destroyed;
00912         ptr_->IncrementWeakCounter();
00913       }
00914 
00915       return *this;
00916     }
00917 
00919 
00925     template <typename O>
00926     ObjectWeakPtr &operator = (O *ptr)
00927     {
00928       if (ptr_ && _reference_count && (_reference_count->GetValue() != 0) )
00929       {
00930         ptr_->DecrementWeakCounter();
00931       }
00932       else if (_reference_count && _weak_reference_count)
00933       {
00934         _weak_reference_count->Decrement();
00935       }
00936       else
00937       {
00938         nuxAssertMsg (0, TEXT ("Could there be something wrong her?") );
00939       }
00940 
00941       if (_reference_count && _weak_reference_count && (_reference_count->GetValue() == 0) && (_weak_reference_count->GetValue() == 0) )
00942       {
00943         if (!(*_destroyed))
00944         {
00945           // The object is between Object::Destroy() and Object::~Object()
00946           ptr_->_reference_count = 0;
00947           ptr_->_weak_reference_count = 0;
00948         }
00949         else
00950         {
00951           // The object has been destroyed and this is the last weak reference to it.
00952           delete _destroyed;
00953         }
00954         delete _reference_count;
00955         delete _weak_reference_count;
00956       }
00957 
00958       ptr_ = 0;
00959       _reference_count = 0;
00960       _weak_reference_count = 0;
00961       _destroyed = 0;
00962 
00963       if (ptr != 0)
00964       {
00965         if (ptr->Type().IsDerivedFromType (T::StaticObjectType) )
00966         {
00967           if (ptr->OwnsTheReference() == false)
00968           {
00969             //nuxDebugMsg (TEXT ("[ObjectWeakPtr::operator = ()] Warning: Constructing a weak smart pointer from an object with a floating reference.") );
00970           }
00971 
00972           ptr_ = (T *) ptr;
00973           _reference_count = ptr->_reference_count;
00974           _weak_reference_count = ptr->_weak_reference_count;
00975           _destroyed = ptr->_destroyed;
00976           ptr_->IncrementWeakCounter();
00977         }
00978       }
00979     }
00980 
00981     ~ObjectWeakPtr ()
00982     {
00983       ReleaseReference ();
00984     }
00985 
00986     T &operator * () const
00987     {
00988       nuxAssert (_reference_count && (_reference_count->GetValue() != 0) && (ptr_ != 0) );
00989 
00990       return *(NUX_CONST_CAST (T*, GetPointer ()));
00991     }
00992 
00993     T *operator -> () const
00994     {
00995       nuxAssert (_reference_count && (_reference_count->GetValue() != 0) && (ptr_ != 0) );
00996 
00997       return NUX_CONST_CAST (T*, GetPointer ());
00998     }
00999 
01000 //     void Swap (ObjectWeakPtr<T>& other)
01001 //     {
01002 //         std::swap (ptr_, other.ptr_);
01003 //         std::swap (refCounts_, other.refCounts_);
01004 //     }
01005 
01006     bool operator < (T *ptr) const
01007     {
01008       return (ptr_ < ptr);
01009     }
01010 
01011     bool operator > (T *ptr) const
01012     {
01013       return (ptr_ > ptr);
01014     }
01015 
01016     bool operator < (ObjectWeakPtr<T> other) const
01017     {
01018       return (ptr_ < other.ptr_);
01019     }
01020 
01021     bool operator > (ObjectWeakPtr<T> other) const
01022     {
01023       return (ptr_ > other.ptr_);
01024     }
01025 
01026     bool operator != (T *ptr) const
01027     {
01028       return ( (void *) ptr_ != (void *) ptr);
01029     }
01030 
01031     bool operator == (T *ptr) const
01032     {
01033       return ( (void *) ptr_ == (void *) ptr);
01034     }
01035 
01036     template<typename U>
01037     bool operator != (U *ptr) const
01038     {
01039       if (ptr && (!ptr->Type().IsDerivedFromType (T::StaticObjectType) ) )
01040         return true;
01041 
01042       return ( (void *) ptr_ != (void *) ptr);
01043     }
01044 
01045     template<typename U>
01046     bool operator == (U *ptr) const
01047     {
01048       if (ptr && (!ptr->Type().IsDerivedFromType (T::StaticObjectType) ) )
01049         return false;
01050 
01051       return ( (void *) ptr_ == (void *) ptr);
01052     }
01053 
01058     template<typename U>
01059     bool operator != (const ObjectWeakPtr<U>& other) const
01060     {
01061       // Test if the object in other has been destroyed. If yes, then we can't call other.ptr_->Type().
01062       if (other._reference_count->GetValue () != 0)
01063       {
01064         if (other.ptr_ && (!other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) ) )
01065           return true;
01066       }
01067 
01068       return ( (void *) ptr_ != (void *) other.ptr_);
01069     }
01070 
01075     template<typename U>
01076     bool operator == (const ObjectWeakPtr<U>& other) const
01077     {
01078       // Test if the object in other has been destroyed. If yes, then we can't call other.ptr_->Type().
01079       if (other._reference_count->GetValue () != 0)
01080       {
01081         if (other.ptr_ && (!other.ptr_->Type().IsDerivedFromType (T::StaticObjectType)))
01082           return false;
01083       }
01084 
01085       return ( (void *) ptr_ == (void *) other.ptr_);
01086     }
01087 
01092     template<typename U>
01093     bool operator != (const ObjectPtr<U>& other) const
01094     {
01095       if (other.ptr_ && (!other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) ) )
01096         return true;
01097 
01098       return ( (void *) ptr_ != (void *) other.ptr_);
01099     }
01100 
01105     template<typename U>
01106     bool operator == (const ObjectPtr<U>& other) const
01107     {
01108       if (other.ptr_ && (!other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) ) )
01109         return false;
01110 
01111       return ( (void *) ptr_ == (void *) other.ptr_);
01112     }
01113 
01115 
01118     bool operator () () const
01119     {
01120       return GetPointer() != 0;
01121     }
01122     
01124 
01127     bool IsValid() const
01128     {
01129       return GetPointer() != 0;
01130     }
01131 
01133 
01136     bool IsNull() const
01137     {
01138       return GetPointer() == 0;
01139     }
01140 
01142 
01151     bool Release()
01152     {
01153       return ReleaseReference ();
01154     }
01155 
01157 
01161     const T* GetPointer () const
01162     {
01163       if ((_weak_reference_count == 0) || (_weak_reference_count->GetValue () == 0))
01164       {
01165         return 0;
01166       }
01167 
01168       if ((_reference_count == 0) || (_reference_count->GetValue () == 0))
01169       {
01170         return 0;
01171       }
01172 
01173       return ptr_;
01174     }
01175 
01177 
01181     T *GetPointer() 
01182     {
01183       return NUX_CONST_CAST (T*, (const_cast< const ObjectWeakPtr* > (this))->GetPointer ());
01184     }
01185 
01186     int GetReferenceCount ()
01187     {
01188       if (_reference_count == 0)
01189       {
01190         return 0;
01191       }
01192 
01193       return _reference_count()->GetValue ();
01194     }
01195 
01196     int GetWeakReferenceCount ()
01197     {
01198       if (_weak_reference_count == 0)
01199       {
01200         return 0;
01201       }
01202 
01203       return _weak_reference_count->GetValue ();
01204     }
01205 
01206   private:
01207     bool ReleaseReference ()
01208     {
01209       if (ptr_ == 0)
01210       {
01211         return false;
01212       }
01213 
01214       nuxAssert (_weak_reference_count->GetValue() >= 1);
01215 
01216       _weak_reference_count->Decrement();
01217       bool delete_warning = (_weak_reference_count->GetValue() == 0);
01218 
01219       if (delete_warning)
01220       {
01221         if (!(*_destroyed))
01222         {
01223           // The object is between Object::Destroy() and Object::~Object()
01224           ptr_->_reference_count = 0;
01225           ptr_->_weak_reference_count = 0;
01226         }
01227         else
01228         {
01229           // The object has been destroyed and this is the last weak reference to it.
01230           delete _destroyed;
01231         }
01232         delete _reference_count;
01233         delete _weak_reference_count;
01234       }
01235 
01236       _reference_count = 0;
01237       _weak_reference_count = 0;
01238       ptr_ = 0;
01239 
01240       return delete_warning; 
01241     }
01242 
01243     T *ptr_;
01244     NThreadSafeCounter *_reference_count;
01245     NThreadSafeCounter *_weak_reference_count;
01246     bool               *_destroyed;
01247 
01248     template <typename O>
01249     friend class ObjectWeakPtr;
01250 
01251     template <typename O>
01252     friend class SmartPtr;
01253 
01254     //     template<typename T, typename U>
01255     //     friend bool operator == (const ObjectWeakPtr<T>& a, const ObjectWeakPtr<U>& b);
01256 
01257     //     template<typename T, typename U>
01258     //     friend bool operator != (const ObjectWeakPtr<T>& a, const ObjectWeakPtr<U>& b);
01259 
01260     //     template<typename T>
01261     //     friend bool operator == (const ObjectWeakPtr<T>& a, T*);
01262 
01263     template<typename U>
01264     friend bool operator == (U *, const ObjectWeakPtr<U>& a);
01265 
01266     //     template<typename T>
01267     //     friend bool operator != (const ObjectWeakPtr<T>& a, T*);
01268 
01269     template<typename U>
01270     friend bool operator != (U *, const ObjectWeakPtr<U>& a);
01271 
01272     //     template<typename T, typename U>
01273     //     friend bool operator == (const ObjectPtr<T>& a, const ObjectWeakPtr<U>& b);
01274     //
01275     //     template<typename T, typename U>
01276     //     friend bool operator == (const ObjectWeakPtr<T>& a, const ObjectPtr<U>& b);
01277     //
01278     //     template<typename T, typename U>
01279     //     friend bool operator != (const ObjectPtr<T>& a, const ObjectWeakPtr<U>& b);
01280     //
01281     //     template<typename T, typename U>
01282     //     friend bool operator != (const ObjectWeakPtr<T>& a, const ObjectPtr<U>& b);
01283 
01284     template <typename U, typename F>
01285     friend ObjectWeakPtr<U> staticCast (const ObjectWeakPtr<F>& from);
01286 
01287     template <typename U, typename F>
01288     friend ObjectWeakPtr<U> constCast (const ObjectWeakPtr<F>& from);
01289 
01290     template <typename U, typename F>
01291     friend ObjectWeakPtr<U> dynamicCast (const ObjectWeakPtr<F>& from);
01292 
01293     template <typename U, typename F>
01294     friend ObjectWeakPtr<U> checkedCast (const ObjectWeakPtr<F>& from);
01295 
01296     template <typename U, typename F>
01297     friend ObjectWeakPtr<U> queryCast (const ObjectWeakPtr<F>& from);
01298   };
01299 
01301 // globals
01302 
01303 // template<typename T, typename U>
01304 // inline bool operator == (const ObjectPtr<T>& a, const ObjectPtr<U>& b)
01305 // {
01306 //     return a.ptr_ == b.ptr_;
01307 // }
01308 //
01309 // template<typename T, typename U>
01310 // inline bool operator == (const ObjectWeakPtr<T>& a, const ObjectWeakPtr<U>& b)
01311 // {
01312 //     return a.GetPointer () == b.GetPointer ();
01313 // }
01314 //
01315 // template<typename T, typename U>
01316 // inline bool operator == (const ObjectPtr<T>& a, const ObjectWeakPtr<U>& b)
01317 // {
01318 //     return a.ptr_ == b.GetPointer ();
01319 // }
01320 //
01321 // template<typename T, typename U>
01322 // inline bool operator == (const ObjectWeakPtr<T>& a, const ObjectPtr<U>& b)
01323 // {
01324 //     return a.GetPointer () == b.ptr_;
01325 // }
01326 //
01327 // template<typename T, typename U>
01328 // inline bool operator != (const ObjectPtr<T>& a, const ObjectPtr<U>& b)
01329 // {
01330 //     return a.ptr_ != b.ptr_;
01331 // }
01332 //
01333 // template<typename T, typename U>
01334 // inline bool operator != (const ObjectWeakPtr<T>& a, const ObjectWeakPtr<U>& b)
01335 // {
01336 //     return a.GetPointer () != b.GetPointer ();
01337 // }
01338 //
01339 // template<typename T, typename U>
01340 // inline bool operator != (const ObjectPtr<T>& a, const ObjectWeakPtr<U>& b)
01341 // {
01342 //     return a.ptr_ != b.GetPointer ();
01343 // }
01344 //
01345 // template<typename T, typename U>
01346 // inline bool operator != (const ObjectWeakPtr<T>& a, const ObjectPtr<U>& b)
01347 // {
01348 //     return a.GetPointer () != b.ptr_;
01349 // }
01350 
01351 // template<typename T>
01352 // inline bool operator == (const ObjectPtr<T>& a, T* ptr)
01353 // {
01354 //     return a.ptr_ == ptr;
01355 // }
01356 
01357   template<typename T>
01358   inline bool operator == (T *ptr, const ObjectPtr<T>& a)
01359   {
01360     return a.ptr_ == ptr;
01361   }
01362 
01363 // template<typename T>
01364 // inline bool operator != (const ObjectPtr<T>& a, T* ptr)
01365 // {
01366 //     return a.ptr_ != ptr;
01367 // }
01368 
01369   template<typename T>
01370   inline bool operator != (T *ptr, const ObjectPtr<T>& a)
01371   {
01372     return a.ptr_ != ptr;
01373   }
01374 
01375 // template<typename T>
01376 // inline bool operator == (const ObjectWeakPtr<T>& a, T* ptr)
01377 // {
01378 //     return a.ptr_ == ptr;
01379 // }
01380 
01381   template<typename T>
01382   inline bool operator == (T *ptr, const ObjectWeakPtr<T>& a)
01383   {
01384     return a.ptr_ == ptr;
01385   }
01386 
01387 // template<typename T>
01388 // inline bool operator != (const ObjectWeakPtr<T>& a, T* ptr)
01389 // {
01390 //     return a.ptr_ != ptr;
01391 // }
01392 
01393   template<typename T>
01394   inline bool operator != (T *ptr, const ObjectWeakPtr<T>& a)
01395   {
01396     return a.ptr_ != ptr;
01397   }
01398 
01399 // ///////////////////////////////////////////////////////
01400 // // creation functions
01401 //
01402 // template <typename T>
01403 // ObjectPtr<T> Create ()
01404 // {
01405 //     RefCounts* rc = new RefCounts;
01406 //
01407 //     try
01408 //     {
01409 //         T* t = new T;
01410 //         return ObjectPtr<T> (t, rc);
01411 //     }
01412 //     catch (...)
01413 //     {
01414 //         delete rc;
01415 //         throw;
01416 //     }
01417 // }
01418 //
01419 // template <typename T, typename P1>
01420 // ObjectPtr<T> Create (P1 p1)
01421 // {
01422 //     RefCounts* rc = new RefCounts;
01423 //
01424 //     try
01425 //     {
01426 //         T* t = new T (p1);
01427 //         return ObjectPtr<T> (t, rc);
01428 //     }
01429 //     catch (...)
01430 //     {
01431 //         delete rc;
01432 //         throw;
01433 //     }
01434 // }
01435 //
01436 // template <typename T, typename P1, typename P2>
01437 // ObjectPtr<T> Create (P1 p1, P2 p2)
01438 // {
01439 //     RefCounts* rc = new RefCounts;
01440 //
01441 //     try
01442 //     {
01443 //         T* t = new T (p1, p2);
01444 //         return ObjectPtr<T> (t, rc);
01445 //     }
01446 //     catch (...)
01447 //     {
01448 //         delete rc;
01449 //         throw;
01450 //     }
01451 // }
01452 //
01453 // template <typename T, typename P1, typename P2, typename P3>
01454 // ObjectPtr<T> Create (P1 p1, P2 p2, P3 p3)
01455 // {
01456 //     RefCounts* rc = new RefCounts;
01457 //
01458 //     try
01459 //     {
01460 //         T* t = new T (p1, p2, p3);
01461 //         return ObjectPtr<T> (t, rc);
01462 //     }
01463 //     catch (...)
01464 //     {
01465 //         delete rc;
01466 //         throw;
01467 //     }
01468 // }
01469 //
01470 // template <typename T, typename P1, typename P2, typename P3, typename P4>
01471 // ObjectPtr<T> Create (P1 p1, P2 p2, P3 p3, P4 p4)
01472 // {
01473 //     RefCounts* rc = new RefCounts;
01474 //
01475 //     try
01476 //     {
01477 //         T* t = new T (p1, p2, p3, p4);
01478 //         return ObjectPtr<T> (t, rc);
01479 //     }
01480 //     catch (...)
01481 //     {
01482 //         delete rc;
01483 //         throw;
01484 //     }
01485 // }
01486 //
01487 // template <typename T, typename P1, typename P2, typename P3, typename P4, typename P5>
01488 // ObjectPtr<T> Create (P1 p1, P2 p2, P3 p3, P4 p4, P5 p5)
01489 // {
01490 //     RefCounts* rc = new RefCounts;
01491 //
01492 //     try
01493 //     {
01494 //         T* t = new T (p1, p2, p3, p4, p5);
01495 //         return ObjectPtr<T> (t, rc);
01496 //     }
01497 //     catch (...)
01498 //     {
01499 //         delete rc;
01500 //         throw;
01501 //     }
01502 // }
01503 //
01504 // template <typename T, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6>
01505 // ObjectPtr<T> Create (P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6)
01506 // {
01507 //     RefCounts* rc = new RefCounts;
01508 //
01509 //     try
01510 //     {
01511 //         T* t = new T (p1, p2, p3, p4, p5, p6);
01512 //         return ObjectPtr<T> (t, rc);
01513 //     }
01514 //     catch (...)
01515 //     {
01516 //         delete rc;
01517 //         throw;
01518 //     }
01519 // }
01520 //
01521 // template <typename T, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7>
01522 // ObjectPtr<T> Create (P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7)
01523 // {
01524 //     RefCounts* rc = new RefCounts;
01525 //
01526 //     try
01527 //     {
01528 //         T* t = new T (p1, p2, p3, p4, p5, p6, p7);
01529 //         return ObjectPtr<T> (t, rc);
01530 //     }
01531 //     catch (...)
01532 //     {
01533 //         delete rc;
01534 //         throw;
01535 //     }
01536 // }
01537 //
01538 // template <typename T>
01539 // ObjectPtr<T> WrapWSPtr (T* t)
01540 // {
01541 //     if (t == 0)
01542 //     {
01543 //         return ObjectPtr<T> ();
01544 //     }
01545 //
01546 //     try
01547 //     {
01548 //         RefCounts* rc = new RefCounts;
01549 //
01550 //         return ObjectPtr<T> (t, rc);
01551 //     }
01552 //     catch (...)
01553 //     {
01554 //         delete t;
01555 //         throw;
01556 //     }
01557 // }
01558 //
01559 // ///////////////////////////////////////////////////////
01560 // // casts
01561 //
01562 // template <typename U, typename F>
01563 // ObjectPtr<U> staticCast (const ObjectPtr<F>& from)
01564 // {
01565 //     if (from.ptr_ == 0)
01566 //     {
01567 //         return ObjectPtr<U>();
01568 //     }
01569 //
01570 //     U* ptr = static_cast <U*> (from.ptr_);
01571 //     RefCounts* refCounts = from.refCounts_;
01572 //
01573 //     if (ptr != 0)
01574 //     {
01575 //         //refCounts->strongRefs_.Increment();
01576 //         //refCounts->totalRefs_.Increment();
01577 //         from._reference_count->Increment();
01578 //         from._weak_reference_count->Increment();
01579 //     }
01580 //
01581 //     return ObjectPtr<U> (ptr, refCounts);
01582 // }
01583 //
01584 // template <typename T, typename F>
01585 // ObjectPtr<T> constCast (const ObjectPtr<F>& from)
01586 // {
01587 //     if (from.ptr_ == 0)
01588 //     {
01589 //         return ObjectPtr<T>();
01590 //     }
01591 //
01592 //     T* ptr = const_cast <T*> (from.ptr_);
01593 //     RefCounts* refCounts = from.refCounts_;
01594 //
01595 //     if (ptr != 0) {
01596 //         //refCounts->strongRefs_.Increment();
01597 //         //refCounts->totalRefs_.Increment();
01598 //         from._reference_count->Increment();
01599 //         from._weak_reference_count->Increment();
01600 //     }
01601 //
01602 //     return ObjectPtr<T> (ptr, refCounts);
01603 // }
01604 //
01605 // template <typename T, typename F>
01606 // ObjectPtr<T> dynamicCast (const ObjectPtr<F>& from)
01607 // {
01608 //     if (from.ptr_ == 0)
01609 //     {
01610 //         return ObjectPtr<T>();
01611 //     }
01612 //
01613 //     T* ptr = &dynamic_cast <T&> (*from.ptr_);
01614 //     RefCounts* refCounts = from.refCounts_;
01615 //
01616 //     if (ptr != 0)
01617 //     {
01618 //         //refCounts->strongRefs_.Increment();
01619 //         //refCounts->totalRefs_.Increment();
01620 //         from._reference_count->Increment();
01621 //         from._weak_reference_count->Increment();
01622 //     }
01623 //
01624 //     return ObjectPtr<T> (ptr, refCounts);
01625 // }
01626 //
01627 // template <typename T, typename F>
01628 // ObjectPtr<T> queryCast (const ObjectPtr<F>& from)
01629 // {
01630 //     T* ptr = dynamic_cast <T*> (from.ptr_);
01631 //
01632 //     if (ptr == 0)
01633 //     {
01634 //         return ObjectPtr<T>();
01635 //     }
01636 //
01637 //     RefCounts* refCounts = from.refCounts_;
01638 //
01639 //     if (ptr != 0)
01640 //     {
01641 //         //refCounts->strongRefs_.Increment();
01642 //         //refCounts->totalRefs_.Increment();
01643 //         from._reference_count->Increment();
01644 //         from._weak_reference_count->Increment();
01645 //     }
01646 //
01647 //     return ObjectPtr<T> (ptr, refCounts);
01648 // }
01649 //
01650 // template <typename T, typename F>
01651 // ObjectPtr<T> checkedCast (const ObjectPtr<F>& from)
01652 // {
01653 //     if (from.ptr_ == 0)
01654 //     {
01655 //         return ObjectPtr<T>();
01656 //     }
01657 //
01658 //     nuxAssert(dynamic_cast<T*> (from.ptr_) != 0);
01659 //
01660 //     T* ptr = static_cast <T*> (from.ptr_);
01661 //     RefCounts* refCounts = from.refCounts_;
01662 //
01663 //     if (ptr != 0)
01664 //     {
01665 //         //refCounts->strongRefs_.Increment();
01666 //         //refCounts->totalRefs_.Increment();
01667 //         from._reference_count->Increment();
01668 //         from._weak_reference_count->Increment();
01669 //     }
01670 //
01671 //     return ObjectPtr<T> (ptr, refCounts);
01672 // }
01673 //
01674 // template <typename U, typename F>
01675 // ObjectWeakPtr<U> staticCast (const ObjectWeakPtr<F>& from)
01676 // {
01677 //     if (from.GetPointer () == 0)
01678 //     {
01679 //         return ObjectWeakPtr<U>();
01680 //     }
01681 //
01682 //     U* ptr = static_cast <U*> (from.ptr_);
01683 //     RefCounts* refCounts = from.refCounts_;
01684 //
01685 //     if (ptr != 0)
01686 //     {
01687 //         refCounts->totalRefs_.Increment();
01688 //     }
01689 //
01690 //     return ObjectWeakPtr<U> (ptr, refCounts);
01691 // }
01692 //
01693 // template <typename T, typename F>
01694 // ObjectWeakPtr<T> constCast (const ObjectWeakPtr<F>& from)
01695 // {
01696 //     if (from.GetPointer () == 0)
01697 //     {
01698 //         return ObjectWeakPtr<T>();
01699 //     }
01700 //
01701 //     T* ptr = const_cast <T*> (from.ptr_);
01702 //     RefCounts* refCounts = from.refCounts_;
01703 //
01704 //     if (ptr != 0)
01705 //     {
01706 //         //refCounts->totalRefs_.Increment();
01707 //         from._weak_reference_count->Increment();
01708 //     }
01709 //
01710 //     return ObjectWeakPtr<T> (ptr, refCounts);
01711 // }
01712 //
01713 // template <typename T, typename F>
01714 // ObjectWeakPtr<T> dynamicCast (const ObjectWeakPtr<F>& from)
01715 // {
01716 //     if (from.GetPointer () == 0)
01717 //     {
01718 //         return ObjectWeakPtr<T>();
01719 //     }
01720 //
01721 //     T* ptr = &dynamic_cast <T&> (*from.ptr_);
01722 //     RefCounts* refCounts = from.refCounts_;
01723 //
01724 //     if (ptr != 0)
01725 //     {
01726 //         //refCounts->totalRefs_.Increment();
01727 //         from._weak_reference_count->Increment();
01728 //     }
01729 //
01730 //     return ObjectWeakPtr<T> (ptr, refCounts);
01731 // }
01732 //
01733 // template <typename T, typename F>
01734 // ObjectWeakPtr<T> queryCast (const ObjectWeakPtr<F>& from)
01735 // {
01736 //     T* ptr = dynamic_cast <T*> (from.GetPointer ());
01737 //
01738 //     if (ptr == 0)
01739 //     {
01740 //         return ObjectWeakPtr<T>();
01741 //     }
01742 //
01743 //     RefCounts* refCounts = from.refCounts_;
01744 //
01745 //     if (ptr != 0)
01746 //     {
01747 //         //refCounts->totalRefs_.Increment();
01748 //         from._weak_reference_count->Increment();
01749 //     }
01750 //
01751 //     return ObjectWeakPtr<T> (ptr, refCounts);
01752 // }
01753 //
01754 // template <typename T, typename F>
01755 // ObjectWeakPtr<T> checkedCast (const ObjectWeakPtr<F>& from)
01756 // {
01757 //     if (from.GetPointer () == 0)
01758 //     {
01759 //         return ObjectWeakPtr<T>();
01760 //     }
01761 //
01762 //     nuxAssert(dynamic_cast<T*> (from.ptr_) != 0);
01763 //
01764 //     T* ptr = static_cast <T*> (from.ptr_);
01765 //     RefCounts* refCounts = from.refCounts_;
01766 //
01767 //     if (ptr != 0)
01768 //     {
01769 //         //refCounts->totalRefs_.Increment();
01770 //         from._weak_reference_count->Increment();
01771 //     }
01772 //
01773 //     return ObjectWeakPtr<T> (ptr, refCounts);
01774 // }
01775 
01776 // ///////////////////////////////////////////////////////
01777 // // std specializations
01778 //
01779 // template <typename T>
01780 // inline void swap (ObjectPtr<T>& t1, ObjectPtr<T>& t2)
01781 // {
01782 //     t1.swap (t2);
01783 // }
01784 //
01785 // template <typename T>
01786 // inline void swap (ObjectWeakPtr<T>& t1, ObjectWeakPtr<T>& t2)
01787 // {
01788 //     t1.swap (t2);
01789 // }
01790 
01791 
01792 }
01793 
01794 #endif // INTRUSIVESP_H
01795 
01796