对android中c++常用的智能指针sp,wp结合 android4.2源代码进行了分析,使大家对它有一个更深入的了解。
智能指针:强指针sp,弱指针wp,轻量级指针LightRefBase。
相关文件:RefBase.h,RefBase.cpp,StrongPointer.h(注:参考代码android 4.2.2)。 RefBase.h:定义了RefBase类定义,wp模板类定义和实现,以及LightRefBase类定义。 RefBase.cpp:定义了RefBase类实现以及RefBase的嵌套类weakref_type的实现。 StrongPointer.h:定义了sp模板类定义和实现。
RefBase类主要方法如下:
void RefBase::incStrong(const void* id) const
{
weakref_impl* const refs = mRefs;
refs->incWeak(id); // 增加一次弱引用计数
refs->addStrongRef(id); // 空函数
// 原子操作,增加一次强引用计数,返回的是refs->mStrong执行加1操作之前的值 const int32_t c = android_atomic_inc(&refs->mStrong);
ALOG_ASSERT(c > 0, "incStrong() called on %p after last strong ref", refs);
// 第一次执行,c的值为INITIAL_STRONG_VALUE
if (c != INITIAL_STRONG_VALUE) { //从第二次开始执行后,此条件都成立,直接返回 return;
}
// 执行操作,refs->mStrong + (-INITIAL_STRONG_VALUE),第一次执行后强引用计数refs->mStrong值变为1
android_atomic_add(-INITIAL_STRONG_VALUE, &refs->mStrong);
refs->mBase->onFirstRef(); //第一次执行会调用该方法,子类可以覆盖该方法。 }
void RefBase::decStrong(const void* id) const
{
weakref_impl* const refs = mRefs;
refs->removeStrongRef(id); // 空函数
// 原子操作,强引用计数减1,返回的是执行减1操作之前的值
const int32_t c = android_atomic_dec(&refs->mStrong);
ALOG_ASSERT(c >= 1, "decStrong() called on %p too many times", refs);
if (c == 1) {
refs->mBase->onLastStrongRef(id); // 子类可覆盖该方法
// mFlags值缺省为0
if ((refs->mFlags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_STRONG) {
delete this;
}
}
refs->decWeak(id); // 弱引用计数减1
}
void RefBase::forceIncStrong(const void* id) const
{
android智能指针sp和wp介绍_互联网_IT/计算机_专业资料。主要介绍sp、wp和RefBase...m_refs 其实指向 RefBase 的 mRefs 的成员变量 1.2 总结(1)强引用与弱引用...
强指针和弱指针 在 Android 的源代码中,经常会看到形如:sp<xxx>、wp<xxx>这样的类型定义,这其实是 Android 中的智能指针。智能指针是 C++中的一个概念,通过...
android智能指针spwp 隐藏>> 研究Android 的时候,经常会遇到 sp、wp 的东西,网...总结一下: 一个目标对象,可能被 n 个 sp 强指针指向,那么就存在 n 个 ...
android智能指针sp和wp介绍... 13页 2财富值 android...了解了这些背景知识后,我们就可以进一步学习 Android ...这里总结一下强指针类 sp 在其构造函数里面所做的...
Android中的sp和wp指针_IT/计算机_专业资料。Android 中的 sp 和 wp 指针经常会在 android 的 framework 代码中发现 sp<xxx>和 wp<xxx>这样的指针, 平时看的...
这些 C++代码用到了很多智能指针,因此有必要了解一下 Android 系统在 C/C++ ...Android画图学习总结 59页 7下载券 Android_项目报告总结 20页 2下载券 android...
android 智能机介绍及培训 24页 免费 android智能指针(wp、sp)学... 暂无评价...virtual ~MyClass(); 满足了上述条件的类就可以定义 Android 智能指针了, 定义...
Hspice语言学习总结_学习总结_总结/汇报_应用文书。HSpice...op run.sp A SIMPLE AC RUN V1 1 0 10 AC ...wp 4u 12u 2u .print dc v(in) v(out) ....
培训_学习总结_总结/汇报_实用文档。优风易中央空气.../h WP-350D 风量350m? /h 主机尺寸 初效、中...155×85 主控系统 主控功能 android系统 新风系统软件...
扫描二维码下载 支持Android / iPhone / iPad文档信息...Cb-xwowp毕业实习总结报告Cb-xwowp毕业实习总结报告...谈生活学习以及未来的工作,通过这些我就和老 师们...
热门文档