1
0
mirror of https://github.com/ioacademy-jikim/android_framwork synced 2025-06-07 07:56:34 +00:00
This commit is contained in:
ioacademy-jikim 2018-06-14 11:35:54 +09:00
parent af0342feb7
commit f25774ff2e
5 changed files with 157 additions and 0 deletions

Binary file not shown.

7
03_day/sp/Android.bp Normal file
View File

@ -0,0 +1,7 @@
cc_binary {
name: "my_sp",
shared_libs: ["liblog", "libutils"],
srcs: [
"sp_1.cpp",
],
}

Binary file not shown.

View File

@ -1,6 +1,74 @@
#if 1
#include <stdio.h>
class RefBase
{
int mRefs;
public:
RefBase():mRefs(0) { printf("RefBase::RefBase()\n"); }
virtual ~RefBase() { printf("RefBase::~RefBase()\n"); }
void incStrong() {
printf("RefBase::incStrong() : mRefs=%d\n", ++mRefs);
}
void decStrong()
{
--mRefs;
printf("RefBase::decStrong() : mRefs=%d\n", mRefs);
if( mRefs == 0 )
delete this;
}
};
class AAA : public virtual RefBase
{
public:
AAA(){ printf("AAA::AAA()\n"); }
~AAA() { printf("AAA::~AAA()\n"); }
void foo() { printf("AAA::foo()\n"); }
};
template < typename T >
class sp
{
T *mPtr;
public:
sp( T *ptr):mPtr(ptr) {
printf("sp::sp(T)\n");
mPtr->incStrong();
}
sp( const sp<T> &r ) : mPtr(r.mPtr) {
printf("sp::sp(sp<T>)\n");
mPtr->incStrong();
}
~sp() {
printf("sp::~sp()\n");
mPtr->decStrong();
}
T * operator->()
{
return mPtr;
}
T operator*()
{
return *mPtr;
}
};
int main()
{
{
sp<AAA> p1 = new AAA;
sp<AAA> p2 = p1;
p1->foo();
p2->foo();
}
return 0;
}
#endif
#if 0
#include <stdio.h>
class AAA
{
int mRefs;

82
03_day/sp/sp_1.cpp Normal file
View File

@ -0,0 +1,82 @@
#if 1
#include <stdio.h>
#include <utils/RefBase.h>
#include <utils/StrongPointer.h>
using namespace android;
class AAA;
class BBB;
class AAA : public virtual RefBase
{
public:
wp<BBB> pb;
AAA(){ printf("AAA::AAA()\n"); }
~AAA() { printf("AAA::~AAA()\n"); }
void foo() { printf("AAA::foo()\n"); }
};
class BBB : public virtual RefBase
{
public:
wp<AAA> pa;
BBB(){ printf("BBB::BBB()\n"); }
~BBB() { printf("BBB::~BBB()\n"); }
void foo() { printf("BBB::foo()\n"); }
};
int main()
{
{
sp<AAA> p1 = new AAA;
sp<BBB> p2 = new BBB;
p1->pb = p2;
p2->pa = p1;
}
return 0;
}
#endif
#if 0
#include <stdio.h>
#include <utils/RefBase.h>
#include <utils/StrongPointer.h>
using namespace android;
class AAA;
class BBB;
class AAA : public virtual RefBase
{
public:
sp<BBB> pb;
AAA(){ printf("AAA::AAA()\n"); }
~AAA() { printf("AAA::~AAA()\n"); }
void foo() { printf("AAA::foo()\n"); }
};
class BBB : public virtual RefBase
{
public:
sp<AAA> pa;
BBB(){ printf("BBB::BBB()\n"); }
~BBB() { printf("BBB::~BBB()\n"); }
void foo() { printf("BBB::foo()\n"); }
};
int main()
{
{
sp<AAA> p1 = new AAA;
sp<BBB> p2 = new BBB;
p1->pb = p2;
p2->pa = p1;
}
return 0;
}
#endif