谷歌的C++智能指针实现

//智能指针基类所有智能指针对象都继承该类
class
RefCountedBase { public: virtual int AddRef()=0; virtual int Release()=0; protected: virtual ~RefCountedBase(){} };

智能指针对象类的实现

template <class T>
class RefCountedPtr
{
public:
    RefCountedPtr() : m_ptr(NULL)
    {
    }

    RefCountedPtr(T* p) : m_ptr(p) 
    {
        if (m_ptr)m_ptr->AddRef();
    }

    RefCountedPtr(const RefCountedPtr<T>& r) : m_ptr(r.m_ptr) 
    {
        if (m_ptr)m_ptr->AddRef();
    }

    template <typename U>
    RefCountedPtr(const RefCountedPtr<U>& r) : m_ptr(r.get())
    {
        if (m_ptr) m_ptr->AddRef();
    }

    ~RefCountedPtr()
    {
        if (m_ptr) m_ptr->Release();
    }

    T* get() const { return m_ptr; }
    operator T*() const { return m_ptr; }
    T* operator->() const { return m_ptr; }


    T* release() 
    {
        T* retVal = m_ptr;
        m_ptr = NULL;
        return retVal;
    }

    RefCountedPtr<T>& operator=(T* p) 
    {
        // AddRef first so that self assignment should work
        if (p) p->AddRef();
        if (m_ptr) m_ptr ->Release();
        m_ptr = p;
        return *this;
    }

    RefCountedPtr<T>& operator=(const RefCountedPtr<T>& r)
    {
        return *this = r.m_ptr;
    }

    template <typename U>
    RefCountedPtr<T>& operator=(const RefCountedPtr<U>& r)
    {
        return *this = r.get();
    }

    void swap(T** pp)
    {
        T* p = m_ptr;
        m_ptr = *pp;
        *pp = p;
    }

    void swap(RefCountedPtr<T>& r)
    {
        swap(&r.m_ptr);
    }

protected:
    T* m_ptr;
};

 

使用示例如下:

class MyClass:public RefCountedBase
{
public:
       ...

       virtual int AddRef() 
    {
        return ++m_count;
    }

    virtual int Release() 
    {
        int count = --m_count;
        if (!count) 
        {
            delete this;
        }
        return count;
    }

private:
     
  int m_count;
};

RefCountedPtr<MyClass> test=new RefCountedObject<MyClass>()

使用C++模板可以省去每创建一个对象都要实现AddRef() 及 Release() 接口的麻烦

template <class T>
class RefCountedObject : public T 
{
public:
    RefCountedObject() : m_count(0) 
    {
    }

    template<typename P>
    explicit RefCountedObject(P p) : T(p), m_count(0) 
    {
    }

    template<typename P1, typename P2>
    RefCountedObject(P1 p1, P2 p2) : T(p1, p2), m_count(0) 
    {
    }

    template<typename P1, typename P2, typename P3>
    RefCountedObject(P1 p1, P2 p2, P3 p3) : T(p1, p2, p3), m_count(0) 
    {
    }

    template<typename P1, typename P2, typename P3, typename P4>
    RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4)
        : T(p1, p2, p3, p4), m_count(0) 
    {
    }

    template<typename P1, typename P2, typename P3, typename P4, typename P5>
    RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5)
        : T(p1, p2, p3, p4, p5), m_count(0) 
    {
    }

    virtual int AddRef() 
    {
        return AtomicOps::Increment(&m_count);
    }

    virtual int Release() 
    {
        int count = AtomicOps::Decrement(&m_count);
        if (!count) 
        {
            delete this;
        }
        return count;
    }

protected:
    virtual ~RefCountedObject() {
    }

    int m_count;
};

使用方法如下:

class Test:public RefCountedBase
{
public:
    void test(){}
};

//AddRef()及Release()方法由模板实现,无需再实现
RefCountedPtr<Test> test=new RefCountedObject<Test>()
test->test();

 

 

 

 

 

转载于:https://www.cnblogs.com/zentel/p/3722499.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值