iniwf

风是温柔的,雨是伤心的,云是快乐的,月是多情的,爱是迷失的,恋是醉人的,情是难忘的,天是长久的,地是永恒的

智能指针的代码实例

转自http://www.cppblog.com/martin/archive/2009/03/09/75988.aspx


智能指针类用到的基类的定义:
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 *intconst;
}
;




// 智能指针类定义

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-16 19:55 iniwf 阅读(427) 评论(0)  编辑 收藏 引用 所属分类: C&C++


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


导航

统计

常用链接

留言簿(2)

随笔分类

随笔档案

收藏夹

IT技术

积分与排名

最新评论

阅读排行榜

评论排行榜