Android 智能指针详解

阅读: 评论:0

Android 智能指针详解

Android 智能指针详解

源码基于:Android N

相关博文:

Android 智能指针详解

Android 智能指针详解 -- wp 

Android 智能指针详解 -- sp

Android 智能指针详解 -- RefBase

0. 前言

上一篇关于 sp的博文,通过 source code 来解释了sp的功能,sp被称为强指针,但是强引用更贴切点。那么弱引用wp到底有什么功能?与sp有什么区别?这一篇通过 source code 来解释。

1. 源码解析

system/core/include/utils/RefBase.htemplate <typename T>
class wp
{
public:typedef typename RefBase::weakref_type weakref_type;inline wp() : m_ptr(0) { }wp(T* other);wp(const wp<T>& other);wp(const sp<T>& other);template<typename U> wp(U* other);template<typename U> wp(const sp<U>& other);template<typename U> wp(const wp<U>& other);~wp();// Assignmentwp& operator = (T* other);wp& operator = (const wp<T>& other);wp& operator = (const sp<T>& other);template<typename U> wp& operator = (U* other);template<typename U> wp& operator = (const wp<U>& other);template<typename U> wp& operator = (const sp<U>& other);void set_object_and_refs(T* other, weakref_type* refs);// promotion to spsp<T> promote() const;// Resetvoid clear();// Accessorsinline  weakref_type* get_refs() const { return m_refs; }inline  T* unsafe_get() const { return m_ptr; }// OperatorsCOMPARE_WEAK(==)COMPARE_WEAK(!=)COMPARE_WEAK(>)COMPARE_WEAK(<)COMPARE_WEAK(<=)COMPARE_WEAK(>=)inline bool operator == (const wp<T>& o) const {return (m_ptr == o.m_ptr) && (m_refs == o.m_refs);}template<typename U>inline bool operator == (const wp<U>& o) const {return m_ptr == o.m_ptr;}inline bool operator > (const wp<T>& o) const {return (m_ptr == o.m_ptr) ? (m_refs > o.m_refs) : (m_ptr > o.m_ptr);}template<typename U>inline bool operator > (const wp<U>& o) const {return (m_ptr == o.m_ptr) ? (m_refs > o.m_refs) : (m_ptr > o.m_ptr);}inline bool operator < (const wp<T>& o) const {return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr);}template<typename U>inline bool operator < (const wp<U>& o) const {return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr);}inline bool operator != (const wp<T>& o) const { return m_refs != o.m_refs; }template<typename U> inline bool operator != (const wp<U>& o) const { return !operator == (o); }inline bool operator <= (const wp<T>& o) const { return !operator > (o); }template<typename U> inline bool operator <= (const wp<U>& o) const { return !operator > (o); }inline bool operator >= (const wp<T>& o) const { return !operator < (o); }template<typename U> inline bool operator >= (const wp<U>& o) const { return !operator < (o); }private:template<typename Y> friend class sp;template<typename Y> friend class wp;T*              m_ptr;weakref_type*   m_refs;
};

1.1 通过定义与sp比较

  • 都是模板类
  • 都有7个构造函数和6个赋值重载函数
  • 因为多了成员变量m_refs,所以,比较的运算符进行了重载,sp中没有m_refs,所以直接用宏COMPARE_WEAK
  • 都有指针T* m_ptr
  • wp多了一个promote函数,为了向sp转换
  • wp并没有sp中重载运算符 * 和运算符 ->

1.2 构造函数和析构函数

template<typename T>
wp<T>::wp(T* other): m_ptr(other)
{if (other) m_refs = other->createWeak(this);
}template<typename T>
wp<T>::wp(const wp<T>& other): m_ptr(other.m_ptr), m_refs(other.m_refs)
{if (m_ptr) m_refs->incWeak(this);
}template<typename T>
wp<T>::wp(const sp<T>& other): m_ptr(other.m_ptr)
{if (m_ptr) {m_refs = m_ptr->createWeak(this);}
}template<typename T> template<typename U>
wp<T>::wp(U* other): m_ptr(other)
{if (other) m_refs = other->createWeak(this);
}template<typename T> template<typename U>
wp<T>::wp(const wp<U>& other): m_ptr(other.m_ptr)
{if (m_ptr) {m_refs = other.m_refs;m_refs->incWeak(this);}
}template<typename T> template<typename U>
wp<T>::wp(const sp<U>& other): m_ptr(other.m_ptr)
{if (m_ptr) {m_refs = m_ptr->createWeak(this);}
}template<typename T>
wp<T>::~wp()
{if (m_ptr) m_refs->decWeak(this);
}

从构造函数可以看出来几点与sp不同的地方:

  • 并不是像 sp那样通过incStrong 和decStrong方式控制计数,而是通过createWeak 、incWeak、decWeak来控制,具体这几个函数是做什么的?与sp的两种函数有什么区别?后面解释。
  • 并没有像 sp那样有移动构造函数,为什么呢?

1.3 赋值运算符的重载

template<typename T>
wp<T>& wp<T>::operator = (T* other)
{weakref_type* newRefs =other ? other->createWeak(this) : 0;//wp 中指针other通过createWeak创建m_refsif (m_ptr) m_refs->decWeak(this); //原来的m_refs需要decWeakm_ptr = other;m_refs = newRefs;return *this;
}template<typename T>
wp<T>& wp<T>::operator = (const wp<T>& other)
{weakref_type* otherRefs(other.m_refs);T* otherPtr(other.m_ptr);if (otherPtr) otherRefs->incWeak(this);if (m_ptr) m_refs->decWeak(this);m_ptr = otherPtr;m_refs = otherRefs;return *this;
}template<typename T>
wp<T>& wp<T>::operator = (const sp<T>& other)
{weakref_type* newRefs =other != NULL ? other->createWeak(this) : 0;//同形参为T* other,需要createWeakT* otherPtr(other.m_ptr);if (m_ptr) m_refs->decWeak(this);m_ptr = otherPtr;m_refs = newRefs;return *this;
}template<typename T> template<typename U>
wp<T>& wp<T>::operator = (U* other)
{weakref_type* newRefs =other ? other->createWeak(this) : 0;if (m_ptr) m_refs->decWeak(this);m_ptr = other;m_refs = newRefs;return *this;
}template<typename T> template<typename U>
wp<T>& wp<T>::operator = (const wp<U>& other)
{weakref_type* otherRefs(other.m_refs);U* otherPtr(other.m_ptr);if (otherPtr) otherRefs->incWeak(this);if (m_ptr) m_refs->decWeak(this);m_ptr = otherPtr;m_refs = otherRefs;return *this;
}template<typename T> template<typename U>
wp<T>& wp<T>::operator = (const sp<U>& other)
{weakref_type* newRefs =other != NULL ? other->createWeak(this) : 0;U* otherPtr(other.m_ptr);if (m_ptr) m_refs->decWeak(this);m_ptr = otherPtr;m_refs = newRefs;return *this;
}

通过赋值运算符重载看到,一般的指针在使用wp时,需要createWeak,如果已经是wp引用,直接incWeak
 

1.4 promote()

template<typename T>
sp<T> wp<T>::promote() const
{sp<T> result;if (m_ptr && m_refs->attemptIncStrong(&result)) {result.set_pointer(m_ptr);}return result;
}

2. 遗留问题

  • wp 中m_refs 到底是什么意思?
  • 构造或者是赋值的时候,为什么指针用的是createWeak,而m_refs 用的是incWeak 和decWeak,又有什么作用?
  • wp称为弱指针也可以,确切的看并没有看到指针使用的地方,那wp如何使用?
  • sp的博文中遗留的问题decStrong 和incStrong的具体实现是什么?为什么控制指针的计数?

这些问题需要看下RefBase 就全然明白。

 相关博文:

Android 智能指针详解

Android 智能指针详解 -- wp 

Android 智能指针详解 -- sp

Android 智能指针详解 -- RefBase

本文发布于:2024-01-30 03:11:20,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170655548018811.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:指针   详解   智能   Android
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23