如果此时强指针不为0,或者初始值,那么说明RefBase及其子类还没有删除,所以可以提升为强指针
如果此时强指针为0,并且还受强指针控制,那么此时,RefBase及其子类已经被删除了,所以不能提升为强指针
如果此时强指针为0,并且还受弱指针控制,那么此时,RefBase及其子类还没有删除,能不能提升就看impl->mBase->onIncStrongAttempted(FIRST_INC_STRONG, id)的了
如果此时强指针为INITIAL_STRONG_VALUE,并且还受强指针控制,此时RefBase及其子类还没有删除,所以能够提升。
如果此时强指针为INITIAL_STRONG_VALUE,并且还受弱指针控制,此时RefBase及其子类还没有删除,能不能提升就看impl->mBase->onIncStrongAttempted(FIRST_INC_STRONG, id)的了
三、main.cpp
#include#include RefBase.h #define INITIAL_STRONG_VALUE (1<<28) class WeightClass: public RefBase { public: void printRefCount() { int strong = getStrongCount(); weakref_type* ref = getWeakRefs(); printf(----------------------- ); printf(Strong Ref Count: %d. , (strong == INITIAL_STRONG_VALUE 0 : strong)); printf(Weak Ref Count: %d. , ref->getWeakCount()); printf(----------------------- ); } }; class StrongClass: public WeightClass { public: StrongClass() { printf(Construct StrongClass Object. ); } virtual ~StrongClass() { printf(Destory StrongClass Object. ); } }; class WeakClass: public WeightClass { public: WeakClass() { extendObjectLifetime(OBJECT_LIFETIME_WEAK); printf(Construct WeakClass Object. ); } virtual ~WeakClass() { printf(Destory WeakClass Object. ); } }; class ForeverClass: public WeightClass { public: ForeverClass() { extendObjectLifetime(OBJECT_LIFETIME_FOREVER); printf(Construct ForeverClass Object. ); } virtual ~ForeverClass() { printf(Destory ForeverClass Object. ); } }; void TestStrongClass(StrongClass* pStrongClass) { wp wpOut = pStrongClass; pStrongClass->printRefCount(); { sp spInner = pStrongClass; pStrongClass->printRefCount(); } pStrongClass->printRefCount(); sp spOut = wpOut.promote(); printf(spOut: %p. , spOut.get()); pStrongClass->printRefCount(); } void TestStrongClass2(StrongClass* pStrongClass) { wp wpOut = pStrongClass; pStrongClass->printRefCount(); } void TestStrongClass4(StrongClass* pStrongClass) { wp wpOut = pStrongClass; pStrongClass->printRefCount(); sp spInner = pStrongClass; pStrongClass->printRefCount(); sp spOut = wpOut.promote(); printf(spOut: %p. , spOut.get()); pStrongClass->printRefCount(); } void TestStrongClass3(StrongClass* pStrongClass) { wp wpOut = pStrongClass; pStrongClass->printRefCount(); sp spOut = wpOut.promote(); printf(spOut: %p. , spOut.get()); pStrongClass->printRefCount(); } void TestWeakClass(WeakClass* pWeakClass) { wp wpOut = pWeakClass; pWeakClass->printRefCount(); { sp spInner = pWeakClass; pWeakClass->printRefCount(); } pWeakClass->printRefCount(); sp spOut = wpOut.promote(); printf(spOut: %p. , spOut.get()); pWeakClass->printRefCount(); } void TestForeverClass(ForeverClass* pForeverClass) { wp wpOut = pForeverClass; pForeverClass->printRefCount(); { sp spInner = pForeverClass; pForeverClass->printRefCount(); } pForeverClass->printRefCount(); } int main(int argc, char** argv) { /* printf(Test Strong Class: ); StrongClass* pStrongClass = new StrongClass(); TestStrongClass(pStrongClass);*/ /* printf(Test Strong Class2: ); StrongClass* pStrongClass = new StrongClass(); TestStrongClass2(pStrongClass);*/ /* printf( Test Weak Class: ); WeakClass* pWeakClass = new WeakClass(); TestWeakClass(pWea