一、RefBash.h
class RefBase
{
public:
void incStrong(const void* id) const;
void decStrong(const void* id) const;
int getStrongCount() const;
class weakref_type
{
public:
RefBase* refBase() const;
void incWeak(const void* id);
void decWeak(const void* id);
bool attemptIncStrong(const void* id);
int getWeakCount() const;
};
weakref_type* createWeak(const void* id) const;
weakref_type* getWeakRefs() const;
protected:
RefBase();
virtual ~RefBase();//delete this时候会调用子类
enum {
OBJECT_LIFETIME_WEAK = 0x0001,
OBJECT_LIFETIME_FOREVER = 0x0003
};
void extendObjectLifetime(int mode);
enum {
FIRST_INC_STRONG = 0x0001
};
virtual bool onIncStrongAttempted(int flags, const void* id);//子类可以覆盖
private:
//friend class weakref_type;
class weakref_impl; //不能include,只能前向声明
RefBase(const RefBase& o);
RefBase& operator=(const RefBase& o);
weakref_impl* const mRefs;
};
// ---------------------------------------------------------------------------
template
class sp
{
public:
typedef typename RefBase::weakref_type weakref_type;
//相当于typedef RefBase::weakref_type weakref_typ
sp(T* other);
sp(const sp
& other); ~sp(); inline T* get() const { return m_ptr; } private: template
friend class wp;//wp可以操作sp的私有变量,如构造函数 sp(T* p, weakref_type* refs); T* m_ptr; }; // --------------------------------------------------------------------------- template
class wp { public: typedef RefBase::weakref_type weakref_type; wp(T* other); wp(const wp
& other); ~wp(); sp
promote() const; inline weakref_type* get_refs() const { return m_refs; } private: T* m_ptr;//strongPointer weakref_type* m_refs;//weakref_impl }; // --------------------------------------------------------------------------- template
sp
::sp(T* other) : m_ptr(other)//strongPointer { if (other) other->incStrong(this); } template
sp
::sp(const sp
& other) : m_ptr(other.m_ptr) { if (m_ptr) m_ptr->incStrong(this); } template
sp
::~sp() { if (m_ptr) m_ptr->decStrong(this); } template
sp
::sp(T* p, weakref_type* refs) : m_ptr((p && refs->attemptIncStrong(this)) p : 0) { } // --------------------------------------------------------------------------- template
wp
::wp(T* other) : m_ptr(other)//strongPointer { if (other) m_refs = other->createWeak(this);//weakref_impl } template
wp
::wp(const wp
& other) : m_ptr(other.m_ptr), m_refs(other.m_refs) { if (m_ptr) m_refs->incWeak(this); } template
wp
::~wp() { if (m_ptr) m_refs->decWeak(this); } template
sp
wp
::promote() const { return sp
(m_ptr, m_refs); }
二、RefBase.cpp
#include RefBase.h #includeusing namespace std; #define INITIAL_STRONG_VALUE (1<<28) class RefBase::weakref_impl : public RefBase::weakref_type { public: volatile int mStrong; volatile int mWeak; RefBase* const mBase; volatile int mFlags; weakref_impl(RefBase* base) : mStrong(INITIAL_STRONG_VALUE) , mWeak(0) , mBase(base) , mFlags(0) { } ~weakref_impl() { cout << ~weakref_impl< incWeak(id); const int c = refs->mStrong++; if (c != INITIAL_STRONG_VALUE) { return; } refs->mStrong = refs->mStrong - INITIAL_STRONG_VALUE; } void RefBase::decStrong(const void* id) const { weakref_impl* const refs = mRefs; const int c = --refs->mStrong; if (c == 0) { if ((refs->mFlags&OBJECT_LIFETIME_WEAK) != OBJECT_LIFETIME_WEAK) { delete this; } } refs->decWeak(id); } int RefBase::getStrongCount() const { return mRefs->mStrong; } RefBase* RefBase::weakref_type::refBase() const { return static_cast (this)->mBase; } void RefBase::weakref_type::incWeak(const void* id) { weakref_impl* cons