C++之Android弱指针

RefBase

在头文件RefBase.h中实现RefBase类。

class RefBase                                                                      
{                                                                                  
public:    
..........

    class weakref_type                                                          
    {                                                                           
    public:                                                                     
        RefBase*            refBase() const;                                    
        void                incWeak(const void* id);                            
        void                decWeak(const void* id);                                                                                                                        
        // acquires a strong reference if there is already one.                 
        bool                attemptIncStrong(const void* id);                   
        // acquires a weak reference if there is already one.                   
        // This is not always safe. see ProcessState.cpp and BpBinder.cpp       
        // for proper use.                                                      
        bool                attemptIncWeak(const void* id);                     
        //! DEBUGGING ONLY: Get current weak ref count.                         
        int32_t             getWeakCount() const;                               
        //! DEBUGGING ONLY: Print references held on object.                    
        void                printRefs() const;  
        };
        
            weakref_type*   createWeak(const void* id) const;                   
            weakref_type*   getWeakRefs() const;  
            protected:                                                                      
                            RefBase();                                          
    virtual                 ~RefBase(); 

private:
         class weakref_impl; 

private:
          weakref_impl* const mRefs;
  }

可以看出在RefBase类中,有两个内部类weakref_type和weakref_impl。

类weakref_type中封装了函数。
类weakref_impl,继承了weakref_type,还增加了mWeak变量。

weakref_impl类

在RefBase.cpp中实现weakref_impl类。

class RefBase::weakref_impl : public RefBase::weakref_type                         
{                                                                                  
public:                                                                            
    volatile int32_t    mStrong;                                                   
    volatile int32_t    mWeak;                                                     
    RefBase* const      mBase;                                                     
    volatile int32_t    mFlags;   
发布了115 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/chengbeng1745/article/details/104072009