This commit is contained in:
ioacademy-jikim
2018-06-15 10:55:16 +09:00
parent f3d74671fa
commit 7a0a2db279
53 changed files with 3849 additions and 0 deletions
+7
View File
@@ -0,0 +1,7 @@
cc_binary {
name: "my_wp",
shared_libs: ["liblog", "libutils"],
srcs: [
"wp_1.cpp",
],
}
BIN
View File
Binary file not shown.
+409
View File
@@ -0,0 +1,409 @@
#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;
public:
AAA():mRefs(0) { printf("AAA::AAA()\n"); }
~AAA() { printf("AAA::~AAA()\n"); }
void foo() { printf("AAA::foo()\n"); }
void incStrong() {
printf("AAA::incStrong() : mRefs=%d\n", ++mRefs);
}
void decStrong()
{
--mRefs;
printf("AAA::decStrong() : mRefs=%d\n", mRefs);
if( mRefs == 0 )
delete this;
}
};
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
{
public:
AAA() { printf("AAA::AAA()\n"); }
~AAA() { printf("AAA::~AAA()\n"); }
void foo() { printf("AAA::foo()\n"); }
};
template < typename T >
class sp
{
T *mPtr;
static int mRefs;
public:
sp( T *ptr):mPtr(ptr) {
mRefs=1;
printf("sp::sp(T), mRefs=%d\n", mRefs);
}
sp( const sp<T> &r ) {
mRefs++;
printf("sp::sp(sp<T>), mRefs=%d\n", mRefs);
}
~sp() {
printf("sp::~sp()\n");
if( --mRefs == 0 )
delete mPtr;
}
T * operator->()
{
return mPtr;
}
T operator*()
{
return *mPtr;
}
};
template < typename T >
int sp<T>::mRefs = 0;
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
{
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()\n"); }
~sp() {
printf("sp::~sp()\n");
delete mPtr;
}
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
{
public:
AAA() { printf("AAA::AAA()\n"); }
~AAA() { printf("AAA::~AAA()\n"); }
void foo() { printf("AAA::foo()\n"); }
};
class BBB
{
public:
BBB() { printf("BBB::BBB()\n"); }
~BBB() { printf("BBB::~BBB()\n"); }
void foo() { printf("BBB::foo()\n"); }
};
template < typename T >
class sp
{
T *mPtr;
public:
sp( T *ptr):mPtr(ptr) { printf("sp::sp()\n"); }
~sp() {
printf("sp::~sp()\n");
delete mPtr;
}
T * operator->()
{
return mPtr;
}
T operator*()
{
return *mPtr;
}
};
int main()
{
{
sp<AAA> pa = new AAA;
pa->foo();
sp<BBB> pb = new BBB;
pb->foo();
}
return 0;
}
#endif
#if 0
#include <stdio.h>
class AAA
{
public:
AAA() { printf("AAA::AAA()\n"); }
~AAA() { printf("AAA::~AAA()\n"); }
void foo() { printf("AAA::foo()\n"); }
};
class sp
{
AAA *mPtr;
public:
sp( AAA *ptr):mPtr(ptr) { printf("sp::sp()\n"); }
~sp() {
printf("sp::~sp()\n");
delete mPtr;
}
AAA * operator->()
{
return mPtr;
}
AAA operator*()
{
return *mPtr;
}
};
int main()
{
{
sp pa = new AAA;
pa->foo(); // pa.operator->()->foo();
(*pa).foo();
}
return 0;
}
#endif
#if 0
#include <stdio.h>
class AAA
{
public:
AAA() { printf("AAA::AAA()\n"); }
~AAA() { printf("AAA::~AAA()\n"); }
void foo() { printf("AAA::foo()\n"); }
};
class sp
{
AAA *mPtr;
public:
sp( AAA *ptr):mPtr(ptr) { printf("sp::sp()\n"); }
~sp() {
printf("sp::~sp()\n");
delete mPtr;
}
};
int main()
{
{
//AAA* pa = new AAA;
//pa->foo();
sp pa = new AAA;
pa->foo(); // pa.operator->()->foo();
}
return 0;
}
#endif
#if 0
#include <stdio.h>
class AAA
{
public:
AAA() { printf("AAA::AAA()\n"); }
~AAA() { printf("AAA::~AAA()\n"); }
};
int main()
{
AAA *pa = new AAA;
//...
return 0;
}
#endif
#if 0
#include <stdio.h>
class AAA
{
public:
AAA() { printf("AAA::AAA()\n"); }
~AAA() { printf("AAA::~AAA()\n"); }
};
int main()
{
AAA *pa = new AAA;
delete pa;
//...
return 0;
}
#endif
#if 0
#include <stdio.h>
class AAA
{
public:
AAA() { printf("AAA::AAA()\n"); }
~AAA() { printf("AAA::~AAA()\n"); }
};
int main()
{
AAA aaa;
return 0;
}
#endif
+82
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
+119
View File
@@ -0,0 +1,119 @@
#if 1
#include <stdio.h>
#include <utils/RefBase.h>
#include <utils/StrongPointer.h>
using namespace android;
class AAA : public virtual RefBase
{
int data;
public:
AAA(){ printf("AAA::AAA()\n"); }
~AAA() { printf("AAA::~AAA()\n"); }
void foo() // void foo( AAA *this )
{
data = 10; // this->data = 10; => (*0).data = 10;
printf("AAA::foo()\n");
}
};
int main()
{
//((AAA*)0)->foo(); // AAA::foo(0);
{
wp<AAA> p1 = new AAA;
{
sp<AAA> p2 = p1.promote();
p2->foo();
}
printf("step 1.\n");
if( p1.promote() != 0 )
p1.promote()->foo();
else
printf("객체는 이미 소멸됨\n");
}
printf("step 2.\n");
return 0;
}
#endif
#if 0
#include <stdio.h>
#include <utils/RefBase.h>
#include <utils/StrongPointer.h>
using namespace android;
class AAA : public virtual RefBase
{
public:
AAA(){ printf("AAA::AAA()\n"); }
~AAA() { printf("AAA::~AAA()\n"); }
void foo() { printf("AAA::foo()\n"); }
};
int main()
{
{
wp<AAA> p1 = new AAA;
sp<AAA> p2 = p1.promote();
p2->foo();
}
return 0;
}
#endif
#if 0
#include <stdio.h>
#include <utils/RefBase.h>
#include <utils/StrongPointer.h>
using namespace android;
class AAA : public virtual RefBase
{
public:
AAA(){ printf("AAA::AAA()\n"); }
~AAA() { printf("AAA::~AAA()\n"); }
void foo() { printf("AAA::foo()\n"); }
};
int main()
{
{
sp<AAA> p1 = new AAA;
wp<AAA> p2 = p1;
p1->foo();
}
return 0;
}
#endif
#if 0
#include <stdio.h>
#include <utils/RefBase.h>
#include <utils/StrongPointer.h>
using namespace android;
class AAA : public virtual RefBase
{
public:
AAA(){ printf("AAA::AAA()\n"); }
~AAA() { printf("AAA::~AAA()\n"); }
void foo() { printf("AAA::foo()\n"); }
};
int main()
{
{
wp<AAA> pa = new AAA;
pa.promote()->foo();
}
return 0;
}
#endif