Android应用程序框架——智能指针(二)

强弱指针的分析示例如下,不同于《Android应用程序框架——智能指针(一)》,我们这里不再把RefBase.h头文件拷贝到源代码目录下,而是通过Android.mk文件去指定包含目录。

源代码如下

weightpointer.cpp文件

#include<stdio.h>

#include<utils/RefBase.h>



#define INITIAL_STRONG_VAULE (1<<28)

using namespace android;



class WeightClass : public RefBase

{

public:

	void printRefCount(){

		int32_t strong = getStrongCount();

		weakref_type* ref = getWeakRefs();

		printf("---------------------\n");

		printf("Strong Ref Count: %d.\n", (strong == INITIAL_STRONG_VAULE ? 0 : strong));

		printf("Weak Ref Count: %d.\n", ref->getWeakCount());

		printf("---------------------\n");

	}

};





class StrongClass : public WeightClass

{

public:

	StrongClass(){

		printf("Construct StrongClass Object. \n");

	}

	virtual ~StrongClass(){

		printf("Destory StrongClass Object. \n");

	}

};



class WeakClass : public WeightClass

{

public:

	WeakClass(){

		extendObjectLifetime(OBJECT_LIFETIME_WEAK);

		printf("Construct WeakClass Object. \n");

	}

	virtual ~WeakClass(){

		printf("Destory WeakClass Object. \n");

	}

};







class ForeverClass : public WeightClass

{

public:

	ForeverClass(){

		extendObjectLifetime(OBJECT_LIFETIME_MASK);

		printf("Construct ForeverClass Object. \n");

	}

	virtual ~ForeverClass(){

		printf("Destory ForeverClass Object. \n");

	}

};





void TestStrongClass(StrongClass* pStrongClass){

	wp<StrongClass> wpOut = pStrongClass;

	pStrongClass->printRefCount();

	{

		sp<StrongClass> spInner = pStrongClass;

		pStrongClass->printRefCount();

	}

	sp<StrongClass> spOut = wpOut.promote();

	printf("spOut: %p.\n", spOut.get());

}





void TestWeakClass(WeakClass* pWeakClass){

	wp<WeakClass> wpOut = pWeakClass;

	pWeakClass->printRefCount();

	{

		sp<WeakClass> spInner = pWeakClass;

		pWeakClass->printRefCount();

	}

	pWeakClass->printRefCount();

	sp<WeakClass> spOut = wpOut.promote();

	printf("spOut: %p.\n", spOut.get());

}



void TestForeverClass(ForeverClass* pForeverClass){

	wp<ForeverClass> wpOut = pForeverClass;

	pForeverClass->printRefCount();

	{

		sp<ForeverClass> spInner = pForeverClass;

		pForeverClass->printRefCount();

	}

}



int main(int argc, char** argv){



	printf("Test Strong Class: \n");



	StrongClass* pStrongClass = new StrongClass();



	TestStrongClass(pStrongClass);





	printf("Test Weak Class: \n");

	WeakClass* pWeakClass = new WeakClass();

	TestWeakClass(pWeakClass);



	printf("Test Forever Class: \n");

	ForeverClass* pForeverClass = new ForeverClass();

	TestForeverClass(pForeverClass);



	pForeverClass->printRefCount();

	delete pForeverClass;





	return 0;

}


Android.mk文件

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := cpp_test_weightpointer
LOCAL_SRC_FILES := weightpointer.cpp
LOCAL_C_INCLUDES += /home/yong/dev/android-6.0/system/core/include
LOCAL_SHARED_LIBRARIES :=  \
	libcutils \
	libutils
include $(BUILD_EXECUTABLE)
运行结果:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值