Android中wp promote为sp流程

几个相关class的定义位置:

system/core$ vim include/utils/RefBase.h 其中定义了class wp
system/core/libutils/RefBase.cpp 其中定义了class RefBase::weakref_impl

system/core$ vim include/utils/StrongPointer.h 其中定义了class sp

以以下代码运行为例:

链接:http://blog.csdn.net/mfbao01/article/details/6255655

class WPTest : public RefBase {
public:
    WPTest(){
        LOGD("WPTest constructor");
    }
    virtual ~WPTest() {
        LOGD("WPTest destructor");
    }

    virtual void onFirstRef() {
        LOGD("first weak ptr ref callback");
    }

    virtual void onLastStrongRef(const void* id) {
        LOGD("last strong ptr ref callback");
    }

    virtual void onLastWeakRef(const void* id) {
        LOGD("last weak ptr ref callback");
    }
};


int main()
{
    WPTest *T = new WPTest();
    {
        wp<WPTest> weakp(T);

        {
            LOGD("promote to strong ptr.../n");

            sp<WPTest> strongp = weakp.promote();

            LOGD("strong ptr's lifetime is just about to finish .../n");
        }

        LOGD("weak ptr's lifetime is just about to finish .../n");
    }

    LOGD("weak ptr is out of scope./n");

    return 0;
}

第一步:调用wp构造方法

system/core/include/utils/RefBase.h

template<typename T> template<typename U>
wp<T>::wp(U* other)
    : m_ptr(other)
{
    if (other) m_refs = other->createWeak(this);
}
m_refs为weakref_type类型的指针

上一步调了实例对像W

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值