<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                我們首先分析LightRefBase類的實現原理,它的定義如下所示。 **frameworks/base/include/utils/RefBase.h** ~~~ template <class T> class LightRefBase { public: inline LightRefBase() : mCount(0) { } inline void incStrong(const void* id) const { android_atomic_inc(&mCount); } inline void decStrong(const void* id) const { if (android_atomic_dec(&mCount) == 1) { delete static_cast<const T*>(this); } } //! DEBUGGING ONLY: Get current strong ref count. inline int32_t getStrongCount() const { return mCount; } protected: inline ~LightRefBase() { } private: mutable volatile int32_t mCount; }; ~~~ LightRefBase類是一個模板類,其中,模板參數T表示對象的實際類型,它必須是繼承了LightRefBase類的。LightRefBase類只有一個成員變量mCount,用來描述一個對象的引用計數值。LightRefBase類同時提供了成員函數incStrong和decStrong來增加和減少它所引用的對象的引用計數。 > 注意:在成員函數decStrong中,如果對象的引用計數值在減少之后變成0,那么就表示需要釋放這個對象所占用的內存了。 輕量級指針的實現類為sp,它同時也是強指針的實現類。在本節中,我們只關注它與輕量級指針相關的實現,它的定義如下所示。 **frameworks/base/include/utils/RefBase.h** ~~~ template <typename T> class sp { public: typedef typename RefBase::weakref_type weakref_type; inline sp() : m_ptr(0) { } sp(T* other); sp(const sp<T>& other); template<typename U> sp(U* other); template<typename U> sp(const sp<U>& other); ~sp(); // Assignment sp& operator = (T* other); sp& operator = (const sp<T>& other); template<typename U> sp& operator = (const sp<U>& other); template<typename U> sp& operator = (U* other); //! Special optimization for use by ProcessState (and nobody else). void force_set(T* other); // Reset void clear(); // Accessors inline T& operator* () const { return *m_ptr; } inline T* operator-> () const { return m_ptr; } inline T* get() const { return m_ptr; } // Operators COMPARE(==) COMPARE(!=) COMPARE(>) COMPARE(<) COMPARE(<=) COMPARE(>=) private: template<typename Y> friend class sp; template<typename Y> friend class wp; // Optimization for wp::promote(). sp(T* p, weakref_type* refs); T* m_ptr; }; ~~~ sp類也是一個模塊類,其中,模板參數T表示對象的實際類型,它也是必須繼承了LightRefBase類的。在sp類內部,使用RefBase::weakref_type類來維護它所引用的對象的強引用計數和弱引用計數,在3.2小節中分析強指針的實現原理時,我們再詳細分析它。 sp類的實現比較復雜,但是與輕量級指針相關的部分只有成員變量m_ptr以及構造函數和析構函數。不難看出,sp類的成員變量m_ptr是一個指針,它是在構造函數里面初始化的,指向實際引用的對象。接下來我們就分析sp類的構造函數和析構函數的實現。 sp類的構造函數有兩個版本,一個是普通的構造函數,一個是拷貝構造函數,如下所示。 **frameworks/base/include/utils/RefBase.h** ~~~ template<typename T> sp<T>::sp(T* other) : m_ptr(other) { if (other) other->incStrong(this); } template<typename T> sp<T>::sp(const sp<T>& other) : m_ptr(other.m_ptr) { if (m_ptr) m_ptr->incStrong(this); } ~~~ 在這兩個構造函數里面,首先都是初始化成員變量m_ptr,然后再調用它的成員函數incStrong來增加它的引用計數。由于成員變量m_ptr所指向的對象是從LightRefBase類繼承下來的,因此,這兩個構造函數實際上是調用了LightRefBase類的成員函數incStrong來增加對象的引用計數。 sp類的析構函數的實現如下所示。 **frameworks/base/include/utils/RefBase.h** ~~~ template<typename T> sp<T>::~sp() { if (m_ptr) m_ptr->decStrong(this); } ~~~ sp類的析構函數執行的操作剛好與構造函數相反,即調用成員變量m_ptr所指向的對象的成員函數decStrong來減少它的引用計數,實際上是調用LightRefBase類的成員函數decStrong來減少對象的引用計數。 至此,輕量級指針的實現原理就介紹完了。接下來我們通過一個實例來說明它的使用方法,以便加深對它的理解。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看