mirror of
https://github.com/ioacademy-jikim/android_framwork
synced 2025-06-07 16:06:29 +00:00
4
This commit is contained in:
parent
af0342feb7
commit
f25774ff2e
BIN
03_day.pptx
BIN
03_day.pptx
Binary file not shown.
7
03_day/sp/Android.bp
Normal file
7
03_day/sp/Android.bp
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
cc_binary {
|
||||||
|
name: "my_sp",
|
||||||
|
shared_libs: ["liblog", "libutils"],
|
||||||
|
srcs: [
|
||||||
|
"sp_1.cpp",
|
||||||
|
],
|
||||||
|
}
|
BIN
03_day/sp/a.out
BIN
03_day/sp/a.out
Binary file not shown.
@ -1,6 +1,74 @@
|
|||||||
#if 1
|
#if 1
|
||||||
#include <stdio.h>
|
#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
|
class AAA
|
||||||
{
|
{
|
||||||
int mRefs;
|
int mRefs;
|
||||||
|
82
03_day/sp/sp_1.cpp
Normal file
82
03_day/sp/sp_1.cpp
Normal 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
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user