zoukankan      html  css  js  c++  java
  • 带引用计数的智能指针

    #define DbgPrint(...) printf(__VA_ARGS__)
    #define DbgEnter() {DbgPrint("+%s/n",__FUNCSIG__);DbgPrint("+pointer_=0x%x,refcount_=0x%x,*refcount_=0x%x/n",pointer_,refcount_,refcount_==0?0:*refcount_);}
    #define DbgLeave() {DbgPrint("+pointer_=0x%x,refcount_=0x%x,*refcount_=0x%x/n",pointer_,refcount_,refcount_==0?0:*refcount_);DbgPrint("-%s/n",__FUNCSIG__);}
    //带引用计数的智能指针
    template<typename T>
    class XSharedPointer
    {
    public:
    XSharedPointer();
    XSharedPointer(T
    * t);
    XSharedPointer(
    const XSharedPointer<T>& t);
    virtual ~XSharedPointer();
    T
    & operator*();
    T
    * operator->();
    XSharedPointer
    <T>& operator=(const XSharedPointer<T>& t);
    XSharedPointer
    <T>& operator=(const T* t);
    void clear();
    protected:
    private:
    T
    * pointer_;
    unsigned
    int* refcount_;
    };
    template
    <typename T>
    XSharedPointer
    <T>::XSharedPointer():refcount_(0),pointer_(0)
    {
    DbgEnter();
    pointer_
    =0;
    refcount_
    =0;
    DbgLeave();
    }

    template
    <typename T>
    XSharedPointer
    <T>::XSharedPointer(T* t):refcount_(0),pointer_(0)
    {
    DbgEnter();
    pointer_
    =t;
    if (t!=0)
    {
    refcount_
    =new unsigned int(1);
    }
    else
    {
    refcount_
    =0;
    }
    DbgLeave();
    }

    template
    <typename T>
    XSharedPointer
    <T>::XSharedPointer(const XSharedPointer<T>& t):refcount_(0),pointer_(0)
    {
    DbgEnter();
    pointer_
    =t.pointer_;
    refcount_
    =t.refcount_;
    if (refcount_!=0)
    {
    (
    *refcount_)++;
    }
    DbgLeave();
    }

    template
    <typename T>
    XSharedPointer
    <T>::~XSharedPointer()
    {
    DbgEnter();
    //引用计数-1;若==1则删除指针
    if (refcount_!=0&&(*refcount_)--==1)
    {
    delete pointer_;
    delete refcount_;
    pointer_
    =0;
    refcount_
    =0;
    }
    DbgLeave();
    }

    template
    <typename T>
    T
    & XSharedPointer<T>::operator *()
    {
    DbgEnter();
    DbgLeave();
    return *pointer_;
    }
    template
    <typename T>
    T
    * XSharedPointer<T>::operator ->()
    {
    DbgEnter();
    DbgLeave();
    return pointer_;
    }
    template
    <typename T>
    XSharedPointer
    <T>& XSharedPointer<T>::operator=(const XSharedPointer<T>& t)
    {
    DbgEnter();
    if (&t==this)
    {
    return *this;
    }
    if (refcount_!=0&&(*refcount_)--==1)
    {
    delete pointer_;
    delete refcount_;
    pointer_
    =0;
    refcount_
    =0;
    }
    refcount_
    =t.refcount_;
    if (refcount_!=0)
    {
    (
    *refcount_)++;
    }
    pointer_
    =t.pointer_;
    DbgLeave();
    return *this;
    }
    template
    <typename T>
    XSharedPointer
    <T>& XSharedPointer<T>::operator =(const T* t)
    {
    DbgEnter();
    if (refcount_!=0&&(*refcount_)--==1)
    {
    delete pointer_;
    delete refcount_;
    pointer_
    =0;
    refcount_
    =0;
    }
    refcount_
    =new int(1);
    pointer_
    =t;
    DbgLeave();
    return *this;
    }
    /* 清除共享指针 */
    template
    <typename T>
    void XSharedPointer<T>::clear()
    {
    DbgEnter();
    if (refcount_!=0&&(*refcount_)--==1)
    {
    delete pointer_;
    delete refcount_;
    }
    pointer_
    =0;
    refcount_
    =0;
    DbgLeave();
    }

      

  • 相关阅读:
    JQuery EasyUi之界面设计——通用的JavaScript
    easyui datagrid 行右键 动态获取并生成toolbar 按钮
    Jq基础拓展 json to String
    电信光纤猫(HG8245)破解教程 开启无线网、路由器功能(第二章)
    plsql 无需配置客户端连接.
    中文分词常用算法之基于词典的正向最大匹配
    中文分词常用算法之基于词典的逆向最大匹配
    SQL SERVER安装序列号
    查询锁事务及语句
    SQL Server 数据库备份
  • 原文地址:https://www.cnblogs.com/oyjj/p/2132871.html
Copyright © 2011-2022 走看看