/*
一、本文中包括以下几个类:
RefCountedBase
-->为子类增加引用计数,基类的基类,无线程安全
RefCountedThreadSafeBase
-->为子类增加引用计数,基类的基类,有线程安全
RefCounted:public RefCountedBase
-->为子类增加引用计数,基类,线程安全
RefCountedThreadSafe:public RefCountedThreadSafeBase
-->为子类增加引用计数,基类,线程安全
DefaultRefCountedThreadSafeTraits
-->为RefCountedThreadSafe提供析构支持
RefCountedData : public base::RefCounted< base::RefCountedData<T> >
-->为其它数据增加引用计数
scoped_refptr
-->自身有作用域的、其对象带引用计数的 智能指针。出作用域就析构,
释放自己对对象的引用。
*/
#ifndef BASE_MEMORY_REF_COUNTED_H_
#define BASE_MEMORY_REF_COUNTED_H_
#include <cassert>
#include "base/atomic_ref_count.h"
#include "base/base_export.h"
#include "base/compiler_specific.h"
#include "base/threading/thread_collision_warner.h"
namespace base {
namespace subtle {
class BASE_EXPORT RefCountedBase {
public://仅有一个引用时返回true
bool HasOneRef() const { return ref_count_ == 1; }
protected:
RefCountedBase();
~RefCountedBase();
void AddRef() const;//增加引用计数
// Returns true if the object should self-delete.
bool Release() const;//减少引用计数
private:
mutable int ref_count_;
#ifndef NDEBUG
mutable bool in_dtor_;
#endif
DFAKE_MUTEX(add_release_);
DISALLOW_COPY_AND_ASSIGN(RefCountedBase);
};
class BASE_EXPORT RefCountedThreadSafeBase {
public:
bool HasOneRef() const;//仅有一个引用时返回true
protected:
RefCountedThreadSafeBase();
~RefCountedThreadSafeBase();
void AddRef() const;//增加引用计数,原子地+=1
// Returns true if the object should self-delete.
bool Release() const;//增加引用计数,原子地-=1
private:
mutable AtomicRefCount ref_count_;
#ifndef NDEBUG
mutable bool in_dtor_;
#endif
DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafeBase);
};
} // namespace subtle
/*
对父类接口进行包装,在release时释放自己的内存
*/
template <class T>
class RefCounted : public subtle::RefCountedBase {
public:
RefCounted() {}
void AddRef() const {//直接调用父类的
subtle::RefCountedBase::AddRef();
}
void Release() const {
if (subtle::RefCountedBase::Release()) {
//释放内存,引用为0时父类Release返回True
//会释放内存
delete static_cast<const T*>(this);
}
}
protected:
~RefCounted() {}
private:
DISALLOW_COPY_AND_ASSIGN(RefCounted<T>);
};
// Forward declaration.
template <class T, typename Traits> class RefCountedThreadSafe;
/*
在多线程情况下,用户可通过Traits来确定对象release时的操作,而
下面这个结构是默认的操作,它将调用RefCountedThreadSafe的
DeleteInternal来删除RefCountedThreadSafe子类对象,这个有点绕。
*/
template<typename T>
struct DefaultRefCountedThreadSafeTraits {
static void Destruct(const T* x) {
RefCountedThreadSafe<T,
DefaultRefCountedThreadSafeTraits>::DeleteInternal(x);
}
};
template <class T, typename Traits = DefaultRefCountedThreadSafeTraits<T> >
class RefCountedThreadSafe : public subtle::RefCountedThreadSafeBase {
public:
RefCountedThreadSafe() {}
void AddRef() const {
//直接调用父类的
subtle::RefCountedThreadSafeBase::AddRef();
}
void Release() const {
//先调用父类的,当引用为0时父类Release函数会返回True
//这样的话,会调用释放内存的函数,同时用户可以通过
//Traits来参与进来,决定内存怎么释放。
if (subtle::RefCountedThreadSafeBase::Release()) {
Traits::Destruct(static_cast<const T*>(this));
}
}
protected:
~RefCountedThreadSafe() {}
private:
friend struct DefaultRefCountedThreadSafeTraits<T>;
static void DeleteInternal(const T* x) { delete x; }
DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafe);
};
//
// A wrapper for some piece of data so we can place other things in
// scoped_refptrs<>.
// 给一些其它数据增加计数引用
template<typename T>
class RefCountedData : public base::RefCounted< base::RefCountedData<T> > {
public:
RefCountedData() : data() {}
RefCountedData(const T& in_value) : data(in_value) {}
T data;
private:
friend class base::RefCounted<base::RefCountedData<T> >;
~RefCountedData() {}
};
} // namespace base
/*
scoped_refptr智能指针
*/
template <class T>
class scoped_refptr {
public:
typedef T element_type;
scoped_refptr() : ptr_(NULL) {//构造
}
scoped_refptr(T* p) : ptr_(p) {//构造
if (ptr_)
ptr_->AddRef();//子类必须是能引用计数的
}
//拷贝构造
scoped_refptr(const scoped_refptr<T>& r) : ptr_(r.ptr_) {
if (ptr_)
ptr_->AddRef();
}
//拷贝构造
template <typename U>
scoped_refptr(const scoped_refptr<U>& r) : ptr_(r.get()) {
if (ptr_)
ptr_->AddRef();
}
//析构,释放本指针的引用,导致对象引用-=1
~scoped_refptr() {
if (ptr_)
ptr_->Release();
}
//返回对象
T* get() const { return ptr_; }
operator T*() const { return ptr_; }
T* operator->() const {
assert(ptr_ != NULL);
return ptr_;
}
// Release a pointer.
// The return value is the current pointer held by this object.
// If this object holds a NULL pointer, the return value is NULL.
// After this operation, this object will hold a NULL pointer,
// and will not own the object any more.
// 本智能指针对象不再拥有object对象指针,将它传给其它智能指针对象
T* release() WARN_UNUSED_RESULT {
T* retVal = ptr_;
ptr_ = NULL;
return retVal;
}
//赋值,接受对象的指针,放弃本智能指针原来拥有的对象,致使原对象引用-=1
//重新拥有新的对象
scoped_refptr<T>& operator=(T* p) {
// AddRef first so that self assignment should work
if (p)
p->AddRef();
T* old_ptr = ptr_;
ptr_ = p;
if (old_ptr)
old_ptr->Release();
return *this;
}
//赋值,复制,将r指针所指的内容赋值给本智能指针
scoped_refptr<T>& operator=(const scoped_refptr<T>& r) {
return *this = r.ptr_;
}
//赋值,复制,将r指针所指的内容赋值给本智能指针
template <typename U>
scoped_refptr<T>& operator=(const scoped_refptr<U>& r) {
return *this = r.get();
}
//swap内部函数
void swap(T** pp) {
T* p = ptr_;
ptr_ = *pp;
*pp = p;
}
//交换两个智能指针内容
void swap(scoped_refptr<T>& r) {
swap(&r.ptr_);
}
//那个对象,带引用计数的
protected:
T* ptr_;
};
// Handy utility for creating a scoped_refptr<T> out of a T* explicitly without
// having to retype all the template arguments
// 返回对象指针的智能指针对象
template <typename T>
scoped_refptr<T> make_scoped_refptr(T* t) {
return scoped_refptr<T>(t);
}
#endif // BASE_MEMORY_REF_COUNTED_H_
scoped_refptr
最新推荐文章于 2025-05-19 15:34:41 发布