martin

thinking

常用链接

统计

software

最新评论

智能指针的代码实例

前段时间,写了一点关于智能指针的东西,有读者反映没有代码比较难懂.现给出源码,并稍微加以解释.

智能指针类用到的基类的定义:
template<typename T>
class HandleBase
{
public:

    typedef T element_type;

    T* get() const
    {
        return _ptr;
    }
   
   //重载->操作符,返回所指对象的指针.

    T* operator->() const
    {
        if(!_ptr)
        {
            //
            // We don't throw directly NullHandleException here to
            // keep the code size of this method to a minimun (the
            // assembly code for throwing an exception is much bigger
            // than just a function call). This maximises the chances
            // of inlining by compiler optimization.
            //
            throwNullHandleException(__FILE__, __LINE__);
        }

        return _ptr;
    }

//  通过智能指针获取所指对象的引用.

    T& operator*() const
    {
        if(!_ptr)
        {
            //
            // We don't throw directly NullHandleException here to
            // keep the code size of this method to a minimun (the
            // assembly code for throwing an exception is much bigger
            // than just a function call). This maximises the chances
            // of inlining by compiler optimization.
            //
            throwNullHandleException(__FILE__, __LINE__);
        }

        return *_ptr;
    }

    operator bool() const
    {
        return _ptr ? true : false;
    }

    void swap(HandleBase& other)
    {
        std::swap(_ptr, other._ptr);
    }

    T* _ptr;

private:

    void throwNullHandleException(const char *, int) const;
};

......


// 智能指针类定义

template<typename T>
class Handle : public HandleBase<T>
{
public:

    Handle(T* p = 0)               //智能指针的构造函数
    {
        this->_ptr = p;

        if(this->_ptr)
        {
            this->_ptr->__incRef();         //在构造函数中增加所指对象的引用计数
        }
    }

    template<typename Y>                  //拷贝构造函数
    Handle(const Handle<Y>& r)
    {
        this->_ptr = r._ptr;

        if(this->_ptr)
        {
            this->_ptr->__incRef();   //在构造函数中增加所指对象的引用计数
        }
    }

    Handle(const Handle& r)         //拷贝构造函数
    {
        this->_ptr = r._ptr;

        if(this->_ptr)
        {
            this->_ptr->__incRef();    //在构造函数中增加所指对象的引用计数
        }
    }

    ~Handle()
    {
        if(this->_ptr)
        {
            this->_ptr->__decRef();      //在析构函数中减少所指对象的引用计数
        }
    }

// 重载=操作符, 要注意所有权 (即,对原实例的处理).

    Handle& operator=(T* p)         
    {
        if(this->_ptr != p)
        {
            if(p)
            {
                p->__incRef();      //增加新指对象的引用计数
            }

            T* ptr = this->_ptr;
            this->_ptr = p;

            if(ptr)
            {
                ptr->__decRef();   //减少原来所指对象的引用计数
            }
        }
        return *this;
    }

    template<typename Y>
    Handle& operator=(const Handle<Y>& r)
    {
        if(this->_ptr != r._ptr)
        {
            if(r._ptr)
            {
                r._ptr->__incRef();   //增加新指对象的引用计数
            }

            T* ptr = this->_ptr;
            this->_ptr = r._ptr;

            if(ptr)
            {
                ptr->__decRef();      //减少原来所指对象的引用计数
            }
        }
        return *this;
    }

    Handle& operator=(const Handle& r)
    {
        if(this->_ptr != r._ptr)
        {
            if(r._ptr)
            {
                r._ptr->__incRef();            //增加新指对象的引用计数
            }

            T* ptr = this->_ptr;
            this->_ptr = r._ptr;

            if(ptr)
            {
                ptr->__decRef();            //减少原来所指对象的引用计数
            }
        }
        return *this;
    }

 跟智能指针配合使用的对象.要能够跟指针智能配合使用,这些对象应该是从下列类的派生类的实例.
class SimpleShared
{
public:

    SimpleShared();
    SimpleShared(const SimpleShared&);

    virtual ~SimpleShared()
    {
    }

    SimpleShared& operator=(const SimpleShared&)
    {
        return *this;
    }

    void __incRef()
    {
        assert(_ref >= 0);
        ++_ref;
    }

    void __decRef()
    {
        assert(_ref > 0);
        if(--_ref == 0)               // 如果引用计数为0,则摧毁对象本身.
        {
            if(!_noDelete)
            {
                _noDelete = true;
                delete this;
            }
        }
    }

    int __getRef() const
    {
        return _ref;
    }

    void __setNoDelete(bool b)
    {
        _noDelete = b;
    }

private:

    int _ref;
    bool _noDelete;
};

class Shared
{
public:

    Shared();
    Shared(const Shared&);

    virtual ~Shared()
    {
    }

    Shared& operator=(const Shared&)
    {
        return *this;
    }

    virtual void __incRef();
    virtual void __decRef();
    virtual int __getRef() const;
    virtual void __setNoDelete(bool);

protected:

#if defined(_WIN32)
    LONG _ref;
#elif defined(ICE_HAS_ATOMIC_FUNCTIONS)
    volatile int _ref;
#else
    int _ref;
    Mutex _mutex;
#endif
    bool _noDelete;
};

posted on 2009-03-09 16:07 martin_yahoo 阅读(2137) 评论(6)  编辑 收藏 引用

评论

# re: 智能指针的代码实例 2009-03-09 17:18 eXile

ICE中的实现吧,这个并没有解决循环引用的问题。所以他还有一个GCShared   回复  更多评论   

# re: 智能指针的代码实例[未登录] 2009-03-09 18:01 martin_yahoo

u r right. These souce code is abstracted from ICE.  回复  更多评论   

# re: 智能指针的代码实例 2009-03-11 09:45 梦在天涯

mark  回复  更多评论   

# re: 智能指针的代码实例 2009-05-05 09:02 brightcoder

一个句柄类而已  回复  更多评论   

# re: 智能指针的代码实例 2013-04-26 23:58 WWW

这是哪个开源项目中的啊?  回复  更多评论   

# re: 智能指针的代码实例 2013-06-08 17:13 tb

模仿写一下  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理